제출 #550948

#제출 시각아이디문제언어결과실행 시간메모리
550948Pherokung수열 (APIO14_sequence)C++14
71 / 100
2070 ms67680 KiB
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define K 205
#define ll long long
typedef pair<ll,int> pa;
struct line{
	ll m,c;
	int idx;
	line(ll m,ll c,int i) : m(m), c(c), idx(i) {}
	ll dot(ll x){ return m*x + c;}	
	ll inter(line l){ return floor((long double)(c-l.c)/(l.m-m));}
};
 
struct CHT{
	deque<line> hull;
	void add(line l){
		if(hull.size() >= 1 && hull.back().m == l.m && hull.back().c == l.c) hull.pop_back();
		while(hull.size() >= 2 && l.inter(hull.back()) <= hull.back().inter(hull[hull.size()-2])) hull.pop_back();
		hull.push_back(l);
	}
	pa query(ll x){
		while(hull.size() >= 2 && hull[0].dot(x) <= hull[1].dot(x)) hull.pop_front();
		return {hull[0].dot(x), hull[0].idx};
	}
};
 
int n,k,a[N],pre[N],P[K][N],pos;
ll dp[2][N],ans=-1e18;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]), pre[i] = pre[i-1] + a[i];
	for(int i=1;i<n;i++) dp[1][i] = 1ll*(pre[n] - pre[i]) * pre[i];
	for(int i=2;i<=k;i++){
		int now = i%2, prv = now^1;
		CHT cht;
		for(int j=i;j<n;j++){
			cht.add(line(pre[j-1], 1ll * -pre[j-1] * pre[n] + dp[prv][j-1], j-1));
			pa mx = cht.query(pre[j]);
			long long res = mx.first, idx = mx.second;
			dp[now][j] = res + 1ll * pre[j] * pre[n] - 1ll * pre[j] * pre[j];
			P[i][j] = idx;
		}
	}
	
	for(int i=k;i<n;i++){
		if(dp[k%2][i] > ans){
			ans = dp[k%2][i];
			pos = i;
		}
	}
	
	stack<int> stk;
	while(k){
		stk.push(pos);
		pos = P[k--][pos];
	}
	
	printf("%lld\n",ans);
	while(!stk.empty() && stk.top() != 0){
		printf("%d ",stk.top());
		stk.pop();
	}
}

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

sequence.cpp: In function 'int main()':
sequence.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  scanf("%d%d",&n,&k);
      |  ~~~~~^~~~~~~~~~~~~~
sequence.cpp:32:29: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |  for(int i=1;i<=n;i++) scanf("%d",&a[i]), pre[i] = pre[i-1] + a[i];
      |                        ~~~~~^~~~~~~~~~~~
#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...