이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5, K = 2e2;
long long f[N + 1], dp[N + 1][K + 1];
int n, k;
struct Convex
{
deque<long double> inter;
deque<long long> A, B;
void reset()
{
inter = {-1e18};
A = {0};
B = {0};
}
long double Intersect(int i, int j)
{
return (long double)(B[j] - B[i])/(A[i] - A[j]);
}
bool ok(long long a, long long b)
{
long double x1 = (long double)(B[A.size() - 2] - b)/(a - A[A.size() - 2]);
return x1 > Intersect(A.size() - 2, A.size() - 1);
}
void add(long long a, long long b)
{
if (A.back() == a)
{
if (b > B.back())
{
A.pop_back();
B.pop_back();
inter.pop_back();
if (inter.empty())
{
inter = {-1e18};
A = {a};
B = {b};
}
else
{
A.push_back(a);
B.push_back(b);
inter.push_back(Intersect(A.size() - 2, A.size() - 1));
}
}
return ;
}
while(inter.size() >= 2 && !ok(a, b))
{
inter.pop_back();
A.pop_back();
B.pop_back();
}
A.push_back(a);
B.push_back(b);
inter.push_back(Intersect(A.size() - 2, A.size() - 1));
}
long long get(long long x)
{
while(inter.size() >= 2 && inter[1] <= x)
{
inter.pop_front();
A.pop_front();
B.pop_front();
}
return A.front() * x + B.front();
}
} CHT;
void read()
{
cin >> n >> k;
for (int i = 1; i <= n; ++ i)
{
cin >> f[i];
f[i] = f[i - 1] + f[i];
}
}
void Traceback(int i, int j)
{
if (j == 0)
{
return ;
}
for (int l = i - 1; l >= 1; -- l)
{
if (dp[i][j] == dp[l][j - 1] + (f[i] - f[l]) * f[l])
{
Traceback(l, j - 1);
cout << l << ' ';
return ;
}
}
}
void solve()
{
for (int j = 1; j <= k; ++ j)
{
CHT.reset();
for (int i = 1; i <= n; ++ i)
{
dp[i][j] = CHT.get(f[i]);
CHT.add(f[i], dp[i][j - 1] - f[i] * f[i]);
}
}
cout << dp[n][k] << '\n';
Traceback(n, k);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |