Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解
A
判断下HQ9有没有出现过
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 112 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 char s[N]; 18 int main() 19 { 20 int i,j,k; 21 cin>>s; 22 k = strlen(s); 23 for(i = 0; i < k ;i++) 24 if(s[i]=='H'||s[i]=='9'||s[i]=='Q') 25 break; 26 if(i==k) 27 puts("NO"); 28 else 29 puts("YES"); 30 return 0; 31 }
B
模拟下就OK
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 112 12 #define LL long long 13 #define INF 0xfffffff 14 #define mod 1000003 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 char s[N]; 19 int a[550]; 20 int main() 21 { 22 int i,j,k; 23 a['>'] = 8; 24 a['<'] = 9; 25 a['+'] = 10; 26 a['-'] = 11; 27 a['.'] = 12; 28 a[','] = 13; 29 a['['] = 14; 30 a[']'] = 15; 31 cin>>s; 32 k = strlen(s); 33 int ans = 0; 34 for(i = 0;i < k ; i++) 35 { 36 // cout<<a[s[i]]<<endl; 37 ans = (ans*16+a[s[i]])%mod; 38 } 39 cout<<ans<<endl; 40 return 0; 41 }
C
题意有点费解 给定操作 使其数字变为字符 先给你字符 问原先数字为多少 模拟。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 112 12 #define LL long long 13 #define INF 0xfffffff 14 #define mod 256 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 char s[N]; 19 int a[550]; 20 int main() 21 { 22 int i,j,k; 23 gets(s); 24 k = strlen(s); 25 int ans = 0; 26 int kt = 0; 27 for(i = 0;i < k ; i++) 28 { 29 int x = s[i]; 30 int y = kt; 31 int g = 0;kt=0; 32 memset(a,0,sizeof(a)); 33 while(y) 34 { 35 a[g++] = y%2; 36 y/=2; 37 } 38 for(j =0 ; j < 8 ;j++) 39 kt+=pow(2,8-j-1)*a[j]; 40 memset(a,0,sizeof(a)); 41 g = 0; 42 while(x) 43 { 44 a[g++] = x%2; 45 x/=2; 46 } 47 int ans = 0 ; 48 for(j = 0 ; j < 8 ;j++) 49 ans+=pow(2,8-j-1)*a[j]; 50 kt = (kt-ans+mod)%mod; 51 cout<<kt<<endl; 52 kt = s[i]; 53 } 54 return 0; 55 }
D
题意更是费解 大意:你当前在某一个颜色块中 你有两个指向标 一个是向另一块前进的方向 另一个是在本块的前进方向 0块和边外不能走 问m次后你在哪个块中
模拟。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 112 12 #define LL long long 13 #define INF 0xfffffff 14 #define mod 256 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 char s[55][55]; 19 int a[55][55][4],n,k; 20 int judge(int x,int y) 21 { 22 if(x<0||x>=n||y<0||y>=k) 23 return 0; 24 if(s[x][y]=='0') return 0; 25 return 1; 26 } 27 int main() 28 { 29 int i,j,m,g; 30 cin>>n>>m; 31 for(i = 0; i < n ;i++) 32 cin>>s[i]; 33 k = strlen(s[1]); 34 for(i = 0 ;i < n ;i++) 35 { 36 for(j = 0; j < k ;j++) 37 { 38 for(g = j ; g >= 0 ; g--) 39 if(s[i][g]!=s[i][j]) break; 40 a[i][j][3] = g+1; 41 for(g = i ; g >= 0; g--) 42 if(s[g][j]!=s[i][j]) break; 43 a[i][j][0] = g+1; 44 for(g = j ; g < k; g++) 45 if(s[i][g]!=s[i][j]) break; 46 a[i][j][1] = g-1; 47 for(g = i ; g < n ;g++) 48 if(s[g][j]!=s[i][j]) break; 49 a[i][j][2] = g-1; 50 } 51 } 52 int d1 = 1,d2 = 0,x = 0,y = 0; 53 char c = s[0][0]; 54 int k1 = 1; 55 while(k1<=m) 56 { 57 if(d1==0||d1==2) 58 x = a[x][y][d1]; 59 else y = a[x][y][d1]; 60 if(d2==0||d2==2) 61 x = a[x][y][d2]; 62 else y = a[x][y][d2]; 63 int tx,ty; 64 if(d1==0) 65 { 66 tx = x-1;ty = y; 67 } 68 else if(d1==1) 69 { 70 tx = x;ty = y+1; 71 } 72 else if(d1==2) 73 { 74 tx = x+1;ty = y; 75 } 76 else 77 { 78 tx = x;ty = y-1; 79 } 80 if(!judge(tx,ty)) 81 { 82 //cout<<","; 83 if((d2+1)%4==d1) 84 d2 = (d1+1)%4; 85 else 86 { 87 88 d1 = (d1+1)%4;d2 = (d1-1+4)%4; 89 } 90 } 91 else 92 { 93 c = s[tx][ty]; 94 x = tx,y = ty; 95 } 96 k1++; 97 //x = tx 98 //cout<<c<<" "<<d1<<" "<<d2<<" "<<k1<<" "<<x<<" "<<y<<endl; 99 } 100 cout<<c<<endl; 101 return 0; 102 }
E
当时的思路貌似不太对 按照zp说的思路又重写了一遍
dp[i][j][0] 表示在i位置已经改变了j次方向为正的最大移动距离 dp[i][j][1]类似表反方向。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 112 12 #define LL long long 13 #define INF 0xfffffff 14 #define mod 256 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 int dp[N][55][2]; 19 char s[N]; 20 int main() 21 { 22 int i,j,k; 23 cin>>s; 24 cin>>k; 25 int kk = strlen(s); 26 for(i = 0 ; i < kk ; i++) 27 for(j = 0 ; j <= k ; j++) 28 dp[i][j][0] = dp[i][j][1] = -INF; 29 if(s[0]=='T') 30 { 31 for(i = 0 ; i <= k ; i++) 32 { 33 if(i%2!=0) 34 dp[0][i][1] = -1; 35 else 36 dp[0][i][0] = 0; 37 } 38 } 39 else 40 { 41 for(i = 0 ; i <= k ; i++) 42 if(i%2==0) 43 dp[0][i][0] = 1; 44 else 45 dp[0][i][1] = 0; 46 } 47 for(i = 1; i < kk ;i++) 48 { 49 if(s[i]=='T') 50 { 51 dp[i][0][0] = dp[i-1][0][1]; 52 dp[i][0][1] = dp[i-1][0][0]; 53 } 54 else 55 { 56 dp[i][0][0] = dp[i-1][0][0]+1; 57 dp[i][0][1] = dp[i-1][0][1]-1; 58 } 59 for(j = 1; j <= k ; j++) 60 { 61 if(s[i]=='T') 62 { 63 dp[i][j][0] = max(dp[i-1][j][1],dp[i-1][j-1][0]+1); 64 dp[i][j][1] = max(dp[i-1][j][0],dp[i-1][j-1][1]-1); 65 } 66 else 67 { 68 dp[i][j][0] = max(dp[i-1][j][0]+1,dp[i-1][j-1][1]); 69 dp[i][j][1] = max(dp[i-1][j][1]-1,dp[i-1][j-1][0]); 70 } 71 } 72 } 73 int ans1 = max(dp[kk-1][k][0],dp[kk-1][k][1]); 74 for(i = 0 ; i < kk ; i++) 75 for(j = 0 ; j <= k ; j++) 76 dp[i][j][0] = dp[i][j][1] = -INF; 77 if(s[0]=='T') 78 { 79 for(i = 0 ; i <= k ; i++) 80 { 81 if(i%2!=0) 82 dp[0][i][0] = 1; 83 else 84 dp[0][i][1] = 0; 85 } 86 } 87 else 88 { 89 for(i = 0 ; i <= k ; i++) 90 if(i%2==0) 91 dp[0][i][1] = -1; 92 else 93 dp[0][i][0] = 0; 94 } 95 for(i = 1; i < kk ;i++) 96 { 97 if(s[i]=='T') 98 { 99 dp[i][0][0] = dp[i-1][0][1]; 100 dp[i][0][1] = dp[i-1][0][0]; 101 } 102 else 103 { 104 dp[i][0][0] = dp[i-1][0][0]+1; 105 dp[i][0][1] = dp[i-1][0][1]-1; 106 } 107 for(j = 1; j <= k ; j++) 108 { 109 if(s[i]=='T') 110 { 111 dp[i][j][0] = max(dp[i-1][j][1],dp[i-1][j-1][0]+1); 112 dp[i][j][1] = max(dp[i-1][j][0],dp[i-1][j-1][1]-1); 113 } 114 else 115 { 116 dp[i][j][0] = max(dp[i-1][j][0]+1,dp[i-1][j-1][1]); 117 dp[i][j][1] = max(dp[i-1][j][1]-1,dp[i-1][j-1][0]); 118 } 119 } 120 } 121 int ans2 = max(dp[kk-1][k][0],dp[kk-1][k][1]); 122 //cout<<ans1<<" "<<ans2<<endl; 123 int ans = max(ans1,ans2); 124 cout<<ans<<endl; 125 return 0; 126 }