답안 #975679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
975679 2024-05-05T17:26:16 Z dosts 수열 (APIO14_sequence) C++17
0 / 100
20 ms 82268 KB
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops,Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " << 
#define vi vector<int>
const int N = 1e5+1, K = 2e2+1,inf = 1e18;
 
 
int dp2[N],dp[N];
int32_t stat[N][K];
 
 
struct Line {
	int a,b;
	int eval(int x) {
		return a*x+b;
	}
	int div(int a,int b) {
		return a/b - ((a^b) < 0 && a%b);
	}
	int isect(const Line& l) {
		if (l.a == a) return b <= l.b ? -inf : inf;
		return div(b-l.b,l.a-a);
	}
};
 
struct CHT {
	deque<pair<Line,int>> cht;
 
	pii query(int x) {
		while (cht.size() > 1 && cht[0].ff.eval(x) <= cht[1].ff.eval(x)) cht.pop_front();
		return {cht.front().ff.eval(x),cht.front().ss};
	}
	void add(Line l,int idx) {
		while (cht.size() > 1 && cht[cht.size()-2].ff.isect(cht.back().ff) <= cht.back().ff.isect(l)) {
			cht.pop_back();
		}
		cht.push_back({l,idx});
	} 
};
 
void solve() {
	int n,k;
	cin >> n >> k;
	vi a(n+1),p(n+1,0);
	for (int i=1;i<=n;i++) cin >> a[i];	
	for (int i=1;i<=n;i++) p[i] = p[i-1]+a[i];
	CHT cht;
	for (int j=1;j<=k;j++) {
		cht.add(Line{0,0},0);
		for (int i=1;i<=n;i++) {
			auto fff = cht.query(p[i]);
			dp[i] = fff.ff,stat[i][j] = fff.ss;
			cht.add({p[i],dp2[i]-(p[i]*p[i])},i);
		}
		cht.cht.clear();
		for (int i=1;i<=n;i++) dp2[i] = dp[i];
	}
	cout << dp2[n] << endl;
	vi cuts;
	int ptr = n,ptr2 = k;
	while (ptr2) {
		cuts.push_back(stat[ptr][ptr2]);
		ptr = stat[ptr][ptr2];
		ptr2--;
	}
	reverse(cuts.begin(),cuts.end());
	for (auto it : cuts) cout << it << " ";
	cout << endl;
}
                
                            
signed main() { 
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Dodi
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int t = 1;
    //cin >> t; 
    while (t --> 0) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB contestant found the optimal answer: 108 == 108
2 Incorrect 0 ms 2396 KB contestant didn't find the optimal answer: 951 < 999
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB contestant didn't find the optimal answer: 1091288 < 1093956
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 2652 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 2652 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Incorrect 1 ms 2652 KB contestant didn't find the optimal answer: 136631827923 < 1499437552673
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4700 KB contestant didn't find the optimal answer: 20180056 < 21503404
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 10844 KB contestant didn't find the optimal answer: 1794250000 < 1818678304
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 82268 KB contestant didn't find the optimal answer: 6292740000 < 19795776960
2 Halted 0 ms 0 KB -