Codeforces Round 1019 (Div. 2) 题解

A Common Multiple

You are given an array of integers a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,,an. An array x1,x2,…,xmx_1, x_2, \ldots, x_mx1,x2,,xm is beautiful if there exists an array y1,y2,…,ymy_1, y_2, \ldots, y_my1,y2,,ym such that the elements of yyy are distinct (in other words, yi≠yjy_i\neq y_jyi=yj for all 1≤i,j≤m1 \le i, j \le m1i,jm), and the product of xix_ixi and yiy_iyi is the same for all 1≤i≤m1 \le i \le m1im (in other words, xi⋅yi=xj⋅yjx_i\cdot y_i = x_j\cdot y_jxiyi=xjyj for all 1≤i,j≤m1 \le i , j \le m1i,jm).

Your task is to determine the maximum size of a subsequence∗^{\text{∗}} of array aaa that is beautiful.

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		int n=read();
		vi v(n);
		For(i,n) v[i-1]=read();
		sort(ALL(v));
		int s=unique(ALL(v))-v.begin();
		cout<<s<<endl;
	}
	return 0;
}


B Binary Typewriter

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
ll l[212345],r[212345];
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		int n=read();
		string s;cin>>s;
		int p=0;		
		Rep(i,n) {
			if(i>0 && s[i]!=s[i-1] || i==0 && s[i]=='1') ++p;
		}
		if(p==2) p=1;
		else if(p>2) p-=2;
		cout<<p+n<<endl;
	}
	return 0;
}


C Median Splits

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (212345)
int a[MAXN];
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		int n=read(),k=read();
		For(i,n) a[i]=read()>k;
		int p=0,t=0,l=-1,r=-1;
		For(i,n) {
			if(a[i]==0)++p;else --p;
			if(p>=0) {
				if(p==1 && i<n && a[i+1]==1) continue;
				t++,p=0;
				if(t==1) l=i;
			} 
		}
		if(t>=2) {
			puts("YES");continue;
		}
		reverse(a+1,a+1+n);
		p=0,t=0;
		For(i,n) {
			if(a[i]==0)++p;else --p;
			if(p>=0) {
				if(p==1 && i<n && a[i+1]==1) continue;
				t++,p=0;
				if(t==1) r=i;
			}
		}
		if(t>=2) {
			puts("YES");continue;
		}
		if(l!=-1 && r!=-1 && l+r<n) {
			puts("YES");continue;
		}
		puts("NO");
	}
	return 0;
}


D Local Construction

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
ll a[212345],an[212345];
int c[50];
int main()
{
//	freopen("D.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		int n=read();
		For(i,n) a[i]=read();
		MEM(c)MEM(an)
		
		For(i,n) if(a[i]!=-1) c[a[i]]++;
		For(i,49) if(i>=2) c[i]+=c[i-2];
		vector< vi > v(50);
		ll ma=0,posn1=0;
		For(i,n) if(a[i]!=-1){
			v[a[i]].pb(i);
			gmax(ma,a[i])
		}else posn1=i;
		
		int t=0;
		if(ma%2==1) t=n-c[ma];
		else t=c[ma]+1;
		
		For(j,ma) if(SI(v[j])){
			int r=c[j],l=r-SI(v[j])+1;
			
			if(j%2==1) {
				r=n-r+1,l=n-l+1,swap(l,r);
			}
			int posl=0,posr=SI(v[j])-1;
			if(j%2==1) {
				For(i,n) {
					if(a[i]==j) an[i]=r--,++posl;
					else if((a[i]>j || a[i]==-1) ) break;
				}
			}
			if(j%2==0) {
				ForD(i,n) {
					if(a[i]==j) an[i]=l++,--posr;
					else if((a[i]>j || a[i]==-1) ) break;
				}
			} 
			
			Fork(p,posl,posr) an[v[j][p]]=l++;
		} 
		an[posn1]=t;
		
		PRi(an,n)
	} 
	return 0;
}


E Keep the Sum

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

ll a[212345],n,k;
ll pra[212345];
pi p[212345];
int h[212345];
vector<tuple<int,int,int> > vop;
void opr(int id1,int id2,int x1) { //a[id1]->x1
	int t=a[id1]-x1;
	if(id1==id2 || t==0) return;
	assert(a[id1]+a[id2]==k);
	a[id1]-=t;a[id2]+=t;
	vop.pb(make_tuple(id1,id2,t));
}
void work() {
	n=read(),k=read();
	For(i,n) a[i]=read();
	bool fl=0;
	For(i,n-1) if(a[i]>a[i+1]) fl=1;
	if(!fl) {
		puts("0");return ;
	}
	
	For(i,n) p[i]=mp(a[i],i);
	sort(p+1,p+1+n);
	
	int l=1,r=n;
	while(l<r) {
		ll t=p[l].fi+p[r].fi;
		if(t==k) break;
		else if(t<k) ++l;
		else --r;
	}
	if(l>=r || p[l].fi+p[r].fi!=k) {
		puts("-1");return ;
	}
	vector<tuple<int,int,int> > op;
	int id1=p[l].se,id2=p[r].se;
	if(id1!=1 && id2!=1) {
		opr(id1,id2,a[1]);
		id1=1;
	}else if(id1!=1) swap(id1,id2);
	if(id2!=n) {
		opr(id1,id2,k-a[n]);
		id2=n;
	}
	opr(id1,id2,0);
	For(i,n) p[i]=mp(a[i],i);
	sort(p+2,p+n);
	For(i,n) h[i] = p[i].se,pra[i]=a[i];
	Fork(i,2,n) {
		if(h[i]!=i && h[i]>0) {
			int now=i;
			opr(id1,id2,pra[now]);
			id1=now;
			while(1) {
				opr(id1,id2,pra[h[now]]);
				id1=h[now];
				int nx=h[now];
				h[now]=0;
				now=nx;
				if(now==i) break;
			}
			id1=1;
		}
	}
	if(a[1]!=0) {
		opr(id1,id2,0);
	}
	cout<<SI(vop)<<endl;
	for(auto p:vop) {
		cout<<get<0>(p)<<" ";
		cout<<get<1>(p)<<" ";
		cout<<get<2>(p)<<endl;
	}	
	vop.resize(0);
}
int main()
{
//	freopen("E.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		work();
	} 
	return 0;
}


F Maximize Nor

定义 nor(x,y)=¬(x∨y),x,y=0,1nor(x,y)=\lnot {(x \lor y)} ,x,y=0,1nor(x,y)=¬(xy),x,y=0,1
数的nor为其按位进行nor的值。
nor(a,b,c)=nor(nor(a,b),c)nor(a,b,c)=nor(nor(a,b),c)nor(a,b,c)=nor(nor(a,b),c)
给你一个由 k位整数 a1,a2,…,ana1,a2,…,ana1,a2,,an 组成的数组。对于每个索引 i ,找出max(nor(al,al+1,…,ai,…,ar))max(nor(a_l, a_{l+1}, \ldots,a_i,\ldots, a_r))max(nor(al,al+1,,ai,,ar))

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值