首页 > 代码库 > Codeforces_828
Codeforces_828
A.模拟,注意单人的时候判断顺序。
#include<bits/stdc++.h> using namespace std; int n,a,b; int main() { ios::sync_with_stdio(0); cin >> n >> a >> b; int b1 = b,b2 = 0,ans = 0; for(int i = 1;i <= n;i++) { int x; cin >> x; if(x == 1) { if(a) a--; else if(b1) { b1--; b2++; } else if(b2) b2--; else ans++; } else { if(b1) b1--; else ans += 2; } } cout << ans << endl; return 0; }
B.对于每个B点,更新最极端边界,确定最后的长宽,注意不用涂和不成立的情况。
#include<bits/stdc++.h> using namespace std; int n,m; string s[105]; int main() { ios::sync_with_stdio(0); cin >> n >> m; for(int i = 1;i <= n;i++) { cin >> s[i]; s[i] = ‘ ‘+s[i]; } int l = 105,r = 0,h = 0,d = 105,cnt = 0; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { if(s[i][j] == ‘W‘) continue; cnt++; l = min(l,j); r = max(r,j); h = max(h,i); d = min(d,i); } } int ans = max(r-l+1,h-d+1); if(ans <= 0) { cout << 1 << endl; return 0; } if(ans > n || ans > m) { cout << -1 << endl; return 0; } cout << ans*ans-cnt << endl; return 0; }
C.对于每一个串,选择更后面的起点更新。
#include<bits/stdc++.h> using namespace std; char a[2000005] = ""; int n; int main() { ios::sync_with_stdio(0); cin >> n; int len = 0; while(n--) { string s; int x,now = 0; cin >> s >> x; while(x--) { int xx; cin >> xx; int i = max(now,xx); for(int j = i-xx;j < s.length();i++,j++) { len = max(len,i); a[i] = s[j]; } now = max(now,i); } } for(int i = 1;i <= len;i++) { if(a[i]) cout << a[i]; else cout << ‘a‘; } cout << endl; return 0; }
D.从一点个拉出k条链来,每条链长度尽可能相等。
#include<bits/stdc++.h> using namespace std; int n,k; int main() { ios::sync_with_stdio(0); cin >> n >> k; int t = n-k-1; if(t%k == 0) cout << 2+t/k*2 << endl; else if(t%k == 1) cout << 2+t/k*2+1 << endl; else cout << 2+t/k*2+2 << endl; int i; for(i = n-1;i >= max(n-k,1);i--) cout << n << " " << i << endl; for(;i >= 1;i--) cout << i+k << " " << i << endl; return 0; }
E.因为询问的串长最多为10,我们可以每个字母对应的长度都开个树状数组。
#include<bits/stdc++.h> using namespace std; string s; int q,tree[4][11][11][100005] = {0}; map<char,int> mp; inline int lowbit(int x) { return x&-x; } void add(int x,int y,int z,int pos,int xx) { while(pos < s.length()) { tree[x][y][z][pos] += xx; pos += lowbit(pos); } } int getsum(int x,int y,int z,int pos) { int sum = 0; while(pos > 0) { sum += tree[x][y][z][pos]; pos -= lowbit(pos); } return sum; } int main() { ios::sync_with_stdio(0); cin >> s >> q; s = ‘ ‘+s; mp[‘A‘] = 0; mp[‘G‘] = 1; mp[‘C‘] = 2; mp[‘T‘] = 3; for(int i = 1;i < s.length();i++) { for(int j = 1;j <= 10;j++) add(mp[s[i]],i%j,j,i,1); } while(q--) { int x; cin >> x; if(x == 1) { string ss; cin >> x >> ss; for(int i = 1;i <= 10;i++) add(mp[s[x]],x%i,i,x,-1); for(int i = 1;i <= 10;i++) add(mp[ss[0]],x%i,i,x,1); s[x] = ss[0]; } else { int l,r; string ss; cin >> l >> r >> ss; int sum = 0; for(int i = 0;i < ss.length()&&l+i <= r;i++) sum += getsum(mp[ss[i]],(l+i)%ss.length(),ss.length(),r)-getsum(mp[ss[i]],(l+i)%ss.length(),ss.length(),l-1); cout << sum << endl; } } return 0; }
Codeforces_828
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。