首页 > 代码库 > CodeForces 723D Lakes in Berland

CodeForces 723D Lakes in Berland

连通块。

求出连通块,排序即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<ctime>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-10;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}struct X{    int id,c;}s[200000],ss[200000];int sz,cnt,n,m,k;char Map[60][60];int f[60][60],t[100000],h[100000];bool check(int a,int b){    if(a<0||a>=n) return 0;    if(b<0||b>=m) return 0;    if(Map[a][b]==*) return 0;    if(f[a][b]!=0) return 0;    return 1;}void dfs(int a,int b){    f[a][b]=sz;    s[sz].c++;    if(check(a+1,b)) dfs(a+1,b);    if(check(a-1,b)) dfs(a-1,b);    if(check(a,b+1)) dfs(a,b+1);    if(check(a,b-1)) dfs(a,b-1);}bool cmp(X a, X b){    return a.c<b.c;}int main(){    cin>>n>>m>>k;    for(int i=0;i<n;i++) cin>>Map[i];    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(f[i][j]!=0) continue;            if(Map[i][j]==*) continue;            sz++; s[sz].id=sz; dfs(i,j); h[sz]=1;        }    }    for(int i=0;i<n;i++)    {        if(f[i][0]!=0) h[f[i][0]]=0;        if(f[i][m-1]!=0) h[f[i][m-1]]=0;    }    for(int j=0;j<m;j++)    {        if(f[0][j]!=0) h[f[0][j]]=0;        if(f[n-1][j]!=0) h[f[n-1][j]]=0;    }    for(int i=1;i<=sz;i++)    {        if(h[i]==0) continue;         cnt++;        ss[cnt].c=s[i].c;        ss[cnt].id=s[i].id;    }    sort(ss+1,ss+1+cnt,cmp);    /*    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            printf("%d ",f[i][j]);        }        cout<<endl;    }    */    int ans=0;    for(int i=1;i<=cnt-k;i++)    {        ans=ans+ss[i].c;        t[ss[i].id]=1;    }    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(t[f[i][j]]==1)            {                Map[i][j]=*;            }        }    }    printf("%d\n",ans);    for(int i=0;i<n;i++) cout<<Map[i]<<endl;    return 0;}

 

CodeForces 723D Lakes in Berland