题意:
给定N<=105的序列,可以选一个区间或者不选,将数字变为f(x)=(1890x+143)mod10007,求序列的最大和
分析:
dp, 考虑dp[i][0/1][0/1]前i项,变还是不变这个数,序列是否已经有数变过,的最大和
然后分类转移就好了
赛后想了个这个胡搞dp过了,其实转化为最大字段和更简单。。
代码:
//
// Created by TaoSama on 2015-11-28
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, a[N];
int dp[N][2][2];
int getMax(int &x, int y) {
x = max(x, y);
}
int f(int x) {
return (1890 * x + 143) % 10007;
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d", &n) == 1) {
for(int i = 1; i <= n; ++i) scanf("%d", a + i);
for(int i = 1; i <= n; ++i) {
dp[i][0][0] = dp[i - 1][0][0] + a[i];
dp[i][0][1] = max(dp[i - 1][0][1], dp[i - 1][1][1]) + a[i];
dp[i][1][1] = max(dp[i - 1][1][1], dp[i - 1][0][0]) + f(a[i]);
}
int ans = max(dp[n][0][0], max(dp[n][0][1], dp[n][1][1]));
printf("%d\n", ans);
}
return 0;
}
本文探讨使用动态规划(DP)解决序列优化问题,通过分析序列最大和的计算方法,提出了一个巧妙的DP解决方案,并解释了如何将其简化为最大字段和问题。提供了详细代码实现,适合算法爱好者和初学者。
&spm=1001.2101.3001.5002&articleId=50089877&d=1&t=3&u=f955807a43874fb99f3ad79322f2f9e6)
287

被折叠的 条评论
为什么被折叠?



