Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)

题目链接:https://codeforces.com/contest/1119/problem/E

题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 

题解:dp[i]表示前 i 种棍子可以组成的最大三角形数量,f[i]表示没有用到的棍子数量,三角形的形状只有两种(2 ^ i, 2 ^ i, 2 ^ i)或者(2 ^ i, 2 ^ i, 2 ^ j),显然先用之前剩下的来组三角形最优,然后就可以转移了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define mst(a,b) memset((a),(b),sizeof(a))
 6 #define mp(a,b) make_pair(a,b)
 7 #define pi acos(-1)
 8 #define pii pair<int,int>
 9 #define pb push_back
10 #define lowbit(x) ((x)&(-x))
11 const int INF = 0x3f3f3f3f;
12 const double eps = 1e-6;
13 const int maxn = 3e5 + 10;
14 const int maxm = 1e6 + 10;
15 const ll mod =  998244353;
16 
17 
18 int main() {
19 #ifdef local
20     freopen("data.txt", "r", stdin);
21 //    freopen("data.txt", "w", stdout);
22 #endif
23     int n;
24     cin >> n;
25     ll res = 0, ans = 0;
26     for(int i = 0; i < n; i++) {
27         ll a;
28         cin >> a;
29         ll mn = min(res, a / 2);
30         res -= mn, a -= 2 * mn;
31         ans += mn + a / 3;
32         a %= 3;
33         res += a;
34     }
35     cout << ans << endl;
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/scaulok/p/10663389.html
今日推荐