hdoj薛猫猫杯程序设计网络赛1003 球球大作战
Posted wangyiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdoj薛猫猫杯程序设计网络赛1003 球球大作战相关的知识,希望对你有一定的参考价值。
思路:
二分,check函数不是很好写。
实现:
1.
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll INF = 0x3f3f3f3f3f3f3f3f; 5 ll a[100005]; 6 int n, k; 7 bool check(ll x) 8 { 9 int cnt = 0; 10 ll sum = 0; 11 for (int i = 1; i <= n; i++) 12 { 13 if (a[i] >= x) continue; 14 sum = a[i++]; 15 while (i <= n && sum < x) 16 { 17 sum += a[i++]; cnt++; 18 } 19 if (i > n && sum < x) cnt++; 20 i--; 21 } 22 return cnt <= k; 23 } 24 int main() 25 { 26 int T; 27 cin >> T; 28 while (T--) 29 { 30 ll l = INF, r; 31 cin >> n >> k; 32 for (int i = 1; i <= n; i++) 33 { 34 cin >> a[i]; 35 l = min(l, a[i]); 36 r += a[i]; 37 } 38 ll ans = l; 39 while (l <= r) 40 { 41 ll m = l + r >> 1; 42 if (check(m)) 43 { 44 ans = m; 45 l = m + 1; 46 } 47 else r = m - 1; 48 } 49 cout << ans << endl; 50 } 51 return 0; 52 }
2.
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll INF = 0x3f3f3f3f3f3f3f3f; 5 ll a[100005], b[100005]; 6 int n, k; 7 bool check(ll x) 8 { 9 for (int i = 1; i <= n; i++) b[i] = a[i]; 10 int cnt = 0; 11 for (int i = 1; i <= n; i++) 12 { 13 if (b[i] >= x) continue; 14 b[i + 1] += b[i]; 15 cnt++; 16 } 17 return cnt <= k; 18 } 19 int main() 20 { 21 int T; 22 cin >> T; 23 while (T--) 24 { 25 ll l = INF, r; 26 cin >> n >> k; 27 for (int i = 1; i <= n; i++) 28 { 29 cin >> a[i]; 30 l = min(l, a[i]); 31 r += a[i]; 32 } 33 ll ans = l; 34 while (l <= r) 35 { 36 ll m = l + r >> 1; 37 if (check(m)) 38 { 39 ans = m; 40 l = m + 1; 41 } 42 else r = m - 1; 43 } 44 cout << ans << endl; 45 } 46 return 0; 47 }
以上是关于hdoj薛猫猫杯程序设计网络赛1003 球球大作战的主要内容,如果未能解决你的问题,请参考以下文章