Codeforces-33C. Wonderful Randomized Sum
阿新 • • 發佈:2018-02-14
std 相反數 code ont amp cstring 得到 無需 pos
傳送門
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; constint 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; }
Codeforces-33C. Wonderful Randomized Sum