QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#795021#9800. Crisis Event: Meteoriteucup-team3161#WA 122ms100004kbC++173.7kb2024-11-30 17:29:102024-11-30 17:29:10

Judging History

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

  • [2024-11-30 17:29:10]
  • 评测
  • 测评结果:WA
  • 用时:122ms
  • 内存:100004kb
  • [2024-11-30 17:29:10]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,ll>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f3f3f3f3f

int n,m,c[maxn];

struct node{
    int l,r,w;
};
vector<node>f[maxn];
vi a[maxn],sum[maxn];

int ss[maxn];
int pre[maxn],suf[maxn];

pii tol[maxn];

vector<pii>buc[maxn];
int dp[maxn][2];
void cmin(int &x,int y){
    x=min(x,y);
}

int qr[maxn],qw[maxn];
void wk(vector<node>&vec){
    For(i,1,n) qr[i]=inf;
    for(auto [l,r,w]:vec){
        if(r<qr[l]) qr[l]=r,qw[l]=w;
    }
    vector<node>qwq;
    For(i,1,n) if(qr[i]!=inf) qwq.pb({i,qr[i],qw[i]});
    vec=qwq;
}

void work()
{
    cin>>n>>m;
    For(i,0,m){
        a[i].resize(n+1);
        sum[i].resize(n+1);
    }

    For(i,0,m+1) f[i].clear();
    For(i,0,n) buc[i].clear();

    For(i,1,n)cin>>c[i];
    For(i,1,m){
        For(j,1,n){
            cin>>a[i][j];
            sum[i][j]=sum[i-1][j]+a[i][j];
        }
    }
    For(i,1,n)
        if(a[m][i]==0){
            f[m].pb({i,i,sum[m][i]});
        //    cout<<"lst "<<i<<" "<<sum[m][i]<<"\n";
        }
    Rep(t,m-1,1){
     //   cout<<"solve "<<t<<"\n";
        For(i,1,n){
            pre[i]=pre[i-1];
            if(a[t][i]==0)pre[i]=i;
        }
        Rep(i,n,1){
            suf[i]=suf[i+1];
            if(a[t][i]==0)suf[i]=i;
        }

        For(i,1,n) ss[i]=ss[i-1]+sum[t][i];

        vector<node>vec;
        auto add=[&](int l,int r,int w){
            vec.pb({l,r,w});
        };
        
        for(auto [l,r,w]:f[t+1]){
            if(pre[l]) {
                int w2=ss[l-1]-ss[pre[l]-1];
                add(pre[l],r,w2+w);
            }
            if(suf[r]){
                int w2=ss[suf[r]]-ss[r];
                add(l,suf[r],w2+w);
            }
        }

        if(!vec.size()){
            puts("-1");
            return;
        }

        
        sort(vec.begin(),vec.end(),[&](node a,node b){
            return mkp(a.l,a.r)<mkp(b.l,b.r);
        });

        
       /* for(auto [l,r,w]:vec){
            cout<<"l,r,w "<<l<<" "<<r<<" "<<w<<"\n";
        }*/
        
        For(i,0,SZ(vec)-1){
            int mn=inf;
            int j=i;
            while(j+1<SZ(vec) && mkp(vec[i].l,vec[i].r)==mkp(vec[j+1].l,vec[j+1].r)) ++j;
            For(k,i,j) mn=min(mn,vec[k].w);
            i=j;
            f[t].pb({vec[i].l,vec[i].r,mn});
        }
        
        wk(f[t]);
    }

    for(auto [l,r,w]:f[1]){
        buc[l].pb(mkp(r,w));
    }
    
    For(i,0,n+1) For(j,0,2) dp[i][j]=inf;
    dp[0][0]=0;
    For(i,1,n){
        if(c[i] && a[1][i]){
            puts("-1");
            return;
        }
    }
    For(i,1,n){
        cmin(dp[i][2],dp[i-1][2]+a[1][i]);
        cmin(dp[i][1],dp[i-1][1]+a[1][i]);
        cmin(dp[i][0],min(dp[i-1][0],dp[i-1][1]));
        for(auto [r,w]:buc[i]) {
            cmin(dp[r][1],min(dp[i-1][0],dp[i-1][2])+w);
        }
        if(c[i]) dp[i][0]=inf; 
    }
    int res=inf;
    For(i,0,2) res=min(res,dp[n][i]);
    if(res==inf)puts("-1");
    else cout<<res<<"\n";
}

signed main()
{
    int T;cin>>T;
    while(T--)work();
    return 0;
}
/*
1
3 4
1 0 1
0 1 0
2 0 0
0 0 3
4 5 0
*/

详细

Test #1:

score: 100
Accepted
time: 23ms
memory: 100004kb

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: 18ms
memory: 98904kb

input:

1
1 1
1
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 122ms
memory: 100004kb

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:

-1
0
-1
-1
269
1929
0
0
2916
-1
5480
0
0
0
5657
0
5698
0
1667
6808
-1
0
0
3821
0
0
-1
0
0
0
0
5491
0
0
1248
0
14166
0
-1
4368
11430
-1
0
2265
-1
1479
-1
5479
1280
0
3422
0
4382
11377
-1
0
0
-1
0
0
642
0
0
0
48
0
2591
900
-1
0
-1
6830
2096
-1
7445
20
0
2256
0
0
0
6783
-1
582
-1
3458
2164
5051
0
0
749...

result:

wrong answer 1st numbers differ - expected: '6752', found: '-1'