Balanced Stone Heaps(1600 binary search + greedy)

 1 /**\
 2 https://codeforces.com/problemset/problem/1623/C
 3 核心代码:运过来的石子够mid个,将a[i]全部甩过去,不然就把多出来的分到后面
 4 int add = min(a[i], aa[i] - x) / 3;
 5 \**/
 6 #include <bits/stdc++.h>
 7 using namespace std;
 8 #define fi first
 9 #define se second
10 #define go continue
11 #define int long long
12 #define PII pair<int, int>
13 #define sf(x) scanf("%lld",&x)
14 #define ytz int _; sf(_); while(_--)
15 #define fory(i,a,b) for(int i = a; i <= b; ++i)
16 #define forl(i,a,b) for(int i = a; i >= b; --i)
17 #define debug(a) cout << #a << " = " << a <<endl;
18 const int N = 2e5 + 10;
19 int n, a[N], aa[N];
20 bool check(int x)
21 {
22     fory(i, 1, n) aa[i] = a[i];
23     for(int i = n; i >= 3; --i)
24     {
25         if(aa[i] < x) return false;
26 
27         int add = min(a[i], aa[i] - x) / 3; 
28         aa[i - 1] += add;
29         aa[i - 2] += add * 2;
30     }
31     if(aa[1] < x || aa[2] < x) return false;
32     return true;
33 }
34 signed main()
35 {
36     ytz
37     {
38         sf(n);
39         fory(i, 1, n) sf(a[i]);
40         int l = 1, r = 1e9;
41         while(l < r)
42         {
43             int mid = (l + r + 1) >> 1;
44             if(check(mid)) l = mid;
45             else r = mid - 1;
46         }
47         printf("%lld\n", l);
48     }
49     return 0;
50 }

 

posted @ 2022-03-04 09:49  std&ice  阅读(64)  评论(0编辑  收藏  举报