codeforces605
A. Three Friends
#include<bits/stdc++.h> #define rep(i, n) for(int i=0;i!=n;++i) #define per(i, n) for(int i=n-1;i>=0;--i) #define Rep(i, sta, n) for(int i=sta;i!=n;++i) #define rep1(i, n) for(int i=1;i<=n;++i) #define per1(i, n) for(int i=n;i>=1;--i) #define Rep1(i, sta, n) for(int i=sta;i<=n;++i) #define L k<<1 #define R k<<1|1 #define inf (0x3f3f3f3f) #define llinf (1e18) #define mid (tree[k].l+tree[k].r)>>1 #define ALL(A) A.begin(),A.end() #define SIZE(A) ((int)A.size()) typedef long long i64; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q,a,b,c; cin >> q; while(q--){ cin >> a >> b >> c; int minn = min(min(a,b),c); int maxx = max(max(a,b),c); cout << max(0,(maxx-minn)*2-4) <<'\n'; } return 0; }
B. Snow Walking Robot
#include<bits/stdc++.h> #define rep(i, n) for(int i=0;i!=n;++i) #define per(i, n) for(int i=n-1;i>=0;--i) #define Rep(i, sta, n) for(int i=sta;i!=n;++i) #define rep1(i, n) for(int i=1;i<=n;++i) #define per1(i, n) for(int i=n;i>=1;--i) #define Rep1(i, sta, n) for(int i=sta;i<=n;++i) #define L k<<1 #define R k<<1|1 #define inf (0x3f3f3f3f) #define llinf (1e18) #define mid (tree[k].l+tree[k].r)>>1 #define ALL(A) A.begin(),A.end() #define SIZE(A) ((int)A.size()) typedef long long i64; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q; cin >> q; while(q--){ string str; cin >> str; int numU = 0,numD = 0,numL = 0,numR = 0; for(int i=0;i!=SIZE(str);++i) switch(str[i]){ case 'D': ++numD; break; case 'U': ++numU; break; case 'L': ++numL; break; case 'R': ++numR; break; } int r = min(numD,numU),c = min(numL,numR); if(!r&&!c){ cout << 0 << '\n'; continue; } if(!c){ cout << 2 <<'\n'; cout << "UD" <<'\n'; continue; } if(!r){ cout << 2 <<'\n'; cout << "LR" <<'\n'; continue; } cout << (r + c)*2 <<'\n'; for(int i=0;i!=r;++i) cout << "U" ; for(int i=0;i!=c;++i) cout << "L" ; for(int i=0;i!=r;++i) cout << "D" ; for(int i=0;i!=c;++i) cout << "R" ; cout << '\n'; } return 0;
C. Yet Another Broken Keyboard
#include<bits/stdc++.h> #define rep(i, n) for(int i=0;i!=n;++i) #define per(i, n) for(int i=n-1;i>=0;--i) #define Rep(i, sta, n) for(int i=sta;i!=n;++i) #define rep1(i, n) for(int i=1;i<=n;++i) #define per1(i, n) for(int i=n;i>=1;--i) #define Rep1(i, sta, n) for(int i=sta;i<=n;++i) #define L k<<1 #define R k<<1|1 #define inf (0x3f3f3f3f) #define llinf (1e18) #define mid (tree[k].l+tree[k].r)>>1 #define ALL(A) A.begin(),A.end() #define SIZE(A) ((int)A.size()) typedef long long i64; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,k; cin >> n >> k; string str; cin >> str; set<char> S; char ch; while(k--){ cin >> ch; S.insert(ch); } i64 len = 0,sum = 0; bool flag = false; for(int i=0;i!=SIZE(str);++i){ if(!flag&&S.count(str[i])){ ++len; flag = true; continue; } if(flag){ if(S.count(str[i])) ++len; else{ sum += (1+len)*len/2; len = 0; flag = false; } } } sum += (1+len)*len/2; cout << sum <<'\n'; return 0; }
D. Remove One Element
题解:求出每一个位置往左和往右的最长位置,枚举每一个中间位置,如果左边小于右边则与最长长度比较
#include<bits/stdc++.h> #define rep(i, n) for(int i=0;i!=n;++i) #define per(i, n) for(int i=n-1;i>=0;--i) #define Rep(i, sta, n) for(int i=sta;i!=n;++i) #define rep1(i, n) for(int i=1;i<=n;++i) #define per1(i, n) for(int i=n;i>=1;--i) #define Rep1(i, sta, n) for(int i=sta;i<=n;++i) #define L k<<1 #define R k<<1|1 #define inf (0x3f3f3f3f) #define llinf (1e18) #define mid (tree[k].l+tree[k].r)>>1 #define ALL(A) A.begin(),A.end() #define SIZE(A) ((int)A.size()) typedef long long i64; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> arr(n),l(n),r(n); for(auto& v:arr) cin >> v; for(int i=0;i!=n;++i) l[i] = i>0&&arr[i]>arr[i-1] ? l[i] = l[i-1] + 1 : 1; for(int i=n-1;i>=0;--i) r[i] = i<n-1&&arr[i]<arr[i+1] ? r[i] = r[i+1] + 1 : 1; int maxValue = *max_element(l.begin(),l.end()); for(int index=1;index!=n-1;++index) if(arr[index-1] < arr[index+1]) maxValue = max(maxValue,l[index-1]+r[index+1]); cout << maxValue << '\n'; return 0; }
E. Nearest Opposite Parity
题解:将多源点转换为单点,因此反向建边寻找最短路径
#include<bits/stdc++.h> #define rep(i, n) for(int i=0;i!=n;++i) #define per(i, n) for(int i=n-1;i>=0;--i) #define Rep(i, sta, n) for(int i=sta;i!=n;++i) #define rep1(i, n) for(int i=1;i<=n;++i) #define per1(i, n) for(int i=n;i>=1;--i) #define Rep1(i, sta, n) for(int i=sta;i<=n;++i) #define L k<<1 #define R k<<1|1 #define inf (0x3f3f3f3f) #define llinf (1e18) #define mid (tree[k].l+tree[k].r)>>1 #define ALL(A) A.begin(),A.end() #define SIZE(A) ((int)A.size()) #define PII pair<int,int> typedef long long i64; using namespace std; const int maxn = 2e5 + 32; int arr[maxn],ans[maxn],dis[maxn]; vector<int> Edge[maxn]; void spfa(int cur){ memset(dis,inf,sizeof(dis)); queue<int> q; q.push(cur); dis[cur] = 0; while(!q.empty()){ int c = q.front(); q.pop(); for(auto& v:Edge[c]) if(dis[v] > dis[c] + 1){ dis[v] = dis[c] + 1; q.push(v); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i=1;i<=n;++i){ cin >> arr[i]; if(i-arr[i] >= 1) Edge[i-arr[i]].push_back(i); if(i+arr[i] <= n) Edge[i+arr[i]].push_back(i); } //0 odd n+1 even for(int i=1;i!=n+1;++i){ if(arr[i]&1) Edge[n+1].push_back(i); else Edge[0].push_back(i); } memset(ans,-1,sizeof(ans)); spfa(0); for(int i=1;i<=n;++i) if(arr[i]&1&&dis[i]!=inf) ans[i] = dis[i]; spfa(n+1); for(int i=1;i<=n;++i) if(!(arr[i]&1)&&dis[i]!=inf) ans[i] = dis[i]; for(int i=1;i<=n;++i) cout << (ans[i] == -1 ? -1:ans[i] -1) <<" \n"[i==n]; return 0; }
F题没写出来,菜得真实
不怕万人阻挡,只怕自己投降。
posted on 2019-12-19 18:22 chengyulala 阅读(222) 评论(0) 编辑 收藏 举报