A. Drinks Choosing
统计每种酒有多少人偏爱他们。 ki 为每种酒的偏爱人数。
输出ans = (n + 1)/2 > Σki / 2 ? (n + 1)/2 - Σki / 2 + (Σki / 2) * 2 : (n + 1)/2 * 2
#include<iostream> #include<algorithm> using namespace std; int n,k; const int L = 1000+50; int a[L]; int main() { cin>>n>>k; int t; for(int i = 0;i<n;++i) { cin>>t; a[t]++; } n = n%2? (n + 1)/2: n/2; int cnt = 0; for(int i = 1;i<=k;++i) cnt+= a[i]/2; if(cnt<n) cout<< n - cnt + 2*cnt<<endl; else cout<<n*2<<endl; return 0; }
B. Sport Mafia
二分查找二元一次方程的解
设 放入操作数为a,取出操作数为b,有
a*(a + 1)/2 - b = k
a + b = n
联立得a*(a + 3) = 2*(n + k) 二分查找a,再解出b即可
#include<iostream> using namespace std; long long n,k; int main() { cin>>n>>k; long long lo = 0; long long hi = 5e9; long long mi = (lo + hi)>>1; while(lo <hi) { mi = (lo + hi)>>1; if(mi * (mi + 3) < 2*(n+k)) lo = mi+1; else hi = mi; } cout<<n - lo<<endl; return 0; return 0; }
C. Basketball Exercise
dp题
#include<iostream> #include<cstring> using namespace std; const int L = 1e5+50; int n; long long a[L],b[L]; long long ans; long long dp[L][3]; long long max(long long a,long long b) { return a>b?a:b; } int main() { cin>>n; for(int i = 1;i<=n;++i)cin>>a[i]; for(int i = 1;i<=n;++i)cin>>b[i]; dp[n][1] = a[n];dp[n][2] = b[n]; dp[n-1][1] = dp[n][2] + a[n-1]; dp[n-1][2] = dp[n][1] + b[n-1]; for(int i = n-2;i>=1;--i) { dp[i][1] += a[i]; dp[i][2] += b[i]; dp[i][1] += max(dp[i+1][2],dp[i+2][2]); dp[i][2] += max(dp[i+1][1],dp[i+2][1]); } cout<<max(dp[1][1],dp[1][2]); return 0; }
D1/D2. Submarine in the Rybinsk Sea
统计各串数的各个位对最终解的贡献即可。
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int L = 1e5+10; const long long MOD = 998244353; struct E{ char s[12]; int l; }str[L]; long long a[30]; int n; int NUM[12]; int sum[12]; long long ans; int main() { cin>>n; for(int i = 0;i<n;++i) { cin>>str[i].s; str[i].l = strlen(str[i].s); } int LEN = 0; for(int i = 0;i<n;++i) { NUM[str[i].l]++; LEN = max(LEN,str[i].l); } LEN *=2; for(int i = 1;i<12;++i) sum[i] = NUM[i] + sum[i-1]; for(int i = 0;i<n;++i) { reverse(str[i].s,str[i].s+str[i].l); for(int j = 0;j<str[i].l;++j) { a[j*2] += (str[i].s[j] - '0')*(sum[11] - sum[j]); a[j*2]%=MOD; a[j*2 + 1 ] += (str[i].s[j] - '0')*(sum[11] - sum[j]); a[j*2+1]%=MOD; } for(int j = 1;j<11;++j) { if(NUM[j] == 0) continue; int l = str[i].l - j; if(l<=0) break; for(int k=1;k<=l;++k) { a[j*2+k-1] += (str[i].s[j + k - 1]-'0')*2* (NUM[j]); a[j*2+k-1]%=MOD; } } } long long L = 1; for(int i = 0;i<LEN;++i) { ans += (L%=MOD)*(a[i]%MOD); ans%=MOD; L*=10; } cout<<ans<<endl; return 0; }