이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#define MAXN 100005
#define MAXK 205
#define LL long long
using namespace std;
int N, K, Prev[MAXN][MAXK];
LL A[MAXN], P[MAXN], dp[MAXN][MAXK];
vector <int> Out;
struct line {LL m, c, data;};
class CHT {
int pt;
vector<line> S;
public:
void nuke() {
pt = 0;
S.clear();
}
bool bad(line L0) {
line L1 = S.back(), L2 = S[S.size() - 2];
if (L0.m == L1.m) {return(L0.c <= L1.m);}
return((L2.c - L1.c)*(L0.m - L2.m) >= (L2.c - L0.c)*(L1.m - L2.m)); //int128?
}
void put(LL m, LL c, LL id) {
while (S.size() > 1 && bad(line {m,c,id})) {S.pop_back();}
S.push_back(line {m,c,id});
pt = min(pt, (int) S.size()-1);
}
LL eval(line L, LL x) {
return(L.m * x + L.c);
}
line ask(LL x) {
while (pt + 1 < S.size() && eval(S[pt+1], x) > eval(S[pt], x)) {pt++;}
return(S[pt]);
}
} CHT;
int main() {
//freopen("sequencein.txt","r",stdin);
scanf("%d %d",&N,&K);
for (int i=1; i<=N; i++) {
scanf("%lld",&A[i]);
P[i] = P[i-1] + A[i];
}
for (int j=1; j<=K; j++) {
CHT.nuke();
dp[0][j] = -(1LL << 60);
CHT.put(0, dp[0][j],0);
for (int i=1; i<=N; i++) {
line res = CHT.ask(P[i]);
dp[i][j] = res.m*P[i] + res.c;
Prev[i][j] = res.data;
CHT.put(P[i], dp[i][j-1] - P[i]*P[i], i);
}
}
printf("%lld\n",(long long) dp[N][K]);
for (int i=N, j=K; i>0 && j > 0; i=Prev[i][j], j--) {
Out.push_back(Prev[i][j]);
}
while (Out.size()) {
printf("%d ", Out.back());
Out.pop_back();
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'line CHT::ask(long long int)':
sequence.cpp:33:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (pt + 1 < S.size() && eval(S[pt+1], x) > eval(S[pt], x)) {pt++;}
~~~~~~~^~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
sequence.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&A[i]);
~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |