제출 #197801

#제출 시각아이디문제언어결과실행 시간메모리
197801dennisstarSplit the sequence (APIO14_sequence)C++17
60 / 100
2041 ms29828 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ryan bear
#define sq(X) ((X)*(X))
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).erase(unique(all(V)), (V).end())
using namespace std;
typedef long long ll;
typedef vector<ll> vlm;
typedef vector<int> vim;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll INF = (1ll<<60);

int N, K; vlm ar; ll mx; int mi; int lst[205][100010];

struct line {
	ll a, b; int ind;
	ll get(ll x) { return a*x+b; }
	line() {}
	line(ll A, ll B, int i) { a=A, b=B, ind=i; }
};

struct LiChao {
	int l[100010], r[100010], tp;
	line L[100010];
	inline void init() { memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); tp=2; L[0]=L[1]=line(0, -INF, 0); }
	inline void upd(int now, line &v, ll s, ll e) {
		ll md=(s+e)/2;
		line lo=L[now], hi=v;
		if (lo.get(s)>hi.get(s)) swap(lo, hi);
		if (lo.get(e)<=hi.get(e)) { L[now]=hi; return ; }
 
		if (lo.get(md)>=hi.get(md)) {
			L[now]=lo;
			if (!l[now]) { l[now]=tp; tp++; }
			upd(l[now], hi, s, md);
		}
		else {
			L[now]=hi;
			if (!r[now]) { r[now]=tp; L[tp]=line(0, -INF, 0); tp++; }
			upd(r[now], lo, md+1, e);
		}
	}
	inline pll get(int now, ll x, ll s, ll e) {
		if (!now) return pll(-INF, 0);
		ll md=(s+e)/2;
		if (x<=md) return max(pll(L[now].get(x), L[now].ind), get(l[now], x, s, md));
		else return max(pll(L[now].get(x), L[now].ind), get(r[now], x, md+1, e));
	}
}lct[2];

void dfs(int k, int i) {
	if (!k) return ;
	dfs(k-1, lst[k][i]);
	printf("%d ", i);
}

int main() {
	scanf("%d %d", &N, &K); ar.resize(N+1);
	for (int i=1; i<=N; i++) { scanf("%lld", &ar[i]); ar[i]+=ar[i-1]; }
	line im=line(ar[N], 0, 0);
	lct[0].init(); lct[0].upd(1, im, 0, ar[N]);
	for (int i=1; i<K; i++) {
		lct[i&1].init();
		for (int j=i; j<N; j++) {
			auto k=lct[(i-1)&1].get(1, ar[j], 0, ar[N]); k.fi-=ar[j]*ar[j];
			lst[i][j]=k.se;
			im=line(ar[N]+ar[j], k.fi-ar[N]*ar[j], j);
			lct[i&1].upd(1, im, 0, ar[N]);
		}
	}
	for (int i=K; i<N; i++) {
		auto k=lct[(K-1)&1].get(1, ar[i], 0, ar[N]);
		lst[K][i]=k.se;
		if (mx<=k.fi-ar[i]*ar[i]) mx=k.fi-ar[i]*ar[i], mi=i;
	}
	printf("%lld\n", mx);
	dfs(K, mi); puts("");
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int main()':
sequence.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &K); ar.resize(N+1);
  ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:65:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=N; i++) { scanf("%lld", &ar[i]); ar[i]+=ar[i-1]; }
                             ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...