A. Modular Exponentiation
$2^n$很大的时候直接输出$m$, 不然就把$2^n$算出来。
B. Christmas Spruce
没什么可说的。
C. Party Lemonade
感觉有点厉害。如果$2c_{i-1}<c_i$ ,那么咱与其买$i$,不如买两个$i-1$。这样循环一遍,更新所有的$c_i$。
然后在从$0$到$31$循环,相当于枚举$l$的二进制位。如果二进制位为$1$就买,如果之前买的能用当前的替换,就把$ans$重新赋值为$c_i$。
1 #include<map> 2 #include<cstdio> 3 #include<iostream> 4 using namespace std; 5 typedef long long ll; 6 int n, m; 7 ll ans, a[32]; 8 inline void gmin(ll &x, ll y) { 9 if (x > y) x = y; 10 } 11 int main() { 12 cin >> n >> m; 13 for (int i = 0; i < n; ++i) 14 cin >> a[i]; 15 for (int i = 1; i < n; ++i) 16 a[i] = min(a[i], 2 * a[i-1]); 17 for (int i = n; i < 32; ++i) 18 a[i] = a[i-1] * 2; 19 for (int i = 0; i < 32; ++i) { 20 if (ans > a[i]) ans = a[i]; 21 if (m & (1 << i)) ans += a[i]; 22 } 23 printf("%lld\n", ans); 24 return 0; 25 }
D. Too Easy Problems
二分答案。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n, T; 6 struct P { 7 int a, t, id; 8 inline void init(int i) { 9 cin >> a >> t; id = i; 10 } 11 inline bool operator<(const P &p) const { 12 return t < p.t; 13 } 14 } a[200005]; 15 bool check(int k) { 16 int sum = 0, cnt = 0; 17 for (int i = 1; i <= n && cnt < k; ++i) { 18 if (a[i].a >= k) { 19 sum += a[i].t; ++cnt; 20 } 21 } return sum <= T && cnt == k; 22 } 23 inline void pt(int k) { 24 int cnt = 0; 25 for (int i = 1; i <= n && cnt < k; ++i) 26 if (a[i].a >= k) printf("%d ", a[i].id), ++cnt; 27 } 28 int main() { 29 cin >> n >> T; 30 for (int i = 1; i <= n; ++i) a[i].init(i); 31 sort(a + 1, a + 1 + n); 32 int l = 0, r = n; 33 while (l + 1 < r) { 34 int m = (l + r) >> 1; 35 if (check(m)) l = m; else r = m; 36 } 37 while (check(l+1)) ++l; 38 printf("%d\n", l); 39 printf("%d\n", l); 40 pt(l); 41 return 0; 42 }
原文地址:https://www.cnblogs.com/p0ny/p/8253477.html
时间: 2024-10-08 02:44:10