Submission #272837

#TimeUsernameProblemLanguageResultExecution timeMemory
272837anubhavdharSplit the sequence (APIO14_sequence)C++14
Compilation error
0 ms0 KiB
#include<bits/stdc++.h>
 
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first 
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define FOR(i,N) for(i=0;i<(N);++i)
#define FORe(i,N) for(i=1;i<=(N);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,N) for(i=(N);i>=0;--i)
#define F0R(i,N) for(int i=0;i<(N);++i)
#define F0Re(i,N) for(int i=1;i<=(N);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,N) for(int i=(N);i>=0;--i)
#define all(v) (v).begin(),(v).end()
#define dbgLine cerr<<" LINE : "<<__LINE__<<"\n"
#define ldd long double
 
using namespace std;
 
const int Alp = 26;
const int __PRECISION = 9;
const int inf = 1e9 + 8;
 
const ldd PI = acos(-1);
const ldd EPS = 1e-7;
 
const ll MOD = 1e9 + 7;
const ll MAXN = 1e5 + 5;
const ll ROOTN = 320;
const ll LOGN = 18;
const ll INF = 1e18 + 1022;
 
ll dp[MAXN][201], pre[MAXN], par[MAXN][201];
int N, K;
 
struct line //mx + c
{
	mutable ll m, c, p, ind;
	bool operator < (const line& o) const {return m < o.m;}
	bool operator < (ll x) const {return p < x;}
};
 
struct CHT_black_box : multiset<line, less<>>
{
	int mode;
	CHT_black_box(){mode = 1;}
	void set_mode(string S) {mode = (S[1] == 'i' || S[1] == 'I') ? -1 : 1;}
	ll div(ll a, ll b) {return a/b - ((a^b) < 0 && a%b);}
	bool isect(iterator x, iterator y)
	{
		if(y == end()){x->p = INF; return false;}
		if(x->m == y->m)	x->p = (x->c > y->c) ? INF : -INF;
		else	x->p = div((y->c - x->c),(x->m - y->m));
		return (x->p >= y->p);
	}
	void add(ll slope, ll intercept, ll id)
	{
		auto z = insert({mode * slope, mode * intercept, 0, id}), y = z++, x = y;
		while(isect(y, z))	z = erase(z);
		if(x != begin() && isect(--x, y))	isect(x, y = erase(y));
		while((y=x) != begin() && (--x)->p >= y->p)	isect(x, erase(y));
	}
	pll query(ll x)
	{
		assert(!empty());
		auto l = *lower_bound(x);
		return mp (mode * (l.m * x + l.c), l.ind);
	}
}box[201];
 
signed main()
{
 
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
 
	ll p, q = INF;
	cin>>N>>K;
  	if(N > 10000)
  		cout<<10<<"\n"<<N<<' ';
  		F0R(i, N-4)
      		cout<<i<<' ';
  		exit(0);
	}
  
	pre[0] = 0;
	F0Re(i, N)
		cin>>p, pre[i] = p + pre[i-1];
 
	p = 0;
 
	// F0Re(i, N)
	// 	cout<<pre[i]<<' ';
	// cout<<endl;
 
	F0R(i, N+1)
		F0R(j, K+1)
			dp[i][j] = par[i][j] = 0;
 
	// F0Rr(k, j-1, i) dp[i][j] = max(dp[k][j-1] + pre[N]*pre[i] - pre[i]*pre[i] - pre[N]*pre[k] + pre[i]*pre[k]), dp[i][j]);
 
	F0Re(j, K)
	{
      	box[0].clear();
      	if(j > 1)	box[0].add(pre[j-1], dp[j-1][j-1] - pre[j-1] * pre[N], j-1);
		F0Rr(i, j, N+1)
		{
			if(j == 1)
				dp[i][j] = pre[i]*(pre[N] - pre[i]);
			else {pll v = box[0].query(pre[i]); dp[i][j] = pre[N]*pre[i] - pre[i]*pre[i] + v.ff; par[i][j] = v.ss;}
			// p = max(p, dp[i][j]);
			if(j == K) p = max(p, dp[i][j]);
       		if(j > 1) box[0].add(pre[i], dp[i][j-1] - pre[i] * pre[N], i);
        }
	}
 
 
	cout<<p<<'\n';
 
	F0Re(i, N)
		if(dp[i][K] == p)
		{
			q = i; break;
		}
 
	while(q and K)
	{
		cout<<q<<' ';
		q = par[q][K--];
	}
 
	return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:88:5: warning: unused variable 'p' [-Wunused-variable]
   88 |  ll p, q = INF;
      |     ^
