이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 100111
#define maxk 211
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
struct line{
int type;
double x;
ll k, n;
int ind;
};
bool operator<(line a, line b){
if(a.type + b.type > 0)
return a.x < b.x;
return a.k > b.k;
}
set<line> env;
typedef set<line>::iterator sit;
double intersect(sit a, sit b){
return (double) (b -> n - a -> n) / (a -> k - b -> k);
}
bool has_prev(sit it){
return it != env.begin();
}
bool has_next(sit it){
return it != env.end() && next(it) != env.end();
}
void calc_x(sit it){
if(has_prev(it)){
line l = *it;
l.x = intersect(prev(it), it);
env.insert(env.erase(it), l);
}
}
bool irrelevant(sit it){
if(has_next(it) && next(it) -> n <= it -> n)
return 1;
return has_prev(it) && has_next(it) && intersect(prev(it), next(it)) <= intersect(prev(it), it);
}
void add(ll k, ll n, int id){
sit it = env.lower_bound({0, 0, k, n, -1});
if(it != env.end() && it -> k == k){
if(it -> n <= n)
return;
else
env.erase(it);
}
it = env.insert({0, 0, k, n, id}).fi;
if(irrelevant(it)){
env.erase(it);
return;
}
while(has_prev(it) && irrelevant(prev(it)))
env.erase(prev(it));
while(has_next(it) && irrelevant(next(it)))
env.erase(next(it));
if(has_next(it))
calc_x(next(it));
calc_x(it);
}
pair<ll, int> query(ll x){
if(env.size() == 0)
return mp(INF, -1);
sit it = env.upper_bound({1, double(x), 0, 0, -1});
it--;
// cout << it -> k << " " << it -> n << " " << it -> ind << endl;
return mp(it -> k * x + it -> n, it -> ind);
}
ll dp[maxn][maxk], pre[maxn], arr[maxn];
int from[maxn][maxk];
ll sum(int i, int j){
if(i > j)
return 0LL;
return pre[j] - pre[i - 1];
}
int main(){
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++){
scanf("%lld", arr + i);
pre[i] = pre[i - 1] + arr[i];
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= k; j++)
dp[i][j] = -INF;
}
for(int i = 0; i <= k; i++)
dp[0][i] = 0;
for(int i = 1; i <= n - 1; i++)
dp[i][1] = sum(1, i) * sum(i + 1, n);
for(int j = 2; j <= k; j++){
env.clear();
for(int i = j; i <= n - 1; i++){
add(-(-sum(1, i - 1)), -(dp[i - 1][j - 1]), i - 1);
pair<ll, int> cur = query( sum(i + 1, n) );
dp[i][j] = -cur.fi + sum(i + 1, n) * sum(1, i);
from[i][j] = cur.se;
}
}
/* for(int i = 0; i <= k; i++){
for(int j = 0; j <= n; j++)
printf("%lld ", dp[j][i]);
printf("\n");
} */
int ans = 0;
for(int i = 1; i <= n - 1; i++){
if(dp[i][k] > dp[ans][k])
ans = i;
}
printf("%lld\n", dp[ans][k]);
for(int i = k; i >= 1; i--){
printf("%d ", ans);
ans = from[ans][i];
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:112: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:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", arr + 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... |