QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#874790 | #9916. Defeat the Enemies | distant_yesterday | WA | 0ms | 3712kb | C++14 | 4.9kb | 2025-01-28 16:18:33 | 2025-01-28 16:18:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int MOD = 998244353;
void solve() {
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
vector<int> a(n), b(n);
for (auto& x : a) cin >> x;
for (auto& x : b) cin >> x;
int k;
cin >> k;
vector<int> c(k);
for (auto& x : c) cin >> x;
int S = 0;
for (int i = 0; i < n; ++i) {
S = max(S, b[i] + a[i]);
}
vector<int> required_points;
for (int i = 0; i < n; ++i) {
int L = b[i];
int R = S - a[i];
if (L == R) {
required_points.push_back(L);
}
}
required_points.push_back(0);
required_points.push_back(S);
sort(required_points.begin(), required_points.end());
required_points.erase(unique(required_points.begin(), required_points.end()), required_points.end());
bool possible = true;
ll total_cost = 0;
ll total_ways = 1;
for (int i = 1; i < required_points.size() && possible; ++i) {
int s_prev = required_points[i-1];
int s_curr = required_points[i];
int delta = s_curr - s_prev;
if (delta == 0) continue;
vector<pair<int, int>> adjusted_enemies;
for (int j = 0; j < n; ++j) {
int L = b[j];
int R_total = S - a[j];
if (s_prev < L && L <= s_curr) {
int L_prime = L - s_prev;
int R_prime = R_total - s_prev;
adjusted_enemies.emplace_back(L_prime, R_prime);
}
}
if (adjusted_enemies.empty()) {
vector<ll> dp(delta + 1, INF);
vector<ll> ways(delta + 1, 0);
dp[0] = 0;
ways[0] = 1;
for (int s = 0; s <= delta; ++s) {
if (dp[s] == INF) continue;
for (int x = 1; x <= k; ++x) {
if (s + x > delta) continue;
if (dp[s + x] > dp[s] + c[x-1]) {
dp[s + x] = dp[s] + c[x-1];
ways[s + x] = ways[s];
} else if (dp[s + x] == dp[s] + c[x-1]) {
ways[s + x] = (ways[s + x] + ways[s]) % MOD;
}
}
}
if (dp[delta] == INF) {
possible = false;
} else {
total_cost += dp[delta];
total_ways = (total_ways * ways[delta]) % MOD;
}
} else {
int a_max = 0, b_min = 1e9;
for (auto [L, R] : adjusted_enemies) {
a_max = max(a_max, L);
b_min = min(b_min, R);
}
if (a_max > b_min) {
possible = false;
continue;
}
vector<vector<ll>> dp(delta + 1, vector<ll>(2, INF));
vector<vector<ll>> ways(delta + 1, vector<ll>(2, 0));
dp[0][0] = 0;
ways[0][0] = 1;
for (int s = 0; s <= delta; ++s) {
for (int flag : {0, 1}) {
if (dp[s][flag] == INF) continue;
for (int x = 1; x <= k; ++x) {
int new_s = s + x;
if (new_s > delta) continue;
int new_flag = flag;
if (a_max <= new_s && new_s <= b_min) {
new_flag = 1;
}
ll new_cost = dp[s][flag] + c[x-1];
if (new_cost < dp[new_s][new_flag]) {
dp[new_s][new_flag] = new_cost;
ways[new_s][new_flag] = ways[s][flag];
} else if (new_cost == dp[new_s][new_flag]) {
ways[new_s][new_flag] = (ways[new_s][new_flag] + ways[s][flag]) % MOD;
}
}
}
}
if (dp[delta][1] == INF) {
possible = false;
} else {
total_cost += dp[delta][1];
total_ways = (total_ways * ways[delta][1]) % MOD;
}
}
}
if (!possible) {
cout << "0 0\n";
} else {
cout << total_cost << ' ' << total_ways << '\n';
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3712kb
input:
4 5 5 3 5 2 1 2 3 1 3 2 3 3 2 3 4 3 2 2 2 2 2 2 2 3 2 3 3 7 6 5 3 4 6 6 3 4 4 6 4 2 3 5 5 4 2 4 6 7 10 100 38 49 79 66 49 89 21 55 13 23 67 56 26 39 56 16 84 50 92 82 11 6 6 7 8 9 9 9 9 9 9 9
output:
9 1 6 1 18 16 116 56
result:
wrong answer 4th numbers differ - expected: '4', found: '1'