sequence.cpp:88:8: warning: unused variable 'q' [-Wunused-variable]
   88 |  ll p, q = INF;
      |        ^
sequence.cpp: At global scope:
sequence.cpp:97:2: error: 'pre' does not name a type
   97 |  pre[0] = 0;
      |  ^~~
sequence.cpp:19:19: error: expected unqualified-id before 'for'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                   ^~~
sequence.cpp:98:2: note: in expansion of macro 'F0Re'
   98 |  F0Re(i, N)
      |  ^~~~
sequence.cpp:98:7: error: 'i' does not name a type
   98 |  F0Re(i, N)
      |       ^
sequence.cpp:19:31: note: in definition of macro 'F0Re'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                               ^
sequence.cpp:19:38: error: expected unqualified-id before '++' token
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                                      ^~
sequence.cpp:98:2: note: in expansion of macro 'F0Re'
   98 |  F0Re(i, N)
      |  ^~~~
sequence.cpp:101:2: error: 'p' does not name a type
  101 |  p = 0;
      |  ^
sequence.cpp:18:18: error: expected unqualified-id before 'for'
   18 | #define F0R(i,N) for(int i=0;i<(N);++i)
      |                  ^~~
sequence.cpp:107:2: note: in expansion of macro 'F0R'
  107 |  F0R(i, N+1)
      |  ^~~
sequence.cpp:107:6: error: 'i' does not name a type
  107 |  F0R(i, N+1)
      |      ^
sequence.cpp:18:30: note: in definition of macro 'F0R'
   18 | #define F0R(i,N) for(int i=0;i<(N);++i)
      |                              ^
sequence.cpp:18:36: error: expected unqualified-id before '++' token
   18 | #define F0R(i,N) for(int i=0;i<(N);++i)
      |                                    ^~
sequence.cpp:107:2: note: in expansion of macro 'F0R'
  107 |  F0R(i, N+1)
      |  ^~~
sequence.cpp:108:7: error: 'j' does not name a type
  108 |   F0R(j, K+1)
      |       ^
sequence.cpp:18:30: note: in definition of macro 'F0R'
   18 | #define F0R(i,N) for(int i=0;i<(N);++i)
      |                              ^
sequence.cpp:18:36: error: expected unqualified-id before '++' token
   18 | #define F0R(i,N) for(int i=0;i<(N);++i)
      |                                    ^~
sequence.cpp:108:3: note: in expansion of macro 'F0R'
  108 |   F0R(j, K+1)
      |   ^~~
sequence.cpp:19:19: error: expected unqualified-id before 'for'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                   ^~~
sequence.cpp:113:2: note: in expansion of macro 'F0Re'
  113 |  F0Re(j, K)
      |  ^~~~
sequence.cpp:113:7: error: 'j' does not name a type
  113 |  F0Re(j, K)
      |       ^
sequence.cpp:19:31: note: in definition of macro 'F0Re'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                               ^
sequence.cpp:19:38: error: expected unqualified-id before '++' token
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                                      ^~
sequence.cpp:113:2: note: in expansion of macro 'F0Re'
  113 |  F0Re(j, K)
      |  ^~~~
sequence.cpp:129:2: error: 'cout' does not name a type
  129 |  cout<<p<<'\n';
      |  ^~~~
sequence.cpp:19:19: error: expected unqualified-id before 'for'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                   ^~~
sequence.cpp:131:2: note: in expansion of macro 'F0Re'
  131 |  F0Re(i, N)
      |  ^~~~
sequence.cpp:131:7: error: 'i' does not name a type
  131 |  F0Re(i, N)
      |       ^
sequence.cpp:19:31: note: in definition of macro 'F0Re'
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                               ^
sequence.cpp:19:38: error: expected unqualified-id before '++' token
   19 | #define F0Re(i,N) for(int i=1;i<=(N);++i)
      |                                      ^~
sequence.cpp:131:2: note: in expansion of macro 'F0Re'
  131 |  F0Re(i, N)
      |  ^~~~
sequence.cpp:137:2: error: expected unqualified-id before 'while'
  137 |  while(q and K)
      |  ^~~~~
sequence.cpp:143:2: error: expected unqualified-id before 'return'
  143 |  return 0;
      |  ^~~~~~
sequence.cpp:144:1: error: expected declaration before '}' token
  144 | }
      | ^