Codeforces 855 B Marvolo Gaunt's Ring(dp)
2017-10-25 07:46
489 查看
题目地址
题意:给你4个数n,p,q,r,然后告诉你n个数a[],让你求出满足最大的 p·ai + q·aj + r·ak 的i,j,k,并且i<=j<=k(重点)
思路:我们可以发现j在中间,我们只要维护一个前缀最大值和前缀最小值以及后缀最大值和后缀最小值,这样的话我们枚举这个j就好了,然后判断是正还是负,选取要最大值还是最小值,最后求出这个的最大值就好了。
题意:给你4个数n,p,q,r,然后告诉你n个数a[],让你求出满足最大的 p·ai + q·aj + r·ak 的i,j,k,并且i<=j<=k(重点)
思路:我们可以发现j在中间,我们只要维护一个前缀最大值和前缀最小值以及后缀最大值和后缀最小值,这样的话我们枚举这个j就好了,然后判断是正还是负,选取要最大值还是最小值,最后求出这个的最大值就好了。
#include <iostream> #include <cstring> #include <string> #include <queue> #include <vector> #include <map> #include <set> #include <cmath> #include <cstdio> #include <algorithm> #include <iomanip> #define N 100010 #define M 2000010//双倍 #define LL __int64 #define inf 0x3f3f3f3f3f3f3f3f #define lson l,mid,ans<<1 #define rson mid+1,r,ans<<1|1 #define getMid (l+r)>>1 #define movel ans<<1 #define mover ans<<1|1 using namespace std; const LL mod = 1000000007; const double eps = 0.001; LL n, a, b, c, num , sum; LL nummin , nummax , premin , premax ; int main() { cin.sync_with_stdio(false); while (cin >> n >> a >> b >> c) { nummax[0] = -1e18; nummin[0] = 1e18; premax[n + 1] = -1e18; premin[n + 1] = 1e18; for (int i = 1; i <= n; i++) { cin >> num[i]; nummax[i] = max(nummax[i - 1], num[i]); nummin[i] = min(nummin[i - 1], num[i]); } for (int i = n; i >= 1; i--) { premax[i] = max(premax[i + 1], num[i]); premin[i] = min(premin[i + 1], num[i]); } sum = -4e18; for (int i = 1; i <= n; i++) { LL ans = 0; if (a >= 0) { ans += a*nummax[i]; } else { ans += a*nummin[i]; } if (c >= 0) { ans += c*premax[i]; } else { ans += c*premin[i]; } sum = max(sum, num[i] * b + ans); } cout << sum << endl; } return 0; }
相关文章推荐
- Codeforces-855B - Marvolo Gaunt's Ring - DP
- Codeforces 855 B Marvolo Gaunt's Ring
- codeforces Manthan, Codefest 17 B.Marvolo Gaunt's Ring (简单dp)
- codeforces Manthan, Codefest 17 B Marvolo Gaunt's Ring(dp)
- Manthan, Codefest 17 B. Marvolo Gaunt's Ring(前后缀/dp)
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
- Manthan, Codefest 17 - B - Marvolo Gaunt's Ring【动态规划水题】
- Codeforces 855 C. Helga Hufflepuff’s Cup (树形dp)
- Manthan, Codefest 17: B. Marvolo Gaunt's Ring
- codeforces contest 855 problem C(树形DP)
- Manthan, Codefest 17 Marvolo Gaunt's Ring
- Codeforces 340E Iahub and Permutations【思维+错排Dp】
- codeforces 666A (DP)
- codeforces 148E Porcelain(DP, 分组背包)
- Codeforces 830D [DP]
- [CodeForces - 712D]Memory and Scores (DP 或者 生成函数)
- codeforces 777C. Alyona and Spreadsheet (DP)
- codeforces 601C (概率DP)
- CodeForces 238C World Eater Brothers 树形dp
- codeforces 940E Cashback 有趣的dp