代码求条
查看原帖
代码求条
322792
Alea楼主2024/9/1 21:17

inf:

2
5 5
2 9 5 1 3 
1 1 10 6 4 
6 10 5 1 3 
4 1 3 8 1 
1 3 1 6 5 
7
2 2 7 7 7 8 8 
5 5
8 8 3 8 8 
1 7 4 9 10 
3 10 6 6 9 
9 2 9 8 4 
3 5 9 5 4 
7
4 4 5 6 7 8 9 

ans:

2 2 4 4 4 3 3 
2 2 2 4 5 4 2 

感谢!

// UVa1665 - Islands
// Code by Alexandre Lea
#include <bits/stdc++.h>
using namespace std;
const int N=1005,Q=100005;
const int mov[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int h[N][N],g[N][N],n,m,tot,h_[N*N],S[N*N];
int fi[N*N],q,qu[Q],ans[Q],x[N*N],y[N*N];
int fn(int x){
    return fi[x]=fi[x]==x?fi[x]:fn(fi[x]);
}
void solve(){
    tot=0;
    cin>>n>>m;
    for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){
        cin>>h[i][j],g[i][j]=++tot,fi[tot]=tot,S[tot]=1,h_[g[i][j]]=h[i][j];
        x[g[i][j]]=i,y[g[i][j]]=j;
    }
    cin>>q;
    for(int i=1;i<=q;++i) cin>>qu[i];
    for(int _=q;_>=1;--_){
        if(qu[_]==qu[_+1]){
            ans[_]=ans[_+1];
            continue;
        }
        // for(int i=1;i<=n*m;++i) cerr<<S[i];
        // for(int i=1;i<=n*m;++i) cerr<<" "<<fi[i];
        // cerr<<endl;
        for(int u=1;u<=n*m;++u) if(S[u]&&h_[u]>qu[_]){
            int i=x[u],j=y[u];
            for(int c=0;c<4;++c){
                int ii=i+mov[c][0],jj=j+mov[c][1];
                if(1<=ii&&ii<=n&&1<=jj&&jj<=m&&h[ii][jj]>qu[_]){
                    int v=fn(g[ii][jj]);
                    if(u!=v) fi[v]=u,S[v]=0;
                }
            }
        }
        ans[_]=0;
        for(int i=1;i<=n*m;++i) if(S[i]&&h_[i]>qu[_]) ++ans[_];
        // for(int i=1;i<=n*m;++i) cerr<<S[i];
        // for(int i=1;i<=n*m;++i) cerr<<" "<<fi[i];
        // cerr<<endl<<endl;
    }
    // cerr<<endl;
    for(int i=1;i<=q;++i) cout<<ans[i]<<" ";
    cout<<endl;
}
int main(){
    int _;
    cin>>_;
    while(_--) solve();
    return 0;
}

ouf:

4 4 4 4 4 3 3
3 3 3 4 5 4 2
2024/9/1 21:17
加载中...