Submission #126496

# Submission time Handle Problem Language Result Execution time Memory
126496 2019-07-07T23:12:06 Z dragoon Split the sequence (APIO14_sequence) C++14
100 / 100
814 ms 86920 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include <array>
using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
	~debug() { cerr << endl; }
	template<class c> typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
		cerr << boolalpha << i; return *this;
	}

	template<class c, int=0> typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
		return *this << range(begin(i), end(i));
	}

sim, class b dor(pair < b, c > d) {
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
		*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
#else
	sim dor(const c&) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

//typedef int LL;
typedef long long int LL;
//typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL > VPL;
typedef vector<PII > VP;
typedef vector<double> VD;
typedef long double ld;

template<class T=LL>
struct Line {
	int id;
	// y = mx + c
	T m, c;
	// The line starts from start_x.
	static const LL MIN_START_X = 0;
	long double start_x;
	LL Eval(LL x) { return m * x + c; }
	long double Eval(long double x) { return m * x + c; }
	long double Intersect(const Line& other) {
		// mx + c = other.m * x + other.c
		long double num = other.c - c;
		long double den = m - other.m;
		assert(m != other.m);
		return num / den;
	}
};

template<class T=LL>
struct StaticCHT {
	static const int MIN_TYPE = 0;
	static const int MAX_TYPE = 1;
	int type = MAX_TYPE;

	// Stores convex hull lines.
	vector<Line<T>> ch;
	// Used if the query x is increasing.
	// id of the current segment.
	int id = 0;

	void InsertLine(Line<T> cur) {
		int sz = ch.size();
		if (sz) assert(type == MAX_TYPE || (ch[sz - 1].m > cur.m || (ch[sz - 1].m == cur.m && ch[sz - 1].c >= cur.c)));
		if (sz) assert(type == MIN_TYPE || (ch[sz - 1].m < cur.m || (ch[sz - 1].m == cur.m && ch[sz - 1].c <= cur.c)));
		if (sz) {
			if (ch[sz - 1].c == cur.c && ch[sz - 1].m == cur.m) return;
		}
		while (sz > 1) {
			double left_side = (double(ch[sz - 1].c - ch[sz - 2].c)) * (ch[sz - 2].m - cur.m);
			double right_side = (double(ch[sz - 2].m - ch[sz - 1].m)) * (cur.c - ch[sz - 2].c);
			if ((left_side >= right_side)) {
				ch.pop_back();
				sz--;
			}
			else break;
		}
		if (!sz) cur.start_x = Line<T>::MIN_START_X;
		else cur.start_x = ch[sz - 1].Intersect(cur);
		ch.push_back(cur);
	}

	pair<int, T> SlidingEval(T x) {
		// There should be at least one element.
		assert(SZ(ch) > 0);
		// May be ch was updated by the time?
		id = MIN(id, SZ(ch));
		while (id + 1 < SZ(ch) && ((type == MIN_TYPE && ch[id].Eval(x) > ch[id + 1].Eval(x)) ||
			(type == MAX_TYPE && ch[id].Eval(x) < ch[id + 1].Eval(x)))) {
			id++;
		}
		return { ch[id].id, ch[id].Eval(x) };
	}

	pair<int, T> Eval(T x) {
		int lo = 0, hi = SZ(ch) - 1;
		while (lo < hi) {
			int mid = (lo + hi + 1) / 2;
			if (ch[mid].start_x > x) hi = mid - 1;
			else lo = mid;
		}
		return { ch[lo].id, ch[lo].Eval(x) };
	}
};

LL num[100005], sum[100005];
int  n, k;

LL dp[2][100005];
int par[202][100005];

void solve(int ks) {
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &num[i]);
		sum[i] = sum[i - 1] + num[i];
	}

	for (int i = 1; i <= n; i++) dp[1][i] = 0, par[1][i] = -1;
	int u = 1, v = 0;
	for (int i = 2; i <= k + 1; i++) {
		StaticCHT<LL> cht;
		cht.ch.reserve(n);
		for (int j = i; j <= n; j++) {
			cht.InsertLine({j - 1, sum[j - 1], dp[u][j - 1] - S(sum[j - 1])});
			auto ret = cht.SlidingEval(sum[j]);
			dp[v][j] = ret.second;
			par[i][j] = ret.first;
		}
		swap(u, v);
	}
	printf("%lld\n", dp[u][n]);
	VI V; 
	int now = n, cur = k + 1; 
	while (now != -1) {
		V.push_back(now); 
		now = par[cur][now]; 
		cur--;
	}
	for (int i = SZ(V) - 1; i >= 1; i--) {
		printf("%d", V[i]);
		if (i > 1) printf(" ");
	}
	printf("\n");
}

int main()
{
#ifdef LOCAL
	double start_time = clock();
	freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	//	freopen("out.out", "w", stdout);
#endif


	if (0) {
		int T;
		scanf("%d", &T);
		//AIN(T, 1, 25);
		for (int ks = 1; ks <= T; ks++) solve(ks);
	}
	else {
		solve(0);
	}

#ifdef LOCAL
	double end_time = clock();
	fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif

	return 0;
}

Compilation message

sequence.cpp:5:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable:4786)
 
sequence.cpp:6:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable:4996)
 
sequence.cpp: In function 'void solve(int)':
sequence.cpp:178:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:180:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &num[i]);
   ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:223:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &T);
   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 108 == 108
2 Correct 2 ms 376 KB contestant found the optimal answer: 999 == 999
3 Correct 2 ms 376 KB contestant found the optimal answer: 0 == 0
4 Correct 2 ms 248 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 380 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
7 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
8 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
9 Correct 2 ms 376 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 2 ms 376 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 376 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 2 ms 376 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 2 ms 376 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 376 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 376 KB contestant found the optimal answer: 805 == 805
16 Correct 2 ms 376 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 2 ms 376 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 380 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 2 ms 632 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 2 ms 376 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 2 ms 376 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 376 KB contestant found the optimal answer: 933702 == 933702
7 Correct 2 ms 504 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 380 KB contestant found the optimal answer: 687136 == 687136
9 Correct 2 ms 420 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 376 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 2 ms 376 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 3 ms 1404 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 376 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 1144 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 3 ms 1272 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 3 ms 1400 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 2 ms 632 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 504 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 2 ms 636 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 2 ms 504 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 9 ms 2040 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 2 ms 376 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 9 ms 2040 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 9 ms 1784 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 10 ms 2040 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 8 ms 2040 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 888 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 5 ms 1144 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1268 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 4 ms 1148 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 66 ms 9792 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 5 ms 1300 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 51 ms 6264 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 54 ms 7028 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 64 ms 7720 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 47 ms 6548 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 51 ms 7236 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 64 ms 8900 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8644 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 28 ms 8640 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 645 ms 86920 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 30 ms 10260 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 814 ms 86048 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 560 ms 61924 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 632 ms 67648 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 453 ms 56600 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 474 ms 63336 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 626 ms 78972 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845