답안 #131455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131455 2019-07-17T07:24:59 Z someone_aa 수열 (APIO14_sequence) C++17
50 / 100
288 ms 131072 KB
#include <bits/stdc++.h>
#define ll long long 
#define pb push_back
#define mp make_pair
#define x real()
#define y imag()
#define pll pair<ll, ll>
using namespace std;
typedef long long ftype;
typedef complex<ftype> point;
const int maxn = 10100;
ll arr[maxn], n, k;
ll prefix[maxn], suffix[maxn];

ll dot(point a, point b) {
	return (a*conj(b)).x;
}

ll fx(point a, ll d) {
	return dot(a, {d, 1});
}

ll dp[2][maxn];
int dppntr[210][maxn];

class node {
public:
	point f;
	ll lb, rb;
	node *lchild, *rchild;
	int ind;

	node(ll _lb, ll _rb) {
		lb = _lb;
		rb = _rb;
		lchild = rchild = NULL;
		f = {0, INT_MIN};
		ind = 0;
	}
	void update(point nw, int _ind) {
		ll mid = (lb + rb) / 2;

		// SOMETHING NOT OKAY HERE
		bool lft = fx(nw, lb) > fx(f, lb);
		bool md = fx(nw, mid) > fx(f, mid);

		if(md) {
			swap(nw, f);
			swap(ind, _ind);
		}

		if(lb == rb) return;

		if(lft == md) {
			// they don't intersect in left, they might intersect in right part
			if(rchild == NULL) rchild = new node(mid+1, rb);
			rchild -> update(nw, _ind);
		}
		else {
			// they intersect in left part, continue searching in left
			if(lchild == NULL) lchild = new node(lb, mid);
			lchild -> update(nw, _ind);
		}
	}
	pll query(ll p) {
		ll mid = (lb + rb) / 2;

		if(lb == rb) return {fx(f, p), ind};
		else {
			if(p <= mid) {
				pll val = {INT_MIN, INT_MIN};
				if(lchild != NULL) val = lchild -> query(p);
				
				if(fx(f, p) > val.first) return {fx(f, p), ind};
				else return val;
			}
			else {
				pll val = {INT_MIN, INT_MIN};
				if(rchild != NULL) val = rchild -> query(p);
				
				if(fx(f, p) > val.first) return mp(fx(f, p), ind);
				else return val;
			}
		}
	}
};

int main() {
	cin>>n>>k;
	for(int i=1;i<=n;i++) {
		cin>>arr[i];
		prefix[i] = prefix[i-1] + arr[i];
	}

	for(int i=n;i>=1;i--) {
		suffix[i] = suffix[i+1] + arr[i];
	}

	for(int i=1;i<=n;i++) {
		dp[1][i] = prefix[i] * suffix[i+1];
	}

 	for(int p=2;p<=k;p++) {
 		node *root = new node(0, (1LL << 37));
 		for(int i=1;i<=n;i++) {
 			root -> update({-prefix[i-1], dp[(p-1)%2][i-1]}, i-1);

 			if(i < p) {
 				dp[p%2][i] = INT_MIN;
 				continue;
 			} 

 			pll qr = root -> query(suffix[i+1]);

 			dp[p%2][i] = qr.first + prefix[i] * suffix[i+1];
 			dppntr[p][i] = qr.second;
 		
 			//root -> update({-prefix[i-1], dp[p-1][i-1]}, i-1);
 		}

 		/*for(int i=1;i<=n;i++) {
 			for(int lst=i;lst>=1;lst--) {
 				if(dp[p-1][lst-1] + (prefix[i] - prefix[lst-1]) * suffix[i+1] > dp[p][i]) {
 					dp[p][i] = dp[p-1][lst-1] + (prefix[i] - prefix[lst-1]) * suffix[i+1];
 					dppntr[p][i] = lst-1;
 				}
 			}
 		}*/
 	}
 	
 	ll result = LLONG_MIN;
 	ll lst = 0;
 	for(int i=1;i<n;i++) {
 		if(dp[k%2][i] >= result) {
 			result = dp[k%2][i];
 			lst = i;
 		}
 	}

 	/*for(int p=1;p<=k;p++) {
 		cout<<p<<": ";
 		for(int i=1;i<=n;i++) {
 			cout<<dp[p][i]<<" ";
 		} cout<<"\n";
 	}*/

 	//lst++;
 	//cout<<lst<<"\n";

 	vector<int>v;
 	int p = k;
 	while(lst > 0) {
 		v.pb(lst);
 		lst = dppntr[p][lst];
 		p--;
 	}

 	//reverse(v.begin(), v.end());

 	cout<<result<<"\n";
 	for(int i:v) {
 		cout<<i<<" ";
 	} cout<<"\n";

 	//cout<<"KRAJ: "<<v.size()<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 108 == 108
2 Correct 2 ms 380 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 376 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 508 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 376 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 4 ms 1400 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 504 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 504 KB contestant found the optimal answer: 933702 == 933702
7 Correct 3 ms 888 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 3 ms 760 KB contestant found the optimal answer: 687136 == 687136
9 Correct 3 ms 504 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 3 ms 760 KB contestant found the optimal answer: 29000419931 == 29000419931
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 2 ms 376 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 27 ms 12316 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 504 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 19 ms 4236 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 19 ms 5368 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 26 ms 11640 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 10 ms 3448 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 7 ms 2808 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 12 ms 4984 KB contestant found the optimal answer: 490686959791 == 490686959791
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 760 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 3 ms 376 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 187 ms 91864 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 4 ms 1016 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 132 ms 22748 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 117 ms 22392 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 184 ms 85532 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 155 ms 59000 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 40 ms 17656 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 77 ms 35764 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3576 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 11 ms 760 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Runtime error 288 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -