QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780093#9800. Crisis Event: MeteoriteAfterlifeWA 62ms3788kbC++202.7kb2024-11-25 00:54:012024-11-25 00:54:02

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 00:54:02]
  • 评测
  • 测评结果:WA
  • 用时:62ms
  • 内存:3788kb
  • [2024-11-25 00:54:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n , m;
typedef long long ll ;
typedef pair<int,ll> pil ;
const ll inf = 1e18;
typedef pair<int,int> pii;
void solv() {
    cin >> n >> m;
    vector<vector<int> > a(m + 1 , vector<int>(n + 1)) ;
    vector<vector<pil> > f(m + 1 , vector<pil>(n + 1)) ;
    vector<vector<ll> > s(m + 1 , vector<ll>(n + 1)) ;
    vector<int> c(n + 1) ;
    for(int i = 1;i <= n;i++) cin >> c[i];
    for(int i = 1;i <= m;i++) {
        for(int j = 1;j <= n;j++) {
            cin >> a[i][j];
            s[i][j] = s[i][j - 1] + a[i][j] ;
        }
    }
    for(int i = 1;i <= m;i++) {
        bool ff = 1;
        for(int j = 1;j <= n;j++) ff &= (a[i][j] > 0);
        if(ff) {
            cout << -1 << '\n' ; return ;
        }
    }
    for(int i = 1;i <= n;i++) {
        if(a[1][i] && c[i]) {
            cout << -1 << '\n' ; return ;
        }
    }
    int pre = 0;
    for(int i = 1;i <= n;i++) {
        if(a[m][i] == 0) pre = i;
        f[m][i] = {pre , 0} ;
    }
    for(int i = m - 1;i >= 1;i--) {
        int pre = 0;
        int cur = 0;
        pil p = {0, -inf} ;
        for(int j = 1;j <= n;j++) {
            if(a[i][j] == 0) pre = j;
            while(cur < j) {
                cur++ ;
                p = max(p , f[i + 1][j]) ;
            }
            int cp = min(p.first , pre) ;
            f[i][j] = {cp , p.second - (s[i][j] - s[i][max(0 , cp - 1)])} ;
            // printf("%d %d : %d %lld\n",i,j,f[i][j].first , f[i][j].second);
        }
    }
    vector<vector<ll> > g(n + 1 , vector<ll>(3 , inf)) ;
    vector<vector<pil> > r(n + 1) ;
    for(int i = 1;i <=n ;i++) {
        f[1][i].second *= -1;
        if(f[1][i].first != 0) {
            r[f[1][i].first].push_back({i , f[1][i].second}); 
        }
    }
    g[0][0] = 0;
    g[0][1] = g[0][2] = inf;
    auto upd = [&](ll &x,ll y) {
        x = min(x , y);
    };
    for(int i = 0;i < n;i++) {
        g[i][0] = min(g[i][0] , g[i][2]);
        if(c[i + 1] == 0) upd(g[i + 1][0] , g[i][0]);
        upd(g[i + 1][1] , g[i][0] + a[1][i + 1]) ;
        upd(g[i + 1][2] , g[i][2] + a[1][i + 1]) ;
        upd(g[i + 1][1] , g[i][1] + a[1][i + 1]) ;
        for(auto [x,v] : r[i + 1]) {
            upd(g[x][2] , g[i][0] + v) ;
            upd(g[x][2] , g[i][1] + v) ;
            upd(g[x][2] , g[i][2] + v) ;
        }
        // printf("i %d : %lld %lld %lld\n",i,g[i][0],g[i][1],g[i][2]) ;
    }
    ll ans = min(g[n][0] , g[n][2]) ;
    if(ans > 1e17) cout << -1 << '\n' ;
    else cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(false) ; cin.tie(0) ;
    int t ; cin >> t;
    while(t--) solv() ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3484kb

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: 1ms
memory: 3592kb

input:

1
1 1
1
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 62ms
memory: 3788kb

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:

6752
0
10413
9051
269
1929
0
0
2916
11547
5480
0
0
0
5657
0
5698
0
1667
5203
15
0
0
3821
0
0
2787
0
0
0
0
5491
0
0
656
0
14166
0
3580
4368
11430
3464
0
2265
2558
1967
5546
4361
803
0
2782
0
4382
11377
1963
0
0
135
0
0
642
0
0
0
48
0
720
900
6367
0
0
6830
79
9425
7445
20
0
2256
0
0
0
6783
1383
115
99...

result:

wrong answer 46th numbers differ - expected: '1479', found: '1967'