답안 #20819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
20819 2017-02-17T12:32:49 Z kdh9949 수열 (APIO14_sequence) C++14
0 / 100
0 ms 1844 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;

struct Cht{
	struct Line{
		ll a, b; int x;
	};
    double cr(Line p, Line q){
        return (q.b - p.b) * 1.0 / (p.a - q.a);
    }
	Line dq[100010];
	int f, r;
	void clr(){ f = r = 0; }
	void upd(ll a, ll b, int x){
		Line cur = {a, b, x};
        while(f + 1 < r && ((dq[r - 1].a == cur.a && dq[r - 1].b <= cur.b) ||
							(dq[r - 1].a != cur.a && cr(dq[r - 2], cur) <= cr(dq[r - 2], dq[r - 1])))) r--;
        if(f == r || dq[r - 1].a != cur.a) dq[r++] = cur;
	}
	pli query(ll x){
		while(f + 1 < r && x >= cr(dq[f], dq[f + 1])) f++;
		return {dq[f].a * x + dq[f].b, dq[f].x};
	}
}C;

int n, k, b[210][100010];
ll s[100010], dp[210][100010];

void btk(int x, int y){
	if(!x) return;
	btk(x - 1, b[x][y]);
	printf("%d ", b[x][y]);
}

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 i = 1; i <= n; i++) dp[0][i] = s[i] * (s[n] - s[i]);
	for(int i = 1; i <= k; i++){
        C.clr();
        C.upd(s[i], dp[i - 1][i], i);
        for(int j = i + 1; j <= n; j++){
			ll t = s[n] - s[j];
			pli q = C.query(-t);
            dp[i][j] = q.first + t * s[j];
            b[i][j] = q.second;
            C.upd(s[j], dp[i - 1][j], j);
        }
	}
	printf("%lld\n", dp[k][n]);
	btk(k, n);
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:38:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
sequence.cpp:40:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", s + i);
                       ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -