Codeforces Round #154 (Div. 2)
期末考试真是令人纠结的东西。。。最近除了做比赛之外不打算大规模刷题了。。要不期末稳跪。。。
A.有坑的一题。。。需要注意m和n的大小,别的没啥了。。。
A
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int main(){ 6 freopen("input.txt","r",stdin); 7 freopen("output.txt","w",stdout); 8 int n,m; 9 while(~scanf("%d%d",&n,&m)){ 10 int sum=n+m; 11 if(n>m){ 12 for(int i=1;i<=sum;i++){ 13 if(i&1){ 14 if(n>0){ 15 printf("B");n--;continue; 16 } 17 if(m>0){ 18 printf("G");m--;continue; 19 } 20 } 21 else{ 22 if(m>0){ 23 printf("G");m--;continue; 24 } 25 if(n>0){ 26 printf("B");n--;continue; 27 } 28 } 29 } 30 puts(""); 31 } 32 else{ 33 for(int i=1;i<=sum;i++){ 34 if(i%2==0){ 35 if(n>0){ 36 printf("B");n--;continue; 37 } 38 if(m>0){ 39 printf("G");m--;continue; 40 } 41 } 42 else{ 43 if(m>0){ 44 printf("G");m--;continue; 45 } 46 if(n>0){ 47 printf("B");n--;continue; 48 } 49 } 50 } 51 puts(""); 52 } 53 } 54 return 0; 55 }
B.乱搞即可...
B
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define N 100010 5 #define M 5010 6 using namespace std; 7 int s[N]; 8 int hash[N]; 9 int pre[N]; 10 int suc[N]; 11 int main(){ 12 freopen("input.txt","r",stdin); 13 freopen("output.txt","w",stdout); 14 int n; 15 while(~scanf("%d",&n)){ 16 memset(hash,0,sizeof(hash)); 17 memset(pre,0,sizeof(pre)); 18 memset(suc,0,sizeof(suc)); 19 for(int i=1;i<=n;i++){ 20 scanf("%d",&s[i]); 21 hash[s[i]]++; 22 } 23 for(int i=1;i<=5000;i++) 24 pre[i]=pre[i-1]+hash[i]; 25 for(int i=5000;i>=1;i--) 26 suc[i]=suc[i+1]+hash[i]; 27 int ans=1<<30; 28 for(int i=1;i<=5000;i++){ 29 if(hash[i]==0)continue; 30 int num=pre[i-1]; 31 if(i*2<=5000)num+=suc[i*2+1]; 32 ans=min(ans,num); 33 } 34 printf("%d\n",ans); 35 } 36 return 0; 37 }
C.我用bfs写的,每次走的时候就是上下左右,需要注意的是上或者下的特殊情况,向上走的话,如果上面的比底下的短,需要判断一下要走到哪里,向下也同理。
时间复杂度O(100*10^5)...
C
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<map> 5 #include<vector> 6 #include<queue> 7 #define N 110 8 #define M 100010 9 using namespace std; 10 typedef long long ll; 11 bool Map[N][M]; 12 int s[N],n; 13 int move[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; 14 void bfs(int sr,int sc,int er,int ec){ 15 queue<int>qr; 16 queue<int>qc; 17 queue<int>step; 18 qr.push(sr);qc.push(sc); 19 step.push(0); 20 while(!qr.empty()){ 21 int R=qr.front();qr.pop(); 22 int C=qc.front();qc.pop(); 23 int num=step.front();step.pop(); 24 if(R==er&&C==ec){ 25 printf("%d\n",num); 26 return ; 27 } 28 for(int i=0;i<4;i++){ 29 int rr=R+move[i][0]; 30 int cc=C+move[i][1]; 31 if(i==0){ 32 if(rr<1)continue; 33 else if(s[rr]+1<cc){ 34 cc=s[rr]+1; 35 if(Map[rr][cc]!=1)continue; 36 qr.push(rr);qc.push(cc);step.push(num+1); 37 Map[rr][cc]=0; 38 } 39 else if(Map[rr][cc]==1){ 40 qr.push(rr);qc.push(cc);step.push(num+1); 41 Map[rr][cc]=0; 42 } 43 }else if(i==1){ 44 if(rr>n)continue; 45 else if(s[rr]+1<cc){ 46 cc=s[rr]+1; 47 if(Map[rr][cc]!=1)continue; 48 qr.push(rr);qc.push(cc);step.push(num+1); 49 Map[rr][cc]=0; 50 } 51 else if(Map[rr][cc]==1){ 52 qr.push(rr);qc.push(cc);step.push(num+1); 53 Map[rr][cc]=0; 54 } 55 }else if(i==2){ 56 if(s[rr]+1<cc)continue; 57 if(Map[rr][cc]!=1)continue; 58 qr.push(rr);qc.push(cc);step.push(num+1); 59 Map[rr][cc]=0; 60 }else if(i==3){ 61 if(cc<1)continue; 62 if(Map[rr][cc]!=1)continue; 63 qr.push(rr);qc.push(cc);step.push(num+1); 64 Map[rr][cc]=0; 65 } 66 } 67 } 68 } 69 70 int main(){ 71 freopen("input.txt","r",stdin); 72 freopen("output.txt","w",stdout); 73 int r1,c1,r2,c2; 74 while(~scanf("%d",&n)){ 75 memset(Map,0,sizeof(Map)); 76 for(int i=1;i<=n;i++){ 77 scanf("%d",&s[i]); 78 for(int j=1;j<=s[i];j++) 79 Map[i][j]=1; 80 Map[i][s[i]+1]=1; 81 } 82 scanf("%d%d%d%d",&r1,&c1,&r2,&c2); 83 bfs(r1,c1,r2,c2); 84 } 85 return 0; 86 }
D.预处理,sum[i][j]表示以(1,1)为左上角,(i,j)为右下角的矩形内'a'的数量,然后就可以O(1)求出某个矩形内'a'的数量了,要保证矩形四个角的字母相同,我们枚举每两行,然后找出来所有满足条件的位置(对于每个字母都分开存),然后O(n)的时间统计就行了,类似于上一场的c....
D
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<vector> 6 #define N 410 7 using namespace std; 8 typedef long long ll; 9 vector<int>hash[26]; 10 char s[N][N]; 11 int sum[N][N]; 12 int cnt[N][N]; 13 int n,m,k; 14 int cal(int x1,int y1,int x2,int y2){ 15 int ans= sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]; 16 return ans; 17 } 18 int main(){ 19 freopen("input.txt","r",stdin); 20 freopen("output.txt","w",stdout); 21 while(~scanf("%d%d%d",&n,&m,&k)){ 22 memset(cnt,0,sizeof(cnt)); 23 memset(sum,0,sizeof(sum)); 24 for(int i=1;i<=n;i++){ 25 scanf("%s",s[i]+1); 26 for(int j=1;j<=m;j++) 27 cnt[i][j]=cnt[i][j-1]+(s[i][j]=='a'); 28 } 29 for(int i=1;i<=n;i++){ 30 for(int j=1;j<=m;j++) 31 for(int row=1;row<=i;row++){ 32 sum[i][j]+=cnt[row][j]; 33 } 34 } 35 ll ans=0; 36 for(int i=1;i<=n;i++){ 37 for(int j=i+1;j<=n;j++){ 38 for(int l=0;l<26;l++)hash[l].clear(); 39 for(int l=1;l<=m;l++){ 40 if(s[i][l]==s[j][l]){ 41 hash[s[i][l]-'a'].push_back(l); 42 } 43 } 44 for(int l=0;l<26;l++){ 45 if(hash[l].size()<2)continue; 46 for(int a=0,b=1;a<hash[l].size();a++){ 47 while(b<hash[l].size()&&cal(i,hash[l][a],j,hash[l][b])<=k){ 48 ++b; 49 } 50 ll num=b-a-1; 51 52 ans+=(num>0)?num:0; 53 } 54 } 55 } 56 } 57 cout<<ans<<endl; 58 } 59 return 0; 60 }