微众笔试4.20
第一题
思路:记录每回合差值,每充值1元相当于对应的第i回合差值-i,所以只需求保证每回合最终差值小于0就是需要充值的钱数
#include<iostream> #include<vector> using namespace std; typedef long long LL; int main() { int n; LL aSum, bSUm, ans; aSum = bSum = ans = 0; cin >> n; vector<LL> a(n), b(n), c(n); for(int i = 0; i < n; i ++) cin >> a[i]; for(int i = 0; i < n; i ++) cin >> b[i]; for(int i = 0; i < n; i ++) { aSum += a[i]; bSum += b[i]; c[i] = bSum - aSum; } for(int i = 0; i < n; i ++) { LL t = c[i] / (i + 1); if(t * (i + 1) <= c[i]) t = t + 1; res = max(res, t); } cout << res; }
第二题
思路:动态规划,第i个数大于第j个数,则dp[i] = dp[i] + dp[j]
#include<iostream> #include<vector> using namespace std; typedef long long LL; const int maxn = 1e3 + 10; int q[maxn], dp[maxn]; int main() { int n, ans = 0; int MOD = 1e9 + 10; cin >> n; for(int i = 0; i < n; i ++) cin >> q[i]; for(int i = 0; i < n; i ++) dp[i] = 1; for(int i = 1; i < n; i ++) { for(int j = 0; j < i; j ++) { if(q[i] > q[j]) dp[i] = (dp[i] + dp[j]) % MOD; } } for(int i = 0; i < n; i ++) ans = (ans + dp[i]) % MOD; cout << ans; }
第三题
思路:前缀和, 超过k个字符跳出循环
#include<iostream> #include<vector> using namespace std; typedef long long LL; const int maxn = 1e5 + 10; int v[maxn]; int main() { int n, k; LL ans = 0; cin >> n >> k; char ch; cin >> ch; string s; cin >> s; for(int i = 0; i < n; i ++) if(s[i] == ch) v[i] = 1; for(int i = 1; i < n; i ++) v[i] += v[i - 1]; for(int l = 0; l < n; l ++) { for(int r = l; r < n; r ++) { if(l == 0) { if(v[r] == k) ans ++; if(v[r] > k) break; } else { if(v[r] - v[l - 1] == k) ans ++; if(v[r] - v[l - 1] > k) break; } } } cout << ans; }#实习#