Codeforces-33C. Wonderful Randomized Sum
生活随笔
收集整理的這篇文章主要介紹了
Codeforces-33C. Wonderful Randomized Sum
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
?
N個數,允許將前連續任意個數變化為其相反數,也允許把后連續任意個數變為相反數,求最大和
令dp[i][0]前i個數操作后能得到的最大值,dp[i][1]出去前i-1個數操作后能得到的最大值
注意初始化,不然對于答案為無需操作的情況會出錯
?
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; typedef long long LL;const int maxn = 1e5 + 10; int A[maxn]; int dp[maxn][2]; int sum[maxn];int N;int main() {scanf("%d", &N);for (int i = 1; i <= N; i++) {scanf("%d", &A[i]);sum[i] = sum[i - 1] + A[i];}int tmp = -INF;for (int i = 1; i <= N; i++) {tmp = max(tmp, -2 * sum[i]);dp[i][0] = sum[i] + tmp;}tmp = -INF;for (int i = N; i > 0; i--) {tmp = max(tmp, -2 * (sum[N] - sum[i - 1]));dp[i][1] = (sum[N] - sum[i - 1]) + tmp;}int ans = sum[N];// for (int i = 0; i <= N; i++) {ans = max(ans, dp[i][0] + dp[i + 1][1]);}printf("%d\n", ans);return 0; }?
轉載于:https://www.cnblogs.com/xFANx/p/8448187.html
總結
以上是生活随笔為你收集整理的Codeforces-33C. Wonderful Randomized Sum的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Web Pages:Ch
- 下一篇: thinkphp模版常量替换机制