Codeforces Round # 634 (Div. 3) Supplemental questions


A. Candies and Two Sisters
check-in questions, directly output

Code
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n;
		read(n);
		int t=n/2;
		cout<<n-t-1<<endl;
	}
 	return 0;
}



B. Construct the String
length a contains b different letters, just output the first b letters in a loop.

Code

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n,a,b;
		read(n),read(a),read(b);
		int k=a/b;
		int cnt=0;
		bool flag=true;
		for(;;){
			for(int i=0;i<b;i++){
				for(int j=0;j<k;j++){
					putchar('a'+i);
					if(++cnt==n){
						flag=false;
						break;
					}
				}
				if(!flag) break;
			}
			if(!flag) break;
		}
		putchar('\n');
	}
 	return 0;
}



C. Two Teams Composing
counts the number of each element and records the maximum number of all elements. If the largest number of elements is x, then the two queues are constructed in two ways: the first team does not contain x, the second team is all x; the first team contains x, and the second team is all x, compare the two The maximum value of the result of the construction method is sufficient.

Code

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
map<int,int> a;
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n;
		int m=0;
		int sum=0;
		read(n);
		//map<int,int> a;
		a.clear();
		for(int i=0;i<n;i++){
			int t;
			read(t);
			if(!a[t]) sum++;
			a[t]++;
			m=max(m,a[t]); 
		}
		cout<<max(min(sum-1,m),min(sum,m-1))<<"\n";
	}
 	return 0;
}



D. Anti-Sudoku
My idea is that each of the nine small squares must change a number, so that the changed number can affect one row and one column, and finally let the nine numbers affect the different rows and columns. For different Sudoku, the position of the change can be fixed, and the method of change can be to increase the number by one.
The method given by the answer is very clever, just make all the numbers in Sudoku become other numbers, for example, all 2 become 1

Code
#define LOCAL0
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
vector<vector<int> > a(10,vector<int>(10)); 
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		char s;
		for(int i=1;i<=9;i++)
			for(int j=1;j<=9;j++){
				cin>>s;
				a[i][j]=s-'1';
			}
		a[1][1]=(a[1][1]+1)%9;
		a[2][5]=(a[2][5]+1)%9;
		a[3][9]=(a[3][9]+1)%9;
		
		a[5][2]=(a[5][2]+1)%9;
		a[6][6]=(a[6][6]+1)%9;
		a[4][7]=(a[4][7]+1)%9;

		a[9][3]=(a[9][3]+1)%9;
		a[7][4]=(a[7][4]+1)%9;
		a[8][8]=(a[8][8]+1)%9;

		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=9;j++)
				printf("%d",a[i][j]+1);
			putchar('\n');
		}
	}
 	return 0;
}



E1. Three Blocks Palindrome (easy version)
only thought of the solution of E1 during the game, and had to pay it eight times. . .
My idea is to maintain the prefix sum (number) for each element once, so that the number of each element within a certain range can be easily obtained. The sequence required by the title is divided into three parts. My approach is to fix the middle part and then traverse the maximum number of elements on both sides, because it is the left and right boundaries l and r of the middle part. The complexity of traversing l and r is n2.

Code

#include <iostream>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
//map<int,int> idx;
int sum[30][3000];
int idx[300];
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n,cnt=0,ans=0;
		read(n);
		memset(idx,0,sizeof(idx));
		memset(sum,0,sizeof(sum));
		for(int i=1;i<=n;i++){
			int v;
			read(v);
			if(!idx[v]) idx[v]=++cnt;
			for(int j=1;j<=cnt;j++){
				sum[j][i]=sum[j][i-1];
				if(idx[v]==j) sum[j][i]++;
			}
		}
			
		for(int l=1;l<=n;l++)
			for(int r=l;r<=n;r++){
				for(int i=1;i<=cnt;i++)
					for(int j=1;j<=cnt;j++){
						//if(l!=1&&j==i) continue;
						ans=max(ans,sum[i][r]-sum[i][l-1]+2*min(sum[j][n]-sum[j][r],sum[j][l-1]));
						//cout<<l<<" "<<r<<" "<<ans<<endl;
					}
			}
		for(int i=1;i<=cnt;i++) ans=max(ans,sum[cnt][n]);
		cout<<ans<<"\n";
	}
 	return 0;
}
 

