A.map统计数量,更新最大值。
#include<bits/stdc++.h> using namespace std; int n; map<int,int> mp; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { mp.clear(); cin >> n; int ans,maxx = 0; for(int i = 1;i <= n;i++) { string s; int x; cin >> s >> x; mp[x]++; if(maxx == mp[x]) ans = min(ans,x); else if(maxx < mp[x]) { ans = x; maxx = mp[x]; } } cout << ans << endl; } return 0; }
B.统计b中每个字符个数,a串线性扫一遍,到没有的字符则停止。
#include<bits/stdc++.h> using namespace std; char a[100005],b[100005]; int cnt[128]; int main() { ios::sync_with_stdio(0); int T; scanf("%d",&T); while(T--) { scanf("%s%s",a,b); memset(cnt,0,sizeof(cnt)); int len1 = strlen(a),len2 = strlen(b); for(int i = 0;i < len2;i++) cnt[b[i]]++; int ans = 0; for(int i = 0;i < len1;i++) { if(!cnt[a[i]]) break; ans++; cnt[a[i]]--; } printf("%d\n",ans); } return 0; }
C.选最小值。
#include<bits/stdc++.h> using namespace std; int a,b,c; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> a >> b >> c; if(a < b && a < c) cout << "First" << endl; else if(b < a && b < c) cout << "Second" << endl; else cout << "Third" << endl; } return 0; }
D.2*C(a-1,b),预处理阶乘逆元。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; long long a[100005],fac[100005],inv[100005]; long long c(int n,int m) { return fac[n]*inv[m]%MOD*inv[n-m]%MOD; } int main() { inv[0] = 1; inv[1] = 1; for(int i = 2;i <= 100000;i++) inv[i] = inv[MOD%i]*(MOD-MOD/i)%MOD; for(int i = 2;i <= 100000;i++) inv[i] = inv[i-1]*inv[i]%MOD; fac[0] = 1; for(int i = 1;i <= 100000;i++) fac[i] = fac[i-1]*i%MOD; int T; scanf("%d",&T); while(T--) { long long x,y; scanf("%lld%lld",&x,&y); printf("%lld\n",2*c(x-1,y)%MOD); } return 0; }
E.流水线,n+k-1。
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int T; scanf("%d",&T); while(T--) { long long x,y; scanf("%lld%lld",&x,&y); printf("%lld\n",x+y-1); } return 0; }
F.预处理每个数下一个的位置,set储存位置模拟。
#include<bits/stdc++.h> using namespace std; int n,k,a[100005],ne[100005]; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; map<int,int> mp; set<int> s; for(int i = n;i >= 1;i--) { if(mp.count(a[i])) ne[i] = mp[a[i]]; else ne[i] = n+i; mp[a[i]] = i; } int ans = 0; for(int i = 1;i <= n;i++) { if(s.count(i)) { s.erase(i); s.insert(ne[i]); continue; } if(s.size() == k) s.erase(*s.rbegin()); s.insert(ne[i]); ans++; } cout << ans << endl; } return 0; }
G.n^2枚举区间,只要sum%lcm则符合要求,lcm过大时,break掉。
#include<bits/stdc++.h> using namespace std; int n,a[2005]; long long lcm(long long a,long long b) { long long t = __gcd(a,b); if(1e14/b > a/t) return a/t*b; return 1e18; } int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; int ans = 0; for(int i = 1;i <= n;i++) { long long t = 1,sum = 0; for(int j = i;j <= n;j++) { sum += a[j]; t = lcm(t,a[j]); if(t > 1e14) break; if(sum%t == 0) ans++; } } cout << ans << endl; } return 0; }
H.贪心,注意几种特例。
#include<bits/stdc++.h> using namespace std; int n,k; char ans[1000005]; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n >> k; ans[n] = 0; if(n%2 == 0 && k%2 == 1 || 9*n < k || n > 1 && k < 2) { cout << -1 << endl; continue; } for(int i = 0,j = n-1;i < j;i++,j--) { if(k >= 18) { k -= 18; ans[i] = ‘9‘; ans[j] = ‘9‘; } else { int t = k/2; k -= 2*t; ans[i] = t+‘0‘; ans[j] = t+‘0‘; } } if(n%2) ans[n/2] = k+‘0‘; cout << ans << endl; } return 0; }
I.n,m均为偶数,先手必输。
#include<bits/stdc++.h> using namespace std; int n,m; int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T--) { cin >> n >> m; if(n % 2 == 0 && m%2 == 0) cout << "abdullah" << endl; else cout << "hasan" << endl; } return 0; }
J.从大到小枚举长度每一约数,当前约数不成立则把这个约数的所有因子标记不成立。
#include<bits/stdc++.h> using namespace std; char a[100005]; int vis[100005]; int main() { ios::sync_with_stdio(0); int T; scanf("%d",&T); getchar(); while(T--) { gets(a+1); int n = strlen(a+1); n++; a[n] = ‘ ‘; memset(vis,0,sizeof(vis)); int flag = 0; for(int i = n/2;i >= 2;i--) { if(vis[i]) continue; if(n%i) continue; int ok = 1; for(int j = i;j <= n;j += i) { if(a[j] != ‘ ‘) ok = 0; } if(ok == 0) { for(int t = 2;t*t <= i;t++) { if(i%t == 0) { vis[t] = 1; vis[i/t] = 1; } } } else { flag = 1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
Codeforces_101498
时间: 2024-10-11 20:42:10