- 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;
}