E2. Three Blocks Palindrome (hard version)
In fact, the first half of the scale is also the number of maintenance prefixes, but the latter is equivalent to fixing the two sides during the solution, traversing the middle part, and counting the position of each element during preprocessing. When solving, according to the element traversal, first count the number of such elements on [1, n], and then take each of these elements c, you can get the position of the left and right ends of the middle part.

Code
#include <iostream>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
int sum[30][3000];
int idx[300];
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n;
		read(n);
		vector<int> a(n+1);
		vector<vector<int> > pref(201,vector<int>(n+1));
		vector<vector<int> > pos(201);
		for(int i=1;i<=n;i++) read(a[i]);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=200;j++) pref[j][i]=pref[j][i-1];
			pref[a[i]][i]++;
			pos[a[i]].push_back(i);
		}
		int ans=0;
		for(int i=1;i<=200;i++){
			int u=pos[i].size();
			ans=max(ans,u);
			for(int j=1;2*j<=u;j++){
				int l=pos[i][j-1];
				int r=pos[i][u-j];
				for(int k=1;k<=200;k++){
					if(k==i) continue;
					ans=max(ans,2*j+pref[k][r-1]-pref[k][l]);
				}
			}
		}
		cout<<ans<<"\n";
	}
 	return 0;
}
 

F. Robots on a Grid
The figure given above is composed of some rings, otherwise the robot will go all the way to the point that has not been passed, and the size of the figure is limited. If the two robots meet at a certain moment, then they will walk together afterwards, which is the disallowed condition in the question, because the maximum period of the ring does not exceed n * m, so as long as the two robots do not meet in n * m steps , Then they cannot meet. Count where the robot on each grid will be after n * m steps, after which you can get how many robots on the grid can go to a certain grid after n * m steps, and how many robots on the black grid can go A black grid can easily calculate the answer, and let the robot on each grid take n * m steps will obviously time out, so the method of multiplication is used.

Code
#include <iostream>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
inline void read(int &p)
{
    p=0;int flag=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();}
    while(isdigit(c)) {p=p*10+c-'0';c=getchar();}p*=flag;
}
int sum[30][3000];
int idx[300];
int main()
{
	#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
	int q;
	read(q);
	while(q--){
		int n,m;
		read(n),read(m);
		vector<int> col(n*m),dp(n*m),dpn(n*m),ans(n*m),co(n*m);
		string s;
		for(int i=0;i<n;i++){
			cin>>s;
			for(int j=0;j<m;j++){
				col[i*m+j]=(s[j]=='0');
			}
		}
		for(int i=0;i<n;i++){
			cin>>s;
			for(int j=0;j<m;j++){
				int nxt=-1;
				if(s[j]=='U') nxt=(i-1)*m+j;
				if(s[j]=='D') nxt=(i+1)*m+j;
				if(s[j]=='L') nxt=i*m+j-1;
				if(s[j]=='R') nxt=i*m+j+1;
				dp[i*m+j]=nxt;
			}
		}
		int k=0;
		n*=m;
		while((1<<k)<=n) k++;
		while(k--){
			for(int i=0;i<n;i++){
				dpn[i]=dp[dp[i]];
			}
			for(int i=0;i<n;i++){
				dp[i]=dpn[i];
			}
		}
		for(int i=0;i<n;i++){
			ans[dp[i]]++;
			co[dp[i]]+=col[i];
		}
		int res=0,_res=0;
		for(int i=0;i<n;i++){
			if(ans[i]) res++;
			if(co[i]) _res++;
		}
		cout<<res<<" "<<_res<<"\n";
	}
 	return 0;
}
 

Guess you like

Origin www.cnblogs.com/DinoMax/p/12702321.html