QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#194510 | #7523. Partially Free Meal | ucup-team228# | TL | 2ms | 13900kb | C++20 | 3.3kb | 2023-09-30 20:57:37 | 2023-09-30 20:57:39 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2") // doesn't work on Yandex
//#pragma GCC target("avx") // for old judges
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt") // fast bit operations
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<>>;
const ll inf = 1e18;
const int N = 2e5 + 10;
ll a[N], b[N];
ll dp[2][N];
pair<ll, ll> c[N];
ll ans[N];
void solve(int n) {
for (int i = 1; i <= n; i++) {
c[i] = {b[i], a[i]};
}
sort(c + 1, c + n + 1);
for (int i = 1; i <= n; i++) {
a[i] = c[i].second;
b[i] = c[i].first;
}
for (int i = 1; i <= n; i++) {
dp[0][i] = b[i];
}
for (int k = 1; k <= n; k++) {
dp[k & 1][k - 1] = inf;
ans[k] = inf;
for (int i = k; i <= n; i++) {
dp[k & 1][i] = min(dp[k & 1][i - 1] - b[i - 1] + b[i], dp[(k & 1) ^ 1][i - 1] - b[i - 1] + a[i] + b[i]);
ans[k] = min(ans[k], dp[k & 1][i]);
}
}
}
ll res[N], mem_b[N], mem_mask[N];
void slow(int n) {
for (int i = 1; i <= n; i++) {
res[i] = inf;
}
for (int mask = 1; mask < (1 << n); mask++) {
ll sum = 0, mx = 0;
for (int i = 1; i <= n; i++) {
if (mask & (1 << (i - 1))) {
sum += a[i];
mx = max(mx, b[i]);
}
}
int k = __builtin_popcount(mask);
if (res[k] > sum + mx) {
res[k] = sum + mx;
mem_b[k] = mx;
mem_mask[k] = mask;
} else if (res[k] == sum + mx && mem_b[k] > mx) {
mem_b[k] = mx;
mem_mask[k] = mask;
}
}
}
void stress() {
mt19937 rnd;
while (true) {
int n = rnd() % 3 + 1;
for (int i = 1; i <= n; i++) {
a[i] = rnd() % 10 + 1;
b[i] = rnd() % 10 + 1;
}
solve(n);
slow(n);
bool ok = true;
// for (int i = 1; i + 1 <= n; i++) {
// ok &= mem_b[i] <= mem_b[i + 1];
// }
for (int i = 1; i <= n; i++) {
ok &= ans[i] == res[i];
}
if (ok) {
cout << "OK" << endl;
} else {
cout << "WA\n";
cout << n << "\n";
for (int i = 1; i <= n; i++) {
cout << a[i] << " " << b[i] << "\n";
}
cout << "\n";
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
cout << "\n";
for (int i = 1; i <= n; i++) {
cout << res[i] << "\n";
}
break;
}
}
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
// stress();
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
solve(n);
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
// cout << "\n";
// slow(n);
// for (int i = 1; i <= n; i++) {
// cout << res[i] << "\n";
// }
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 13900kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
input:
200000 466436993 804989151 660995237 756645598 432103296 703610564 6889895 53276988 873617076 822481192 532911431 126844295 623111499 456772252 937464699 762157133 708503076 786039753 78556972 5436013 582960979 398984169 786333369 325119902 930705057 615928139 924915828 506145001 164984329 208212435...