Submission #23653

# Submission time Handle Problem Language Result Execution time Memory
23653 2017-05-17T18:49:01 Z SYury Split the sequence (APIO14_sequence) C++11
100 / 100
1839 ms 87340 KB
#include<bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") 

using namespace std;
typedef long long lint;
typedef long double ldb;
typedef unsigned long long uli;

#define X first
#define Y second
#define F(i, l, r) for(auto i = l; i != r; i++)
#define Df(i, l, r) for(auto i = l; i != r; i--)
#define I(i, a) for(auto i : a)
#define pb push_back
#define rs resize
#define mk make_pair
#define asg assign
#define all(x) x.begin(),x.end()
#define ret return
#define cont continue
#define brk break
#define ins insert
#define era erase
#define fi0(x) memset(x, 0, sizeof(x))
#define finf(x) memset(x, 127, sizeof(x))
#define acpy(y, x) memcpy(y, x, sizeof(y))
#define y1 adjf
#define tm dhgdg

const int MAXN = 1e5 + 3;
const int MAXK = 2e2 + 2;
const ldb eps = 1e-16;

int pr[MAXN][MAXK];
int a[MAXN];
int pref[MAXN];
lint sqp[MAXN];
int n, k;
lint lk[2][MAXN];
lint lb[2][MAXN];
int lid[2][MAXN];
int sz[2];
int ptr[2];

lint KEK;

lint f(int ch, int i, int x){
	ret lk[ch][i] * x + lb[ch][i];
}

void add_line(int ch, lint ck, lint cb, int cid){	
		if(sz[ch] > 0 && lk[ch][sz[ch] - 1] == ck && lb[ch][sz[ch] - 1] < cb){sz[ch]--;}
		if(sz[ch] > 0 && lk[ch][sz[ch] - 1] == ck && lb[ch][sz[ch] - 1] >= cb){
			ptr[ch] = min(ptr[ch], sz[ch] - 1);
			ret;
		}
		while(sz[ch] >= 2){
			int p1 = sz[ch] - 1, p2 = sz[ch] - 2;
			ldb f2 = (lb[ch][p2] - lb[ch][p1])/((ldb)lk[ch][p1] - lk[ch][p2]), f1 = (lb[ch][p1] - cb)/((ldb)ck - lk[ch][p1]);
			if(f2 < f1 - eps)brk;
			sz[ch]--;
		}
		lk[ch][sz[ch]] = ck;
		lb[ch][sz[ch]] = cb;	
		lid[ch][sz[ch]] = cid;
		sz[ch]++;
		ptr[ch] = min(ptr[ch], sz[ch] - 1);
}

int get(int ch, int x, int cid){
	if(sz[ch] == 0)ret -1;
	ptr[ch] = min(ptr[ch], sz[ch] - 1);
	while(ptr[ch] < sz[ch] - 1 && lid[ch][ptr[ch] + 1] < cid && f(ch, ptr[ch], x) <= f(ch, ptr[ch] + 1, x))ptr[ch]++;
	if(lid[ch][ptr[ch]] >= cid)ret -1;
	KEK = f(ch, ptr[ch], x);
	ret lid[ch][ptr[ch]];
}

void ch_clear(int ch){
	sz[ch] = 0; ptr[ch] = 0;
}

void restore(int i, int j){
	while(i >= 0){
		printf("%d ", i + 1);
		i = pr[i][j]; j--;
	}
}

lint dcp[MAXN];

int main(){
//	freopen("input.txt", "r", stdin);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	scanf("%d%d", &n, &k);
	F(i, 0, n)scanf("%d", &a[i]);
	F(i, 0, n)pref[i] = a[i] + ((i == 0) ? 0 : pref[i - 1]);
	k++;
	F(i, 0, n)sqp[i] = pref[i] * 1ll * pref[i];
	F(i, 0, n)
		F(j, 1, k + 1)
			pr[i][j] = -2;
	F(i, 0, n)pr[i][1] = -1;
	F(i, 0, n)add_line(0, pref[i], -sqp[i], i);
	int tc = 1;
	F(j, 2, k + 1){
		ch_clear(tc);
		F(i, j - 1, n){
			int tmp = get(1 - tc, pref[i], i);
			if(tmp == -1){cont;}
			dcp[i] = KEK;
			pr[i][j] = tmp;
			add_line(tc, pref[i], dcp[i] - sqp[i], i);
		}
		tc = 1 - tc;
	}
	printf("%lld\n", dcp[n - 1]);
	restore(pr[n - 1][k], k - 1);
	ret 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:98:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
sequence.cpp:99:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  F(i, 0, n)scanf("%d", &a[i]);
                              ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 87340 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 87340 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 87340 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 87340 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 87340 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 87340 KB contestant found the optimal answer: 1 == 1
7 Correct 0 ms 87340 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 87340 KB contestant found the optimal answer: 1 == 1
9 Correct 0 ms 87340 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 87340 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 87340 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 0 ms 87340 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 0 ms 87340 KB contestant found the optimal answer: 140072 == 140072
14 Correct 0 ms 87340 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 0 ms 87340 KB contestant found the optimal answer: 805 == 805
16 Correct 0 ms 87340 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 0 ms 87340 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 0 ms 87340 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 0 ms 87340 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 0 ms 87340 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 0 ms 87340 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 0 ms 87340 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 0 ms 87340 KB contestant found the optimal answer: 933702 == 933702
7 Correct 0 ms 87340 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 0 ms 87340 KB contestant found the optimal answer: 687136 == 687136
9 Correct 0 ms 87340 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 0 ms 87340 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 0 ms 87340 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 0 ms 87340 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 0 ms 87340 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 0 ms 87340 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 0 ms 87340 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 0 ms 87340 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 0 ms 87340 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 0 ms 87340 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 0 ms 87340 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 0 ms 87340 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 0 ms 87340 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 0 ms 87340 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 3 ms 87340 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 0 ms 87340 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 3 ms 87340 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 3 ms 87340 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 3 ms 87340 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 3 ms 87340 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 0 ms 87340 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 3 ms 87340 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 3 ms 87340 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 3 ms 87340 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 83 ms 87340 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 3 ms 87340 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 43 ms 87340 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 56 ms 87340 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 59 ms 87340 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 43 ms 87340 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 53 ms 87340 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 69 ms 87340 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 53 ms 87340 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 56 ms 87340 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1599 ms 87340 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 33 ms 87340 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1266 ms 87340 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 1839 ms 87340 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1109 ms 87340 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 549 ms 87340 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 766 ms 87340 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 916 ms 87340 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845