Codeforces Round #555 (Div. 3)- E. Minimum Array-二分

版权声明:未经博主同意,不可转载 https://blog.csdn.net/pythonbanana/article/details/89606150

题目传送门
这道题是求字典序最小,那么贪心前面最小,典型的贪心。
使用multiset,存数据b。然后二分。
在这里插入图片描述
1,如果刚好找到n-a[i[,则选择n-a[i]。
2,,multiset中剩下的数都小于n-a[i],那么选最小的,开始元素。
3,n-a[i]-y n-a[i]+c (x,y >0),这是lower_bound会到后面,可以证明一定是选后面可以使用反证法证明。

注意:使用std::set::lower_bound,不要使用std::lower_bound(后者超时);
std::set::lower_bound与std::lower_bound的效率问题

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

#define pii pair<int, int>
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=(a);i<=(b);i++)
#define rep(i,a,b) for(int i=(a);i>=(b);i--)
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define Rand() (rand()|rand()<<15)
#define Srand() srand(time(0)^(unsigned)(unsigned long long)(new char))
#define typemax(type)   numeric_limits<type>::max()
#define typemin(type)   numeric_limits<type>::min()
typedef long long LL;

template<typename T> inline void read(T &x) {x = 0;int f = 1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}x=x*f;}
template<typename E> E gcd(E a,E b){return b==0?a:gcd(b,a%b);}
template<typename E> E lcm(E a,E b){return a*b/gcd(a,b);}
template<typename E> E ex_gcd(E a,E b,E &x,E&y) {if(b==0){x=1;y=0;}else {ex_gcd(b,a%b,y,x);y-=(a/b)*x;}}
template<typename E> E quick_mul(E a,E b,E c){E ans=0;while(b){if(b&1) ans=(ans+a)%c;a=(a+a)%c;b>>=1;}  return ans;}
template<typename E> E quick_pow(E a,E b,E c){E ans=1;while(b){if(b&1) ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
template<typename E> E inv1(E a,E b){return quick_pow(a,b-2,b);}
template<typename E> E inv2(E a,E b){E x,y;ex_gcd(a,b,x,y);return (x%b+b)%b;}

/*==================================Header Template=============================================*/

const double eps=1.0e-5;
const int maxn=2e5;
const LL mod=1e9+7;

int n = 0,m = 0;
int a[maxn+10],b[maxn+10],c[maxn+10];
multiset<int> st;
void solve(){
	multiset<int>::iterator it = st.begin();
	per(i,1,n){
		int minv = INF,num = 0;
		//it = lower_bound(st.begin(),st.end(),n - a[i]);//TLE
		it = st.lower_bound(n-a[i]);//multiset,AC
		if(it != st.end() ){
			c[i] = (a[i] + *it) % n;
			st.erase(it);
		}else{
			c[i] = (a[i] + *st.begin()) % n;
			st.erase(st.begin());
		}
	}
	per(i,1,n){
		printf("%d%c",c[i],i == n ? '\n' : ' ');
	}
}
int main(){
	//std::ios::sync_with_stdio(false);
	//cin.tie(0);cout.tie(0);
	while(~scanf("%d",&n)){

		per(i,1,n){
			scanf("%d",&a[i]);
		}
		per(i,1,n){
			scanf("%d",&b[i]);
			st.insert(b[i]);
		}
		solve();
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/pythonbanana/article/details/89606150