이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define lx (id << 1)
#define rx (lx 1)
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;
// #define Sieve
#ifdef Sieve
vector<int> pr;
vector<int> lpf;
void Linear_sieve(int n = Lim) {
pr.assign(1, 2);
lpf.assign(n + 1, 2);
for (int x = 3; x <= n; x += 2) {
if (lpf[x] == 2) pr.push_back(lpf[x] = x);
for (int i = 1; i < pr.size() && pr[i] <= lpf[x] && pr[i] * x <= n; ++i)
lpf[pr[i] * x] = pr[i];
}
}
#endif
/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====
const int base = 3;
const int N = 1e5 + 5;
const int K = 200 + 1;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;
struct Line {
int a;
ll b;
int idx;
Line() = default;
Line(int a, ll b, int id):
a(a), b(b), idx(id) {}
bool operator< (const Line &x) const { return a < x.a; };
ll operator() (ll x) const { return a * x + b; }
};
struct LineContainer {
deque<Line> st;
ld isect(const Line &x, const Line &y) {
return ld(y.b - x.b) / (x.a - y.a);
}
int bad(const Line &a, const Line &b, Line &c) {
if (b.a == c.a) {
maximize(c.b, b.b);
return true;
}
return isect(a, b) < isect(b, c);
}
void add(int k, ll m, int id) {
int top = st.size();
Line l = Line(k, m, id);
while (top >= 2 && bad(st[top - 2], st[top - 1], l))
st.pop_back(), --top;
st.push_back(l);
}
pair<ll, int> query(int x) {
int l = 0, r = st.size() - 1;
while (r >= 1 && isect(st[l], st[l + 1]) >= x) {
st.pop_front(); --r;
}
auto ans = make_pair(st[l](x), st[l].idx);
if (l < r) maximize(ans, make_pair(st[l + 1](x), st[l + 1].idx));
return ans;
}
};
int n, k;
int a[N], pref[N];
ll dp[N][2];
signed par[K][N];
LineContainer cht;
ll f(int l, int r) {
return pref[r] - pref[l - 1];
}
/**
7 3
4 1 3 4 0 2 3
**/
void solve() {
cin >> n >> k;
foru(i, 1, n) cin >> a[i];
foru(i, 1, n) pref[i] = pref[i - 1] + a[i];
foru(i, 0, n) dp[i][0] = -Linf;
dp[0][0] = 0;
foru(z, 1, k) {
int layer = z & 1;
foru(i, 0, n) dp[i][layer] = -Linf;
cht.st.clear();
foru(i, 1, n - 1) {
int j = i - 1;
cht.add(-pref[j], dp[j][!layer], j);
pair<ll, int> res = cht.query(f(i + 1, n));
dp[i][layer] = res.first + f(i + 1, n) * pref[i];
par[z][i] = res.second;
}
}
ll ans = -Linf;
int idx = -1;
foru(i, 1, n - 1) if (maximize(ans, dp[i][k & 1]))
idx = i;
cout << ans << "\n";
for (int z = k; z > 0; --z) {
cout << idx << " ";
idx = par[z][idx];
}
}
signed main() {
FastIO;
#define task ""
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
#define task "Test"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
} else if (0) {
ofstream fout(task".inp");
fout.close();
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
#ifdef Sieve
Linear_sieve();
#endif
int ntest = 1;
// cin >> ntest;
while (ntest--) {
//cout << "Case " << q << ": " << "\n";
solve();
cout << "\n";
}
return 0;
}
/** /\_/\
* (= ._.)
* / >TL \>AC
**/
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp:166: warning: "task" redefined
166 | #define task "Test"
|
beads.cpp:160: note: this is the location of the previous definition
160 | #define task ""
|
beads.cpp: In function 'int main()':
beads.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |