A.统计每个字母数量,比较是否超过k。
#include<bits/stdc++.h> using namespace std; int n,k,cnt[26] = {0}; string s; int main() { ios::sync_with_stdio(0); cin >> n >> k >> s; for(int i = 0;i < s.length();i++) cnt[s[i]-'a']++; for(int i = 0;i < 26;i++) { if(cnt[i] > k) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
B.只要有奇数,First必赢,因为总能到奇数个数为奇数个的情况。
#include<bits/stdc++.h> using namespace std; int n,a[1000005]; int main() { ios::sync_with_stdio(0); cin >> n; int cnt = 0; for(int i = 1;i <= n;i++) { cin >> a[i]; if(a[i]%2) cnt++; } if(cnt > 0) cout << "First" << endl; else cout << "Second" << endl; return 0; }
C.当在[1,x]中随机取一个的时候,最小值期望为x/2,取的次数越多,期望越小。我们把优先次数少的分给大的数。
#include<bits/stdc++.h> using namespace std; int n,a[200005],ans[200005]; struct xx { int x,id; friend bool operator <(xx a,xx b) { return a.x > b.x; } }b[200005]; 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].x; b[i].id = i; } sort(a+1,a+1+n); sort(b+1,b+1+n); for(int i = 1;i <= n;i++) ans[b[i].id] = a[i]; for(int i = 1;i <= n;i++) cout << ans[i] << " "; cout << endl; return 0; }
D.首先图是连通的,若有-1的点存在,则必可以通过这个点,dfs把其它点都调整好。
若没有-1的点存在,我们随便选一个点,dfs下去,最后判断这个点是否符合。
#include<bits/stdc++.h> using namespace std; int n,m,a[300005],vis[300005] = {0},dep[300005] = {0}; struct xxx { int to,id; xxx(int a,int b):to(a),id(b){}; }; vector<xxx> v[300005]; vector<int> ans; bool dfs(int now,int pre) { vis[now] = 1; for(int i = 0;i < v[now].size();i++) { int t = v[now][i].to; if(t == pre || vis[t]) continue; if(dfs(t,now)) { dep[now]++; ans.push_back(v[now][i].id); } } if(a[now] == 1 && dep[now]%2 == 0 || a[now] == 0 && dep[now]%2 == 1) return 1; return 0; } 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++) { int x,y; cin >> x >> y; v[x].push_back(xxx(y,i)); v[y].push_back(xxx(x,i)); } int ok = 0; for(int i = 1;i <= n;i++) { if(a[i] == -1) { dfs(i,0); ok = 1; break; } } if(!ok && dfs(1,0)) { cout << -1 << endl; return 0; } cout << ans.size() << endl; for(int i = 0;i < ans.size();i++) cout << ans[i] << " "; cout << endl; return 0; }