이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template <typename T> class negarr {
private:
T data[505000 * 2 + 1];
public:
negarr() { memset(data, 0, sizeof(data)); }
void reset() { fill(data, data + 505000 * 2 + 1, -1000000000); }
inline T &operator[](int i) { return data[i + 505000]; }
};
class pstate {
public:
int x, y;
pstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const pstate &a, const pstate &b) {
return a.x + a.y < b.x + b.y;
}
};
class nstate {
public:
int x, y;
nstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const nstate &a, const nstate &b) {
return a.y - a.x < b.y - b.x;
}
};
negarr<int> a, knapsack;
int main() {
int m;
long long l;
cin >> m >> l;
const int BOUND = m * (m + 1) / 2 * 100;
if (l > BOUND || l < -BOUND) {
printf("impossible\n");
return 0;
}
knapsack.reset();
knapsack[0] = 0;
for (int i = -m; i <= m; i++) {
cin >> a[i];
negarr<int> tmp;
tmp.reset();
if (i == 0) {
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] += a[i];
}
} else {
if (i > 0) {
vector<priority_queue<nstate>> pq(i);
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
int xx = (id + BOUND) / i;
while (!pq[mod].empty() && xx - pq[mod].top().x > a[i])
pq[mod].pop();
while (!pq[mod].empty() && pq[mod].top() < nstate(xx, knapsack[id]))
pq[mod].pop();
pq[mod].push({xx, knapsack[id]});
if (!pq[mod].empty()) {
nstate t = pq[mod].top();
tmp[id] = max(tmp[id], t.y - t.x + xx);
}
}
} else if (i < 0) {
vector<priority_queue<pstate>> pq(-i);
for (int id = BOUND; id >= -BOUND; id--) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
int xx = (id + BOUND) / (-i);
while (!pq[mod].empty() && pq[mod].top().x - xx > a[i])
pq[mod].pop();
while (!pq[mod].empty() && pq[mod].top() < pstate(xx, knapsack[id]))
pq[mod].pop();
pq[mod].push({xx, knapsack[id]});
if (!pq[mod].empty()) {
pstate t = pq[mod].top();
knapsack[id] = max(knapsack[id], t.y + t.x - xx);
}
}
}
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] = max(knapsack[id], tmp[id]);
}
}
}
if (knapsack[l] >= 0)
printf("%d\n", knapsack[l]);
else
printf("impossible\n");
return 0;
}
# | 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... |
# | 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... |