A.b序列从大到小填a序列中的0,在判断。
#include<bits/stdc++.h> using namespace std; int n,m,a[105],b[105]; int main() { ios::sync_with_stdio(0); cin >> n >> m; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= m;i++) cin >> b[i]; sort(b+1,b+1+m); reverse(b+1,b+1+m); int now = 0; for(int i = 1;i <= n;i++) { if(a[i] == 0) a[i] = b[++now]; } for(int i = 2;i <= n;i++) { if(a[i-1] > a[i]) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
B.分一个位置不同和两个位置不同两种情况。一个位置不同的直接改成没出现的数字。两个位置不同的把两种情况都试一下。
#include<bits/stdc++.h> using namespace std; int n,a[1005],b[1005],c[1005],ok[1005] = {0},x[3]; int main() { ios::sync_with_stdio(0); cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= n;i++) cin >> b[i]; int cnt = 0; for(int i = 1;i <= n;i++) { if(a[i] == b[i]) ok[a[i]] = 1; else x[++cnt] = i; } if(cnt == 1) { for(int i = 1;i <= n;i++) { if(!ok[i]) a[x[1]] = i; } } else { for(int i = 1;i <= n;i++) c[i] = a[i]; for(int i = 1;i <= n;i++) { if(!ok[i]) { a[x[1]] = i; ok[i] = 1; break; } } for(int i = 1;i <= n;i++) { if(!ok[i]) a[x[2]] = i; } int cnt1 = 0,cnt2 = 0; for(int i = 1;i <= n;i++) { if(a[i] != b[i]) cnt1++; if(a[i] != c[i]) cnt2++; } if(cnt1 != 1 || cnt2 != 1) swap(a[x[1]],a[x[2]]); } for(int i = 1;i <= n;i++) cout << a[i] << " "; return 0; }
C.打表每个字母增加每个数值的ans。
#include<bits/stdc++.h> using namespace std; int n,q,ans[128][1505] = {0},cnt[1505]; string s; int main() { ios::sync_with_stdio(0); cin >> n >> s >> q; s = ' '+s; for(char c = 'a';c <= 'z';c++) { memset(cnt,0,sizeof(cnt)); for(int i = 1;i <= n;i++) { cnt[i] = cnt[i-1]; if(s[i] != c) cnt[i]++; } for(int i = 0;i <= n;i++) { for(int j = i+1;j <= n;j++) { ans[c][cnt[j]-cnt[i]] = max(ans[c][cnt[j]-cnt[i]],j-i); } } for(int i = 1;i <= n;i++) ans[c][i] = max(ans[c][i-1],ans[c][i]); } while(q--) { int x; cin >> x >> s; cout << ans[s[0]][x] << endl; } return 0; }
D.判断每个环在第几层,0层和偶数层的加,奇数层的减。
#include<bits/stdc++.h> #define PI acos(-1) using namespace std; int n,x[1005],y[1005],r[1005],cnt[1005] = {0}; int main() { ios::sync_with_stdio(0); cin >> n; for(int i = 1;i <= n;i++) cin >> x[i] >> y[i] >> r[i]; for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(i == j) continue; if(sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j])) <= r[j]-r[i]) cnt[i]++; } } double ans = 0; for(int i = 1;i <= n;i++) { if(cnt[i] == 0 || cnt[i]%2) ans += PI*r[i]*r[i]; else ans -= PI*r[i]*r[i]; } cout << fixed << setprecision(10) << ans << endl; return 0; }