Codeforces Round 470 A-C
CodeForces 948A
要将S与W用D隔开,空地全填充D判断S四周是否有S即可。从一开始可避免维护边界情况,注意输入的换行符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <bits/stdc++.h>
using namespace std; int mat[505][505];
int main() { memset(mat,0,sizeof(mat)); int n,m,ans=0; char c; cin>>n>>m; getchar(); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%c",&c); if(c=='S') mat[i][j]=1; else if(c=='W') mat[i][j]=2; } getchar(); } bool isOK=true; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(mat[i][j]==1) if(mat[i-1][j]==2''mat[i+1][j]==2''mat[i][j+1]==2''mat[i][j-1]==2) isOK=false;
} } if(isOK==true) { cout<<"Yes"<<endl; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(mat[i][j]==1) cout<<"S"; else if(mat[i][j]==2) cout<<"W"; else cout<<"D";
} cout<<endl; } } else { cout<<"No"<<endl; } return 0; }
|
CodeForces 948B
数学题…不明觉厉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <bits/stdc++.h>
using namespace std; const int N = 1000000 + 5; int prime[N]; void getprime() { int n = 2; while (n < N) { int num = n * 2; while (num < N) { prime[num] = n; num += n; } num = n + 1; while (prime[num] != 0 && num < N) { num++; } n = num; } } bool cmp(int x,int y) { return x>y; } int main() { getprime(); int x2,p2,lc,minn; cin >> x2; minn=x2; p2 = prime[x2]; for(int i=1;i<=p2;i++){ if(prime[x2 - p2 + i]) minn = min(minn, x2 - p2 + i - prime[x2 - p2 + i] + 1); } cout << minn << endl; return 0; }
|
CodeForces 948C
用一个升序的优先队列维护Ti-1数组的前缀和以及当Vi的和,优先队列的顶端小于当前的前缀和,就意味着当前堆不能满足融化的值,就将该堆清空,队列剩下的值维护剩下可以呗全部融化的堆,每次输出ans即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <bits/stdc++.h> #define maxn 100000+5 using namespace std; long long a[maxn],b[maxn],sum[maxn],ans; priority_queue<long long, vector<long long>, greater<long long> > q;; int main() { sum[0]=0; int n; scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%lld",&a[i]); for(int i=1; i<=n; i++) { scanf("%lld",&b[i]); sum[i]=sum[i-1]+b[i]; } for(int i=1;i<=n;i++) { ans=0; q.push(a[i]+sum[i-1]); while(!q.empty()&&q.top()<=sum[i]) { ans+=q.top()-sum[i-1]; q.pop(); } ans+=q.size()*b[i]; cout<<ans<<" "; } cout<<endl; return 0; }
|