A.每天更新判断。
#include<bits/stdc++.h> using namespace std; int n,k,a[105]; int main() { ios::sync_with_stdio(0); cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; int ans = 0,now = 0; for(int i = 1;i <= n;i++) { now += a[i]; if(now > 8) { now -= 8; ans += 8; } else { ans += now; now = 0; } if(ans >= k) { cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
B.先贪心把4个位置的坐满,然后贪心两个的位置(加上4个位置剩余组数),最后剩余的座位仅能单人,为前两次贪心剩余组数和。
#include<bits/stdc++.h> using namespace std; int n,k,a[10005]; int main() { ios::sync_with_stdio(0); cin >> n >> k; for(int i = 1;i <= k;i++) cin >> a[i]; int cnt4 = n; for(int i = 1;i <= k;i++) { while(a[i] >= 4 && cnt4) { a[i] -= 4; cnt4--; } } int cnt2 = cnt4+2*n; for(int i = 1;i <= k;i++) { while(a[i] >= 2 && cnt2) { a[i] -= 2; cnt2--; } } int cnt1 = cnt2+cnt4; for(int i = 1;i <= k;i++) cnt1 -= a[i]; if(cnt1 >= 0) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
C.dfs,每点的权值为儿子节点权值的平均值+1。
#include<bits/stdc++.h> using namespace std; int n; vector<int> v[100005]; double dfs(int now,int pre) { double ans = 1; int sz = v[now].size(); if(now != 1) sz--; for(int i = 0;i < v[now].size();i++) { int t = v[now][i]; if(t == pre) continue; ans += dfs(t,now)/sz; } return ans; } int main() { ios::sync_with_stdio(0); cin >> n; for(int i = 1;i < n;i++) { int x,y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } cout << fixed << setprecision(10) << dfs(1,0)-1 << endl; return 0; }
D.首先,对于x个数,都含有某个因子,那么他们被统计的次数为1C(x,1)+2C(x,2)+...+xC(x,x),可推得结果为x*2^(x-1)。
但事实上,某些数因子会被比它的因子替代,我们按因子从大到小容斥。
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; int n; long long two[200005],cnt[1000005] = {0},dp[1000005]; int main() { ios::sync_with_stdio(0); two[0] = 1; for(int i = 1;i <= 200000;i++) two[i] = two[i-1]*2%MOD; cin >> n; int maxx = 0; for(int i = 1;i <= n;i++) { int x; cin >> x; cnt[x]++; maxx = max(maxx,x); } long long ans = 0; for(int i = maxx;i > 1;i--) { long long t = 0; for(int j = i;j <= maxx;j += i) t += cnt[j]; if(t == 0) continue; dp[i] = t*two[t-1]%MOD; for(int j = 2*i;j <= maxx;j += i) dp[i] = (dp[i]-dp[j]+MOD)%MOD; ans = (ans+dp[i]*i)%MOD; } cout << ans << endl; return 0; }
Codeforces_839
时间: 2024-10-12 20:56:38