이 제출은 이전 버전의 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][2];
int p[N + 1][K + 1];
int n, k;
struct Convex
{
deque<long double> inter;
deque<long long> A, B;
deque<int> pos;
void reset()
{
pos = {0};
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(int i, long long a, long long b)
{
if (A.back() == a)
{
if (b >= B.back())
{
pos.pop_back();
A.pop_back();
B.pop_back();
inter.pop_back();
if (inter.empty())
{
pos = {i};
inter = {-1e18};
A = {a};
B = {b};
}
else
{
pos.push_back(i);
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();
pos.pop_back();
}
pos.push_back(i);
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();
pos.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 ;
}
cout << p[i][j] << ' ';
Traceback(p[i][j], j - 1);
}
void solve()
{
for (int j = 1; j <= k; ++ j)
{
CHT.reset();
for (int i = 1; i <= n; ++ i)
{
int cur = j & 1;
dp[i][cur] = CHT.get(f[i]);
p[i][j] = CHT.pos.front();
CHT.add(i, f[i], dp[i][cur ^ 1] - f[i] * f[i]);
}
}
cout << dp[n][k & 1] << '\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:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | 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... |