Submission #33585

# Submission time Handle Problem Language Result Execution time Memory
33585 2017-10-30T09:54:29 Z sinhriv Split the sequence (APIO14_sequence) C++14
71 / 100
1633 ms 31304 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 10010;
const int K = 220;

int n, k;
int arr[N];
int pre[N][K];
long long f[N][K];

template < class T > 
struct LineIT{
	/// Lim 
	static const T inf = -2e9;
	static const T Worst = -1e18;
 
	struct Line{
		T a;
		T b;
		int idx;
 
		T get(T x){
			if(a < inf) return Worst; 
			return a * x + b;
		}
	};
 
 
	T low, high;
 
	int root, cnt;
	vector < int > L;
	vector < int > R;	
	vector < Line > it;
 
	Line Create(T u, T v, int i){
		Line ret;
		ret.a = u;
		ret.b = v;
		ret.idx = i;
		return ret;
	}
 
	LineIT(T from, T to){
		low = from;
		high = to;
		L.resize(N * 10 + 10, 0);
		R.resize(N * 10 + 10, 0);
		it.resize(N * 10 + 10, Create(inf - 1, inf - 1, 0));
		root = cnt = 1;
	}

	void re(){
		fill(L.begin(), L.end(), 0);
		fill(R.begin(), R.end(), 0);
		fill(it.begin(), it.end(), Create(inf - 1, inf - 1, 0));
		cnt = 1;
	}
 
	T lef, rig;
 
 
	void upd(int &x, T l, T r, Line val){
		if(l > rig || r < lef) return;
 
 
		if(x == 0) x = ++cnt;
 
		T mid  = ((l + r) >> 1);
 
 
 		if(l >= lef && r <= rig){
			if(it[x].get(l) >= val.get(l) && it[x].get(r) >= val.get(r)){
				return;
			}
			
			if(it[x].get(l) <= val.get(l) && it[x].get(r) <= val.get(r)){
				it[x] = val;
				return;
			}
	 
			if(it[x].get(l) <= val.get(l) && it[x].get(mid) <= val.get(mid)){
				upd(R[x], mid + 1, r, it[x]);
				it[x] = val;
				return;
			}
	 
			if(it[x].get(l) >= val.get(l) && it[x].get(mid) >= val.get(mid)){
				upd(R[x], mid + 1, r, val);
				return;
			}
	 
			if(it[x].get(mid + 1) <= val.get(mid + 1) && it[x].get(r) <= val.get(r)){
				upd(L[x], l, mid, it[x]);
				it[x] = val;
				return;
			}
	 
			if(it[x].get(mid + 1) >= val.get(mid + 1) && it[x].get(r) >= val.get(r)){
				upd(L[x], l, mid, val);
				return;
			}
		}
		upd(L[x], l, mid, val);
		upd(R[x], mid + 1, r, val);
	}

	Line M(Line x, Line y, T val){
		if(x.get(val) > y.get(val)) return x;
		return y;
	}
 
	Line query(int x, int l, int r, T val){
		if(l > val || r < val || x == 0) return Create(inf - 1, inf - 1, 0);
 
		if(l == r) return it[x];
		T mid  = ((l + r) >> 1);
 
		Line ans = it[x];
 
 
		ans = M(ans, query(L[x], l, mid, val), val);
		ans = M(ans, query(R[x], mid + 1, r, val), val);
 
		return ans;
	}
 
	void update(T a, T b, int idx, pair < T, T > seg = make_pair(-inf - 1, -inf - 1)){
		if(seg.first != -inf - 1){
			lef = seg.first;
			rig = seg.second;
		}
		else{
			lef = low;
			rig = high;
		}
		upd(root, low, high, Create(a, b, idx));
	}
 
	pair < T, int > ask(T x){
		Line p = query(root, low, high, x);
		return make_pair(p.get(x), p.idx);
	}
};

long long solve(vector < int > a){
	LineIT < long long > tree(0, 1e9);

	vector < long long > sum(a.size());
	partial_sum(a.begin(), a.end(), sum.begin());

	long long ans = 0;

	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= k; ++j){
			f[i][j] = -1e9;
		}
	}
	f[0][0] = 0;

	for(int j = 1; j <= k; ++j){
		tree.re();
		for(int i = j; i <= n; ++i){
			
			tree.update(sum[i - 1], -sum[i - 1] * sum[n] + f[i - 1][j - 1], i - 1);
			auto p = tree.ask(sum[i]);
			pre[i][j] = p.second;
			f[i][j] = p.first + sum[i] * sum[n] - sum[i] * sum[i];
			if(j == k) ans = max(ans, f[i][j]);
		}
	}
	return ans;
}


int main(){
	if(fopen("1.inp", "r")){
		freopen("1.inp", "r", stdin);
	}

	scanf("%d%d", &n, &k);
	vector < int > x(1, 0);

	for(int i = 1; i <= n; ++i){
		scanf("%d", arr + i);
		x.push_back(arr[i]);
	}
	vector < int > lst;

	long long ans = solve(x);
	int pos = 0;
	for(int i = 1; i < n; ++i){
		if(f[i][k] == ans){
			pos = i;
		}
	}
	for(int j = k; j >= 1; --j){
		lst.push_back(pos);
		pos = pre[pos][j];
	}
	reverse(lst.begin(), lst.end());

	printf("%lld\n", ans);

	for(int x : lst){
		printf("%d ", x);
	}



	return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:180:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("1.inp", "r", stdin);
                               ^
sequence.cpp:183: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:187:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", arr + i);
                       ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 31020 KB contestant found the optimal answer: 108 == 108
2 Correct 3 ms 31020 KB contestant found the optimal answer: 999 == 999
3 Correct 3 ms 31020 KB contestant found the optimal answer: 0 == 0
4 Correct 3 ms 31020 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 3 ms 31020 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 3 ms 31020 KB contestant found the optimal answer: 1 == 1
7 Correct 0 ms 31020 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 31020 KB contestant found the optimal answer: 1 == 1
9 Correct 0 ms 31020 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 3 ms 31020 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 31020 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 3 ms 31020 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 3 ms 31020 KB contestant found the optimal answer: 140072 == 140072
14 Correct 3 ms 31020 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 3 ms 31020 KB contestant found the optimal answer: 805 == 805
16 Correct 0 ms 31020 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 0 ms 31020 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 3 ms 31020 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 3 ms 31020 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 16 ms 31020 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 0 ms 31020 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 3 ms 31020 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 3 ms 31020 KB contestant found the optimal answer: 933702 == 933702
7 Correct 9 ms 31020 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 3 ms 31020 KB contestant found the optimal answer: 687136 == 687136
9 Correct 0 ms 31020 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 3 ms 31020 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 3 ms 31020 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 3 ms 31020 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 73 ms 31020 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 0 ms 31020 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 59 ms 31020 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 73 ms 31020 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 73 ms 31020 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 23 ms 31020 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 13 ms 31020 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 19 ms 31020 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31020 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 6 ms 31020 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 179 ms 31020 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 6 ms 31020 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 229 ms 31020 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 199 ms 31020 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 179 ms 31020 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 209 ms 31020 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 43 ms 31020 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 69 ms 31020 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 43 ms 31304 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 39 ms 31304 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 1393 ms 31304 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 19 ms 31304 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 1459 ms 31304 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 1633 ms 31304 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 1359 ms 31304 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 1366 ms 31304 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 1269 ms 31304 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 1626 ms 31304 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 31304 KB declared answer doesn't correspond to the split scheme: declared = 96, real = 2757000
2 Halted 0 ms 0 KB -