Codeforces_831
A.线性判断。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int n,a[105] = {0}; int main() { ios::sync_with_stdio(0); cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; int t = 1; while(a[t] > a[t-1]) t++; while(a[t] == a[t-1]) t++; while(a[t] < a[t-1]) t++; if(t > n) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
B.map转换一下。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; map<char,char> mp; string s,s1,s2; int main() { ios::sync_with_stdio(0); cin >> s1 >> s2 >> s; for(int i = 0;i < 26;i++) { mp[s1[i]] = s2[i]; mp[s1[i]+'A'-'a'] = s2[i]+'A'-'a'; } for(int i = 0;i < s.length();i++) { if(mp.count(s[i])) cout << mp[s[i]]; else cout << s[i]; } cout << endl; return 0; }
C.先求评委前缀和,若要某方案符合,则必须所有n个点在k个评委的评分间隔内,枚举每个评委,对应一个点,判断是否符合要求即可,注意重复的情况。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int n,k,a[2005],b[2005]; map<int,int> mp,ok; int main() { ios::sync_with_stdio(0); cin >> k >> n; for(int i = 1;i <= k;i++) cin >> a[i]; for(int i = 2;i <= k;i++) a[i] += a[i-1]; for(int i = 1;i <= k;i++) mp[a[i]] = 1; for(int i = 1;i <= n;i++) cin >> b[i]; for(int i = n;i >= 1;i--) b[i] -= b[1]; int ans = 0; for(int i = 1;i <= k;i++) { int flag = 1; for(int j = 1;j <= n;j++) { if(!mp.count(a[i]+b[j])) { flag = 0; break; } } if(flag && !ok.count(a[i])) { ans++; ok[a[i]] = 1; } } cout << ans << endl; return 0; }
D.先对人和钥匙排序,最优的结果肯定是人在钥匙中连续的某一段,暴力枚举每一段就可以了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int n,k,p,a[2005],b[2005]; int main() { ios::sync_with_stdio(0); cin >> n >> k >> p; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= k;i++) cin >> b[i]; sort(a+1,a+1+n); sort(b+1,b+1+k); int ans = INT_MAX; for(int i = 1;i+n-1 <= k;i++) { int maxx = 0; for(int j = 1;j <= n;j++) { int t = i+j-1; maxx = max(maxx,abs(a[j]-b[t])+abs(b[t]-p)); } ans = min(ans,maxx); } cout << ans << endl; return 0; }
E.给每个数字开个set,储存每个数字下标,原数组排序一下,从小到大开始操作,用树状数组计数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int n,a[100005],tree[100005] = {0}; set<int> s[100005]; inline int lowbit(int x) { return x&-x; } void update(int pos,int x) { while(pos <= n) { tree[pos] += x; pos += lowbit(pos); } } int getsum(int pos) { int sum = 0; while(pos > 0) { sum += tree[pos]; pos -= lowbit(pos); } return sum; } int main() { ios::sync_with_stdio(0); cin >> n; for(int i = 1;i <= n;i++) { cin >> a[i]; s[a[i]].insert(i); update(i,1); } sort(a+1,a+1+n); int now = 0; long long ans = 0; for(int i = 1;i <= n;i++) { int t = a[i]; auto it = s[t].lower_bound(now); if(it == s[t].end()) { ans += getsum(n)-getsum(now); it = s[t].lower_bound(0); now = 0; } ans += getsum(*it)-getsum(now); now = *it; update(*it,-1); s[t].erase(it); } cout << ans << endl; return 0; }
F.要求最大的d,使得
枚举每一个可能的 中d的值,对每一个可能的值计算d再判断。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; long long n,k,a[105],b[8000000]; int main() { ios::sync_with_stdio(0); cin >> n >> k; int cnt = 0; long long sum = k; for(int i = 1;i <= n;i++) { cin >> a[i]; sum += a[i]; for(int j = 1;j*j <= a[i];j++) { b[++cnt] = j; b[++cnt] = (a[i]+j-1)/j; } } sort(b+1,b+1+cnt); cnt = unique(b,b+1+cnt)-b-1; long long ans = 0; for(int i = 1;i <= cnt;i++) { long long now = 0; for(int j = 1;j <= n;j++) now += (a[j]+b[i]-1)/b[i]; if(sum/now >= b[i]) ans = max(ans,sum/now); } cout << ans << endl; return 0; }