이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 40) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for(int i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 == v.size() ? "\n" : " ");
}
return os;
}
template <typename T>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
for(auto& e : t) {
fill_v(e, v);
}
};
struct CHT {
vector<pair<P, int>> lines;
bool check(pair<P, int> l1, pair<P, int> l2, pair<P, int> l3) {
return (l2.first.second - l3.first.second) *
(l2.first.first - l1.first.first) <=
(l1.first.second - l2.first.second) *
(l3.first.first - l2.first.first);
}
void add(ll a, ll b, int idx) {
pair<P, int> l = make_pair(P(a, b), idx);
while(lines.size() >= 2 &&
check(lines[(int)lines.size() - 2], lines.back(), l)) {
lines.pop_back();
}
lines.push_back(l);
}
int id = 0;
ll f(ll i, ll x) {
return lines[i].first.first * x + lines[i].first.second;
}
P query(ll x) {
int nid = id;
for(int i = id + 1; i < lines.size(); i++) {
if(f(i, x) > f(i - 1, x)) {
nid = i;
} else {
break;
}
}
id = nid;
if(id >= lines.size()) {
return P(0, 0);
}
return P(f(id, x), lines[id].second);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<ll> s(n + 10);
for(int i = 1; i <= n; i++) {
ll a;
cin >> a;
s[i] = s[i - 1] + a;
}
vector<ll> res(n + 10);
vector<vector<int>> id(k + 1, vector<int>(n + 1));
for(int l = 0; l < k; l++) {
vector<ll> dp(n + 10);
CHT cht;
for(int i = l + 1; i <= n; i++) {
tie(dp[i], id[l][i]) = cht.query(s[i]);
cht.add(s[i], -s[i] * s[i] + res[i], i);
}
res = dp;
}
cout << res[n] << endl;
vector<int> v;
int idx = n;
for(int l = k - 1; l >= 0; l--) {
v.push_back(id[l][idx]);
idx = id[l][idx];
}
reverse(all(v));
for(int i = 0; i < k; i++) {
cout << v[i];
if(i < k - 1) {
cout << " ";
} else {
cout << endl;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'P CHT::query(ll)':
sequence.cpp:73:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = id + 1; i < lines.size(); i++) {
~~^~~~~~~~~~~~~~
sequence.cpp:81:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(id >= lines.size()) {
~~~^~~~~~~~~~~~~~~
# | 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... |