- CF1355D Game With Array
- CF1352D Alice, Bob and Candies
- CF1352F Binary String Reconstruction
- CF1352E Special Elements
- CF1364C Ehab and Prefix MEXs
- CF1361A Johnny and Contribution
- CF1365D Solve The Maze
1.CF1355D Game With Array
給出兩個數,
問是否可以找到一個序列和一個數()滿足:存在一個長度爲,和爲序列,在其中找不到一個子段的和爲或者
如果可以,輸出YES以及找到的序列還有,否則,輸出NO
首先手玩一下樣例自己造幾組數據可以發現,如果的話一定是無解的
然後構造一個除了最後一位之外全爲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
每組數據有三個整數 。
你需要構造一個 串,長度爲 ,對於每一個長度爲 的連續子串,總共有 個和爲 , 有 個和爲 ,有 個和爲 。
保證輸入有解,對於每一組數據,輸出一行,爲一個 串,沒有空格,如果有多組解,輸出任意一個即可。
先輸出個0,再輸出個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 ( ) — the number of test cases in the input. Then 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 . The second line contains integers ( ).
It is guaranteed that the sum of the values of $ n $ for all test cases in the input does not exceed
給你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
給出一個長度爲 的序列 ,你需要找到一個長度爲 的序列 ,滿足 。
其中 函數的結果是最小的未出現在集合中的非負整數
如果就把這個數填上,如樣例3,輸入爲,時候,輸出,,i=2,輸出,變爲,當,,此時必須填入,因爲是前一組中不存在的最小數字,不填上它的話就不可能讓這個變成
#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
要封住每一個壞人,問是否所有好人都能到達終點
先將壞人邊上的路變成牆,再從終點看遇到的好人數量是否爲總數即可
需要特判終點不可到達並且好人數量不爲的情況
#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;
}