이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define nl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define dcit cin.sync_with_stdio(0);cin.tie(0)
#define frp(x, y) freopen(x, "r", stdin);freopen(y, "w", stdout);
#define endl '\n'
typedef pair<int, int> pii;
typedef long long ll;
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scani(T& x){x = 0; register bool _ = 0;register T c = gc;while((c<48||c>57)&&c!=45) c=gc;_=c== 45;c=_ ?gc:c;while (c<48||c>57)c=gc;for (;c<48||c>57;c=gc);for (;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printi(T n) {register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);pc(10);}
string nextstring() { string r; register char c=gc;while(c==32||c==10) c = gc;while(!(c==32||c==10)) { r += c;c = gc; }return r; }
char nextgetchar(){ register char c = gc; while(c==32 || c==10) c = gc; return c;}
void prints(string k){ for (int i = 0; i < k.length(); i++)pc(k[i]); }
void scani() {}
template<typename First, typename ... Ints>
void scani(First &arg, Ints&... rest) { scani(arg);scani(rest...); }
const int MAXN = 100002;
int N, K, pos[202][MAXN], cur, pre, now = 1;
ll dp[2][MAXN], psa[MAXN], ans = -1;
deque<int> q;
inline double slope(int p, int q) {
if (psa[p] == psa[q])return -1.0e8;
return (double) (dp[pre][p] - psa[p] * psa[N] - dp[pre][q] + psa[q] * psa[N]) / (double) (psa[q] - psa[p]);
}
int main() {
scani(N, K);
for (int i = 1; i <= N; i++) {
scani(psa[i]);
psa[i] += psa[i - 1];
}
for (int k = 1; k <= K; k++) {
memset(dp[now], -1, sizeof(dp[now]));
q.clear();
q.push_back(0);
for (int i = 1; i <= N; i++) {
while (q.size()>1 && slope(q[0], q[1]) <= psa[i])
q.pop_front();
int j = q[0];
dp[now][i] = dp[pre][j] + (psa[i] - psa[j]) * (psa[N] - psa[i]);
pos[k][i] = j;
while (q.size() > 1 && slope(q[q.size()-2], q[q.size()-1]) >= slope(q[q.size()-1], i))
q.pop_back();
q.push_back(i);
// for (int j = last; j < i; j++) {
// ll temp = dp[pre][j]+(psa[i]-psa[j])*(psa[N]-psa[i]);
// if(temp >=dp[now][i]) {
// dp[now][i] = temp; pos[k][i] = j; last = j;
// }
// }
}
pre ^= 1;
now ^= 1;
}
for (int i = 1; i <= N; i++)
if (dp[pre][i] > ans) {
ans = dp[pre][i];
cur = i;
}
printi(ans);
for (int i = K; i >= 1; i--) {
printf("%d ", cur);
cur = pos[i][cur];
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void prints(std::string)':
sequence.cpp:24:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | void prints(string k){ for (int i = 0; i < k.length(); i++)pc(k[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... |