QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#795331 | #9800. Crisis Event: Meteorite | ucup-team296# | WA | 132ms | 3836kb | C++20 | 6.0kb | 2024-11-30 19:43:29 | 2024-11-30 19:43:29 |
Judging History
answer
#include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;
// @author: pashka
struct test {
struct seg {
int r;
int ans = INT_MAX;
int p = -1;
};
int find(vector<seg> &a, int x) {
int l = -1;
int r = (int) a.size();
while (r > l + 1) {
int m = (l + r) / 2;
if (a[m].r < x) l = m; else r = m;
}
return r;
}
void solve() {
int n, m;
cin >> m >> n;
vector<int> c(m);
for (int i = 0; i < m; i++) cin >> c[i];
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < m; i++)
if (c[i] == 1 && a[0][i] > 0) {
cout << "-1\n";
return;
}
for (int i = 0; i < n; i++) {
bool ok = false;
for (int j = 0; j < m; j++) {
if (a[i][j] == 0)ok = true;
}
if (!ok) {
cout << "-1\n";
return;
}
}
vector<vector<vector<seg>>> s(n, vector<vector<seg>>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 0) {
for (int k = j; k < m; k++) {
if (k != j && a[i][k] == 0) break;
s[i][j].push_back({k, INT_MAX, -1});
}
} else {
int k = j + 1;
while (k < m && a[i][k] != 0) k++;
if (k < m) {
s[i][j].push_back({k, INT_MAX, -1});
}
}
}
}
vector<int> p(n, m);
for (int j = m - 1; j >= 0; j--) {
for (int i = 0; i < n; i++) {
if (a[i][j] == 0) p[i] = j;
}
vector<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && p[st.back()] < p[i]) st.pop_back();
for (auto &ss: s[i][j]) {
int l = -1;
int r = (int) st.size();
while (r > l + 1) {
int mm = (l + r) / 2;
if (p[st[mm]] > ss.r) {
l = mm;
} else {
r = mm;
}
}
if (l != -1) {
ss.p = st[l];
}
}
st.push_back(i);
}
}
vector<vector<int>> ps(n, vector<int>(m + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ps[i][j + 1] = ps[i][j] + a[i][j];
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j <= m; j++) {
ps[i][j] += ps[i - 1][j];
}
}
vector<vector<int>> nr(n, vector<int>(m));
vector<vector<int>> nl(n, vector<int>(m));
for (int i = 0; i < n; i++) {
int k = -1;
for (int j = 0; j < m; j++) {
if (a[i][j] == 0) k = j;
nl[i][j] = k;
}
k = m;
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == 0) k = j;
nr[i][j] = k;
}
}
for (int j = 0; j < m; j++) {
for (auto &ss: s[n - 1][j]) {
ss.ans = ps[n - 1][ss.r + 1] - ps[n - 1][j];
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
for (auto &ss: s[i][j]) {
if (ss.ans == INT_MAX) continue;
if (ss.p == -1) continue;
int l = j;
int r = ss.r;
int ii = ss.p;
int rr = nr[ii][r];
if (rr < m) {
auto &ss2 = s[ii][l][find(s[ii][l], rr)];
ss2.ans = min(ss2.ans, ss.ans +
ps[ii][rr + 1] - ps[ii][r + 1]
);
}
int ll = nl[ii][l];
if (ll >= 0) {
auto &ss2 = s[ii][ll][find(s[ii][ll], r)];
ss2.ans = min(ss2.ans, ss.ans +
ps[ii][l] - ps[ii][ll]
);
}
}
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// for (auto &ss : s[i][j]) {
// cout << i << " " << j << "-" << ss.r << ": " << ss.ans << " ^ " << ss.p << "\n";
// }
// }
// }
vector<long> d1(m + 1, INT_MAX);
vector<long> d2(m + 1, INT_MAX);
vector<long> d3(m + 1, INT_MAX);
d1[0] = 0;
for (int j = 0; j < m; j++) {
if (c[j] == 0) {
d1[j + 1] = min(d1[j + 1], min(d1[j], d3[j]));
}
d2[j + 1] = min(d2[j + 1], min(d1[j], d2[j]) + a[0][j]);
d3[j + 1] = min(d3[j + 1], d3[j] + a[0][j]);
for (int i = 0; i < n; i++) {
for (auto &ss: s[i][j]) {
if (ss.ans == INT_MAX) continue;
d3[ss.r + 1] = min(
d3[ss.r + 1],
min(d1[j], d2[j]) + ss.ans
);
}
}
}
cout << min(d1[m], d3[m]) << "\n";
}
};
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
test().solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3836kb
input:
2 3 4 1 0 1 0 1 0 2 0 0 0 0 3 4 5 0 1 1 1 1000
output:
4 -1
result:
ok 2 number(s): "4 -1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
1 1 1 1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 132ms
memory: 3812kb
input:
10000 3 15 0 0 1 0 0 0 0 0 0 0 0 0 818 0 0 0 404 0 0 684 0 0 0 966 0 69 602 0 835 443 458 0 189 0 0 388 0 0 0 768 128 0 0 959 466 0 324 170 59 1 1 0 1 1 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0 1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 0 0 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 0 223 0 0 0 260 0 0 0 504 0 583 878 0...
output:
2044 0 3578 5737 118 1349 0 0 870 3644 3318 0 0 0 3355 0 3608 0 1182 1665 15 0 0 2037 0 0 2415 0 0 0 0 2852 0 0 656 0 1086 0 955 2377 6576 1554 0 141 1794 494 3810 3609 803 0 1848 0 1883 6527 1341 0 0 135 0 0 642 0 0 0 23 0 323 900 2401 0 0 4788 79 2761 3392 20 0 1540 0 0 0 4489 1383 115 4376 1860 9...
result:
wrong answer 1st numbers differ - expected: '6752', found: '2044'