CODE FESTIVAL 2016 Final

Submission #994428

Source codeソースコード

#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>

using namespace std;

#define MOD @
#define ADD(X,Y) ((X) = ((X) + (Y)) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

int N, A[202020], M, X[202020];
i64 dp[202020];
i64 S[202020];

int uf[4040404];

int root(int p)
{
	return uf[p] < 0 ? p : (uf[p] = root(uf[p]));
}
void join(int p, int q)
{
	p = root(p);
	q = root(q);
	if (p == q) return;

	if (p < q) swap(p, q);
	uf[p] += uf[q];
	uf[q] = p;
}

const int OFS = 2010101;

void precalc()
{
	S[0] = 0;
	for (int i = 1; i < N; ++i) S[i] = S[i - 1] + A[i - 1];
	dp[N - 1] = 0;
//	i64 acc = A[N - 1] - dp[N - 1] - S[N - 1];
	for (int i = 0; i <= 2 * OFS; ++i) uf[i] = -1;

	vector<int> unres;
	for (int i = OFS; i >= -OFS; --i) unres.push_back(i);

	for (int i = N - 2; i >= 2; --i) {
		// acc = max(acc, A[i] - (acc + S[i + 1]) - S[i])
		int app = A[i] - S[i + 1] - S[i];
		while (!unres.empty()) {
			int v = unres[unres.size() - 1];
			if (v < app - v) {
				join(v + OFS, app - v + OFS);
				unres.pop_back();
			} else {
				break;
			}
		}

	}
}

i64 solve()
{
	i64 acc = A[N - 1] - S[N - 1];
	if (acc > OFS) {
	} else if (acc < -OFS) {
		acc = -acc;
	} else {
		acc = root(acc + OFS) - OFS;
	}
	/*
	for (int i = N - 2; i >= 2; --i) {
		acc = max(acc, A[i] - S[i + 1] - S[i] - acc);
	}
	*/

	for (int i = 1; i >= 1; --i) {
		dp[i] = acc + S[i + 1];
		acc = max(acc, A[i] - dp[i] - S[i]);
	}
	return dp[1] + A[0] - A[1];
}

int main()
{
	scanf("%d", &N);
	for (int i = 0; i < N - 1; ++i) scanf("%d", A + i);
	scanf("%d", &M);
	for (int i = 0; i < M; ++i) scanf("%d", X + i);

	precalc();
	for (int i = 0; i < M; ++i) {
		A[N - 1] = X[i];
		printf("%lld\n", solve());
	}
	return 0;
}

Submission

Task問題 H - Tokaido
User nameユーザ名 semiexp feat. GUMI
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1600
Source lengthソースコード長 1996 Byte
File nameファイル名
Exec time実行時間 148 ms
Memory usageメモリ使用量 35688 KB

Compiler messageコンパイルメッセージ

./Main.cpp: In function ‘int main()’:
./Main.cpp:94:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^
./Main.cpp:95:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for (int i = 0; i < N - 1; ++i) scanf("%d", A + i);
^
./Main.cpp:96:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &M);
^
./Main.cpp:97:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for (int i = 0; i < M; ++i) scanf("%d", X + i);
^

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt
dataset1 700 / 700 sample-01.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt
dataset2 900 / 900 sample-01.txt,sample-02.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,02-01.txt,02-02.txt,02-03.txt,02-04.txt,02-05.txt,02-06.txt,02-07.txt,02-08.txt,02-09.txt,02-10.txt,02-11.txt,02-12.txt,02-13.txt,02-14.txt,02-15.txt,02-16.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 97 ms 34792 KB
01-02.txt AC 65 ms 32488 KB
01-03.txt AC 77 ms 32488 KB
01-04.txt AC 89 ms 33640 KB
01-05.txt AC 97 ms 34792 KB
01-06.txt AC 99 ms 34792 KB
01-07.txt AC 82 ms 33128 KB
01-08.txt AC 98 ms 34792 KB
01-09.txt AC 98 ms 34792 KB
01-10.txt AC 76 ms 32488 KB
01-11.txt AC 80 ms 32872 KB
01-12.txt AC 95 ms 34664 KB
01-13.txt AC 97 ms 34792 KB
01-14.txt AC 97 ms 34792 KB
01-15.txt AC 96 ms 34792 KB
01-16.txt AC 93 ms 34792 KB
01-17.txt AC 88 ms 34792 KB
01-18.txt AC 88 ms 34792 KB
01-19.txt AC 89 ms 34792 KB
02-01.txt AC 86 ms 33000 KB
02-02.txt AC 144 ms 35560 KB
02-03.txt AC 148 ms 35560 KB
02-04.txt AC 145 ms 35560 KB
02-05.txt AC 129 ms 33256 KB
02-06.txt AC 145 ms 35560 KB
02-07.txt AC 145 ms 35688 KB
02-08.txt AC 126 ms 33256 KB
02-09.txt AC 130 ms 33256 KB
02-10.txt AC 136 ms 34408 KB
02-11.txt AC 143 ms 35688 KB
02-12.txt AC 144 ms 35560 KB
02-13.txt AC 146 ms 35560 KB
02-14.txt AC 142 ms 35560 KB
02-15.txt AC 132 ms 35560 KB
02-16.txt AC 133 ms 35560 KB
sample-01.txt AC 77 ms 32616 KB
sample-02.txt AC 77 ms 32488 KB