結果

問題 No.1117 数列分割
ユーザー akun0716akun0716
提出日時 2020-07-20 20:32:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 3,000 ms
コード長 2,371 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 90,116 KB
実行使用メモリ 79,456 KB
最終ジャッジ日時 2023-08-25 23:51:09
合計ジャッジ時間 6,351 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
74,184 KB
testcase_01 AC 22 ms
74,208 KB
testcase_02 AC 22 ms
74,296 KB
testcase_03 AC 40 ms
75,180 KB
testcase_04 AC 43 ms
77,952 KB
testcase_05 AC 23 ms
74,140 KB
testcase_06 AC 23 ms
74,372 KB
testcase_07 AC 23 ms
74,228 KB
testcase_08 AC 37 ms
74,992 KB
testcase_09 AC 26 ms
74,628 KB
testcase_10 AC 36 ms
74,808 KB
testcase_11 AC 82 ms
75,728 KB
testcase_12 AC 86 ms
75,876 KB
testcase_13 AC 117 ms
76,392 KB
testcase_14 AC 117 ms
76,408 KB
testcase_15 AC 152 ms
76,764 KB
testcase_16 AC 159 ms
76,444 KB
testcase_17 AC 37 ms
74,660 KB
testcase_18 AC 445 ms
79,456 KB
testcase_19 AC 336 ms
78,340 KB
testcase_20 AC 133 ms
76,408 KB
testcase_21 AC 329 ms
78,400 KB
testcase_22 AC 323 ms
78,160 KB
testcase_23 AC 321 ms
78,028 KB
testcase_24 AC 314 ms
78,020 KB
testcase_25 AC 312 ms
78,260 KB
testcase_26 AC 22 ms
74,272 KB
testcase_27 AC 32 ms
74,380 KB
testcase_28 AC 33 ms
74,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

int dp[3010][3010];

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	REP(i, 3010)REP(j, 3010)dp[i][j] = -INF;
	int N, K, M; cin >> N >> K >> M;
	VI A(N);
	REP(i, N)cin >> A[i];
	dp[0][0] = 0;
	vector<deque<pii> > dq1(K + 1), dq2(K + 1);
	dq1[0].emplace_back(pii(0, 0));
	dq2[0].emplace_back(pii(0, 0));
	VI sum(N + 1, 0);
	REP(i, N)sum[i + 1] = sum[i] + A[i];
	REP(i, N) {
		eFOR(j, 1, K) {
			int mx1 = -INF, mx2 = -INF;
			while (!dq1[j - 1].empty() && dq1[j - 1].front().first < i + 1 - M)
				dq1[j - 1].pop_front();

			while (!dq2[j - 1].empty() && dq2[j - 1].front().first < i + 1 - M)
				dq2[j - 1].pop_front();

			if (!dq1[j - 1].empty())mx1 = dq1[j - 1].front().second;
			if (!dq2[j - 1].empty())mx2 = dq2[j - 1].front().second;

			chmax(dp[i + 1][j], mx1 - sum[i + 1]);
			chmax(dp[i + 1][j], mx2 + sum[i + 1]);
		}
		eREP(j, K) {
			while (!dq1[j].empty() && dq1[j].back().second < dp[i + 1][j] + sum[i + 1])
				dq1[j].pop_back();

			while (!dq2[j].empty() && dq2[j].back().second < dp[i + 1][j] - sum[i + 1])
				dq2[j].pop_back();

			dq1[j].emplace_back(pii(i + 1, dp[i + 1][j] + sum[i + 1]));
			dq2[j].emplace_back(pii(i + 1, dp[i + 1][j] - sum[i + 1]));
		}
	}
	   	  
	cout << dp[N][K] << endl;

	return 0;
}

0