刷題記錄 CF每日一題打卡 2020.5月?-六月16

1.CF1355D Game With Array

給出兩個數NN,SS

問是否可以找到一個序列和一個數KK(0<=K<=S0<=K<=S)滿足:存在一個長度爲NN,和爲SS序列,在其中找不到一個子段的和爲KK或者SKS-K

如果可以,輸出YES以及找到的序列還有KK,否則,輸出NO

首先手玩一下樣例自己造幾組數據可以發現,如果2n>s2*n>s的話一定是無解的
然後構造一個除了最後一位之外全爲1的數列即可

#include<bits/stdc++.h>
using namespace std;
int n,m,s;
const int maxn=1e5+5;
int a[maxn],b[maxn],c[maxn];
int main(){
	cin>>n>>s;
	if(2*n>s){
		puts("NO");
		return 0;
	}
	else{
		puts("YES");
		for(int i=1;i<=n-1;i++){
			cout<<"1"<<" ";
		}
		cout<<s-n+1<<endl;
		cout<<s/2;
	}
	return 0;
}

2.CF1352D Alice, Bob and Candies

一個很屑的模擬

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int tt,n,m,a[maxn],b[maxn],num[maxn];
int main(){
	cin>>tt;
	while(tt--){
		cin>>n;
		int ans1=0,ans2=0,cnt=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		int i=0,j=n+1;
		bool c=false;
		int now1=0,now2=0;
		while(i<j){
			cnt++;
			if(!c){
				now1=0;
				while(1){
					i++;
					now1+=a[i];
					if(now1>now2)break;
				}
				if(i>=j){
					while(i>=j){
						now1-=a[i];
						i--;
					}
					i++;
				}
				ans1+=now1;
			}
			else{
				now2=0;
				while(1){
					j--;
					now2+=a[j];
					if(now2>now1)break;
				}
				if(i>=j){
					while(j<=i){
						now2-=a[j];
						j++;
					}
					j--;
				}
				ans2+=now2;
			}
			c=!c;
			if(i>=j-1)break;
		}
		printf("%d %d %d\n",cnt,ans1,ans2);
	}
	return 0;
}

3.CF1352F Binary String Reconstruction

每組數據有三個整數 n0,n1,n2n_0,n_1,n_2

你需要構造一個 0101 串,長度爲 n0+n1+n2+1n_0+n_1+n_2+1,對於每一個長度爲 22 的連續子串,總共有 n0n_0 個和爲 00, 有 n1n_1 個和爲 11,有 n2n_2 個和爲 22

保證輸入有解,對於每一組數據,輸出一行,爲一個 0101 串,沒有空格,如果有多組解,輸出任意一個即可。

先輸出a+1a+1個0,再輸出c+1c+1個1,最後輸出長度爲b1b-1的01串即可

#include<bits/stdc++.h>
using namespace std;
int t;
int a,b,c;
int main(){
	cin>>t;
	while(t--){
		cin>>a>>b>>c;
		int n=a+b+c+1;
		if(a==0&&b==0){
			for(int i=1;i<=n;i++){
				printf("1");
			}
			cout<<endl;
			continue;
		}
		if(b==0&&c==0){
			for(int i=1;i<=n;i++){
				printf("0");
			}
			cout<<endl;
			continue;
		}
		int ans1,ans2,ans3;
		for(int i=1;i<=a+1;i++){
			printf("0");
		}
		for(int i=1;i<=c+1;i++){
			printf("1");
		}
		for(int i=1;i<=b-1;i++){
			if(i%2==1)printf("0");
			else printf("1");
		}
		cout<<endl;
	}
	return 0;
}

4.CF1352E Special Elements

The first line contains an integer tt ( 1t10001 \le t \le 1000 ) — the number of test cases in the input. Then tt test cases follow.

Each test case is given in two lines. The first line contains an integer $ n $ ( $ 1 \le n \le 8000 $ ) — the length of the array aa . The second line contains integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n ).

It is guaranteed that the sum of the values of $ n $ for all test cases in the input does not exceed80008000

給你a數列,讓你判斷其中的每個元素是否能寫成數列中幾個連續元素之和,並統計個數。

暴力枚舉求後綴和,超過n就break

#include<bits/stdc++.h>
using namespace std;
int t,n,m;
int a[8005],sum[8005];
int dd[8005];
signed main(){
	cin>>t;
	while(t--){
		cin>>n;
		memset(dd,0,sizeof(dd));
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		for(int i=1;i<=n-1;i++){
			sum[i]=a[i];
			for(int j=i+1;j<=n;j++){
				sum[j]=sum[j-1]+a[j];
				if(sum[j]>n)break;
				dd[sum[j]]=1;
			}
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(dd[a[i]]==1)ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}

5.CF1364C Ehab and Prefix MEXs

給出一個長度爲 nn 的序列 AA,你需要找到一個長度爲 nn 的序列 BB,滿足 Ai=mex({B1,B2,,Bn})A_i=mex(\{B_1,B_2,\dots,B_n\})

其中 mexmex 函數的結果是最小的未出現在集合中的非負整數

如果i>1a[i1]<a[i]i>1,a[i-1]<a[i]就把這個數填上,如樣例3,輸入爲1131,1,3i=1i=1時候,輸出00f=1f=1,i=2,輸出f=2f=2f++f++變爲22,當i=3i=3a[2]<a[3]a[2]<a[3],此時必須填入a[2]a[2],因爲a[2]a[2]是前一組中不存在的最小數字,不填上它的話就不可能讓這個mexmex變成a[3]a[3]

#include<bits/stdc++.h>
using namespace std;
int n,m,t;
const int maxn=1e5+5;
int a[maxn],b[maxn],c[maxn];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		c[a[i]]=1;
	}
	int f=0;
	for(int i=1;i<=n;i++){
		if(i>1&&a[i-1]<a[i]){
			cout<<a[i-1]<<" ";
		}
		else{
			while(c[f]){
				f++;
			}
			cout<<f++<<" ";
		}
	}
	return 0;
}

6.CF1361A Johnny and Contribution

咕了,待補

7.CF1365D Solve The Maze

要封住每一個壞人,問是否所有好人都能到達終點

先將壞人邊上的路變成牆,再從終點dfsdfs看遇到的好人數量是否爲總數即可
需要特判終點不可到達並且好人數量不爲00的情況

#include<bits/stdc++.h>
using namespace std;
int n,m,t;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,-1,1};
char a[55][55];
int mz[55][55];
int vis[55][55];
int ans=0;
int flag=0;
void dfs(int x,int y){
	vis[x][y]=1;
	if(a[x][y]=='G')ans++;
	for(int i=0;i<4;i++){
		int xx=dx[i]+x;
		int yy=dy[i]+y;
		if(vis[xx][yy]==0&&mz[xx][yy]!=1&&xx>=1&&yy>=1&&xx<=n&&yy<=m){
			dfs(xx,yy);
		}
	}
	return;
}
signed main(){
	cin>>t;
	while(t--){
		int kk=0;
		memset(vis,0,sizeof vis);
		memset(mz,0,sizeof mz);
		ans=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>a[i][j];
				if(a[i][j]=='#'){
					mz[i][j]=1;
				}
				if(a[i][j]=='G'){
					kk++;
				}
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(a[i][j]=='B'){
					mz[i-1][j]=mz[i+1][j]=mz[i][j+1]=mz[i][j-1]=1;
				}
			}
		}
		if(mz[n][m]==1&&kk!=0){
			printf("NO\n");
			continue;
		}
		dfs(n,m);
		if(ans==kk)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章