제출 #101670

#제출 시각아이디문제언어결과실행 시간메모리
101670arman_ferdous수열 (APIO14_sequence)C++17
0 / 100
2054 ms83320 KiB
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
const int N = 100001;
const int K = 201;
const ll oo = 1e18;
 
int n, k;
ll S[N], dp[N][2]; int opt[N][K];

struct CHT{
	ll m[N], c[N]; int id[N], sz, last;
	void init() { sz = 0; }
	bool bad(int i, int j, int k) {
		return 1.0 * (c[j] - c[i]) * (m[i] - m[k]) >= 1.0 * (c[k] - c[i]) * (m[i] - m[j]);
	}
	void insert(ll M, ll C, int id_) {
		m[sz] = M, c[sz] = C, id[sz] = id_; sz++;
		while(sz >= 3 && bad(sz-3, sz-2, sz-1)) {
			m[sz-2] = m[sz-1];
			c[sz-2] = c[sz-1];
			id[sz-2] = id[sz-1];
			sz--;
		}
	}
	ll f(int i, ll X) { return m[i] * X + c[i]; }
	pair<ll,int> get(ll X) {
		last = min(last, sz-1);
		while(last + 1 < sz && f(last,X) < f(last+1,X))
			last++;
		return {f(last,X), id[last]};
	}
}ds;

int main() {
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++) {
		scanf("%lld", &S[i]);
		S[i] += S[i-1];
	}
 
	for(int kk = 1; kk <= k; kk++) {
		int row = (kk&1);
		ds.init();
		for(int i = 1; i <= n; i++) {
			auto got = ds.get(S[i]);
			dp[i][row] = got.first;
			opt[i][kk] = got.second;
			ds.insert(S[i], dp[i][!row] - S[i]*S[i], i);
		}
	}
	printf("%lld\n", dp[n][(k&1)]);
	int pos = n;
	for(int cnt = k; cnt > 0; cnt--) {
		printf("%d ", opt[pos][cnt]);
		pos = opt[pos][cnt];
	}
	return 0;
}

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

sequence.cpp: In function 'int main()':
sequence.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &S[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...