Submission #576772

# Submission time Handle Problem Language Result Execution time Memory
576772 2022-06-13T12:48:39 Z denniskim Split the sequence (APIO14_sequence) C++17
100 / 100
378 ms 9520 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second

ll n, k;
ll a[100010];
ll nu[100010];
ll dp[100010], last[100010], gaet[100010];
ll l, r;
vector<ll> vv, L, R, ans;
ll sum;

struct CHT
{
	struct line_func
	{
		ll A, B;
		ld S;
		ll num;
	};
	
	line_func lin[100010];
	ll tp = 1;
	
	ld crossX(line_func X, line_func Y)
	{
		return (ld)(Y.B - X.B) / (ld)(X.A - Y.A);
	}
	
	void update(ll A, ll B, ll NUM)
	{
		line_func tmp;
		
		tmp.A = A;
		tmp.B = B;
		tmp.num = NUM;
		
		if(tmp.A == lin[tp].A && tp)
		{
			if(tmp.B >= lin[tp].B)
				return;
			
			tp--;
		}
		
		while(tp)
		{	
			tmp.S = crossX(tmp, lin[tp]);
			
			if(tmp.S > lin[tp].S)
				break;
			
			tp--;
		}
		
		lin[++tp] = tmp;
	}
	
	pair<ll, ll> query(ll X)
	{
		ll LL = 1;
		ll RR = tp;
		
		while(LL <= RR)
		{
			ll mid = (LL + RR) / 2;
			
			if(lin[mid].S > X)
				RR = mid - 1;
			else
				LL = mid + 1;
		}
		
		return {lin[RR].A * X + lin[RR].B, lin[RR].num};
	}
}cht;

void init(void)
{
	for(ll i = 1 ; i <= n ; i++)
	{
		dp[i] = 0;
		last[i] = 0;
		gaet[i] = 0;
		cht.lin[i] = {0, 0, 0.0, 0};
	}
	
	cht.tp = 1;
}

void solve(ll lambda2)
{
	init();
	
	for(ll i = 1 ; i <= n ; i++)
	{
		pair<ll, ll> qry = cht.query(nu[i]);
		dp[i] = qry.fi + 2 * nu[i] * nu[i];
		last[i] = qry.se;
		gaet[i] = gaet[last[i]] + 1;
		cht.update(-4 * nu[i], dp[i] + 2 * nu[i] * nu[i] + lambda2, i);
	}
	
	vv.clear();
	
	for(ll i = n ; i != 0 ; i = last[i])
		vv.push_back(i);
	
	vv.push_back(0);
	reverse(vv.begin(), vv.end());
	
	return;
}

int main(void)
{
	scanf("%lld %lld", &n, &k);
	
	for(ll i = 1 ; i <= n ; i++)
	{
		scanf("%lld", &a[i]);
		nu[i] = nu[i - 1] + a[i];
	}
	
	k++;
	l = -1, r = INF / 4;
	
	while(l <= r)
	{
		ll mid = (l + r) / 2;
		
		solve(mid * 2 + 1);
		
		if(gaet[n] < k)
			r = mid - 1;
		else
			l = mid + 1;
	}
	
	solve(l * 2 + 1);
	L = vv;
	
	solve(r * 2 + 1);
	R = vv;
	
	if((ll)L.size() - 1 == k)
		ans = L;
	else if((ll)R.size() - 1 == k)
		ans = R;
	
	else
	{
		ll p = 0;
		
		for(ll i = 0 ; i < (ll)R.size() - 1 ; i++)
		{
			while(p < (ll)L.size() && L[p] <= R[i])
				p++;
			
			if(p == (ll)L.size())
				break;
			
			if(!p)
				continue;
			
			if(L[p - 1] <= R[i] && R[i + 1] <= L[p])
			{
				for(ll j = 0 ; j <= i ; j++)
					ans.push_back(R[j]);
				
				for(ll j = p ; j < (ll)L.size() ; j++)
					ans.push_back(L[j]);
				
				if((ll)ans.size() - 1 == k)
					break;
				
				ans.clear();
			}
		}
	}
	
	for(ll i = 1 ; i < (ll)ans.size() ; i++)
	{
		ll gap = 0;
		
		for(ll j = ans[i - 1] + 1 ; j <= ans[i] ; j++)
			gap += a[j];
		
		sum += gap * gap;
	}
	
	if((ll)ans.size() == 0)
	{
		assert(0);
	}
	
	printf("%lld\n", (nu[n] * nu[n] - sum) / 2);
	
	for(ll i = 1 ; i < (ll)ans.size() - 1 ; i++)
		printf("%lld ", ans[i]);
	
	return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |  scanf("%lld %lld", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |   scanf("%lld", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 340 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 212 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 212 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 212 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 212 KB contestant found the optimal answer: 1 == 1
7 Correct 0 ms 340 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 212 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 212 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 212 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 340 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 0 ms 212 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 0 ms 340 KB contestant found the optimal answer: 140072 == 140072
14 Correct 0 ms 340 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 0 ms 212 KB contestant found the optimal answer: 805 == 805
16 Correct 0 ms 212 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 212 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 0 ms 340 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 212 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 256 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 212 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 212 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 212 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 212 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 212 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 340 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 340 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 1 ms 340 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 340 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 1 ms 340 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 1 ms 316 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 1 ms 340 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 340 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 340 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 340 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 3 ms 420 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 2 ms 340 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 3 ms 340 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 3 ms 340 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 3 ms 340 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 3 ms 340 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 3 ms 320 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 2 ms 340 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 324 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 3 ms 340 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1200 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 26 ms 1116 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 24 ms 1108 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 32 ms 1224 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 29 ms 1208 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 27 ms 1168 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 36 ms 1244 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 28 ms 1108 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 28 ms 1224 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 28 ms 1224 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 284 ms 8892 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 275 ms 8788 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 328 ms 8872 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 308 ms 9520 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 268 ms 9088 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 346 ms 9044 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 343 ms 9308 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 377 ms 9268 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 323 ms 9188 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 378 ms 9244 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845