Codeforces Round #767 (Div. 2) (Updated)

Posted by bidntrade on Sun, 23 Jan 2022 03:08:18 +0100

title : Codeforces Round #767 (Div. 2)
date : 2022-1-23
Tags: ACM, practice notes
author : Linno

Title link: https://codeforces.com/contest/1629

Test progress: 4/7

A-Download More RAM

Give you the initial memory capacity k and n expansion packs. If your current memory is larger than the expansion pack's requirement ai, you can increase the memory bi, which can only be used once per package. How much memory do you have in the end?

thinking

Sort and traverse at the cost of each package.

Code

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

struct X{
	int a,b;
}s[N];

bool cmp(X x,X y){
	return x.a<y.a;
}

signed main(){
	int n,k,t;
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(int i=1;i<=n;i++) cin>>s[i].a;
		for(int i=1;i<=n;i++) cin>>s[i].b;
		sort(s+1,s+1+n,cmp);
		for(int i=1;i<=n;i++){
			if(k>=s[i].a){
				k+=s[i].b;
			}else break;
		}
		cout<<k<<"\n";
	}
	return 0;
}

B-GCD Arrays

Give you L and R to indicate that you have a sequence of all integers in [L,R]. Then you can perform k operations, select two numbers at a time, multiply them, remove them from the sequence, and add the results to the sequence. Ask if you can use k operations to make the GCD of all the numbers in the sequence larger than 1.

thinking

All even GCDs are not 1, while the two adjacent GCDs must be 1, so picking out all odd numbers is definitely optimal. To find the odd number k, you need k-1 operations to merge all odd numbers and then use one operation to make the odd even. This is a general case. In special cases, if there is only one number left, you just need to decide if it is 1.

Code

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}

signed main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
//  freopen("in.cpp","r",stdin);
//  freopen("out.cpp","w",stdout);
	int t,l,r,k,num;
	cin>>t;
	while(t--){
		cin>>l>>r>>k;
		int flag=0;
		if(r-l==k){ //So there's only one element left 
			if(l==r&&r==1){ //The final element is 1
				flag=0; 
			}else flag=1;
		}else{
			if(l&1){ //If l is odd 
				num=(r-l)/2+1;
			}else num=(r-l-1)/2+1;
			if(num<=k) flag=1;
		}
		if(flag) puts("YES");
		else puts("NO");
	}
	return 0;
}

C-Meximum Array

Give you a sequence a, you can do any number of operations, each operation can put the first k number of MEX in a into sequence b, and delete these numbers (k self-extracting), ask the scheme that makes the B dictionary order the largest.

thinking

Enumerate backward and forward, you must find the global MEX for the first time, then observe the maximum MEX of subsequent sequences, add b one by one, then replace MEX. For more details on implementation, see the code.

Code

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}

int t,n,a[N],num[N],mex,tmp; 
vector<int>b;
int vis[N],visnum;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
//  freopen("in.cpp","r",stdin);
//  freopen("out.cpp","w",stdout);
	cin>>t;
	while(t--){
		cin>>n;
		memset(vis,0,sizeof(vis));
		memset(num,0,sizeof(num));
		memset(a,0,sizeof(a));
		b.clear();
		visnum=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			num[a[i]]++;
		}
		for(int j=0;j<=200005;j++) if(!num[j]){
			 tmp=mex=j;
			 break;
		}
		for(int i=1;i<=n;i++){
			num[a[i]]--;
			if(!vis[a[i]]){
				vis[a[i]]=1;
			}
			while(vis[visnum]) visnum++;
			if(!num[a[i]]){
				tmp=min(tmp,a[i]);
			}
			if(visnum==mex){
				b.push_back(mex);
				mex=tmp;
				for(int j=0;j<visnum;j++) vis[j]=0;
				visnum=0;
			}
		}
		if(visnum) b.push_back(0);
		cout<<b.size()<<"\n";
		for(int i=0;i<b.size();i++) cout<<b[i]<<" ";
		cout<<"\n";
	}
	return 0;
}

D-Peculiar Movie Preferences

meaning of the title

You are given n strings up to 3 lengths to form a sequence, and you are asked if there is a scheme for splicing the strings of the subsequences into palindromes.

thinking

The hash value is calculated for each string first. Considering a maximum length of 3, we use the enumerated prefix of each string as the palindrome center, and then calculate whether the hash of the palindrome portion corresponding to the following string has been calculated before. Of course, consider this set of samples, "abc ba", and then it becomes suffix palindrome center, you can reverse all the strings before counting them again.

Code

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define B 31
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}

int get_hash(string s){
	int res=0;
	for(int i=0;i<s.length();i++) res=res*B+s[i];
//	cout<<s<<" "<<res<<"hs\n";
	return res;
}

int inv(string s){
	int res=0;
	for(int i=s.length()-1;i>=0;i--) res=res*B+s[i];
//	cout<<s<<" "<<res<<"inv\n";
	return res;
}

bool hw(string s){
	int len=s.length();
	for(int i=0;i<=len/2;i++){
		if(s[i]!=s[len-1-i]) return false;
	}
	return true;
}

int t,n;
string str[N];
map<int,int>mp;

signed main(){
	cin>>t;
	while(t--){
		cin>>n;
		mp.clear();
		int flag=0;
		for(int i=1;i<=n;i++){
			cin>>str[i];
			int hs=get_hash(str[i]);
			if(!mp[hs]) mp[hs]=i; //mp Records the top position of the entire character hash value 
			string tmp="";
			for(int j=0;j<str[i].length();j++){ //Enumerated prefixes 
				if(hw(tmp)){
					int hsh=inv(str[i].substr(j));
					if(mp[hsh]){
						flag=1;break;
					}
				}
				tmp+=str[i][j];
			}
		}
		for(int i=1;i<=n;i++) reverse(str[i].begin(),str[i].end());
		mp.clear();
		for(int i=n;i>=1;i--){
			int hs=get_hash(str[i]);
			if(!mp[hs]) mp[hs]=n-i+1; //mp Records the top position of the entire character hash value 
			string tmp="";
			for(int j=0;j<str[i].length();j++){ //Enumerated prefixes 
				if(hw(tmp)){
					int hsh=inv(str[i].substr(j));
					if(mp[hsh]){
						flag=1;break;
					}
				}
				tmp+=str[i][j];
			}
		}
		if(flag) puts("YES");
		else puts("NO");
	}
	return 0;
}

Topics: C++ Programming string ICPC acm