P1514 引水入城

我好蒟蒻呀

连区间覆盖这个贪心都不会

我的写法好水呀


启示:要转化问题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
queue<int> q;
bool visit[510][510];
int dx[5]={1,-1,0,0};
int dy[5]={0,0,-1,1};
int tot;
int n,m;
int map[510][510];
bool vis[510][510];
bool used[510];
struct LINE
{
    int l;
    int r;
};
LINE line[510];
int l;
bool check(int x,int y)
{
    if(x<=0||y<=0||x>n||y>m||vis[x][y])
        return false;
    return true;
}
void BFS()
{
    int x,y;
    while(!q.empty())
    {
        x=q.front();q.pop();
        y=q.front();q.pop();
        if(x==n)
            tot+=1;
        for(int i=0;i<=3;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(check(nx,ny)&&map[x][y]>map[nx][ny])
            {
                q.push(nx);q.push(ny);
                vis[nx][ny]=true;
            }
        }
    }
}
void bfs(int begin)
{
    l+=1;
    q.push(1);
    q.push(begin);
    vis[1][begin]=true;
    int x,y;
    line[l].l=510;
    used[begin]=true;
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        y=q.front();
        q.pop();
        if(x==n)
        {
            line[l].l=min(line[l].l,y);
            line[l].r=max(line[l].r,y);
        }
        for(int i=0;i<=3;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(check(nx,ny)&&map[x][y]>map[nx][ny])
            {
                q.push(nx);
                q.push(ny);
                vis[nx][ny]=true;
                if(nx==1)
                    used[ny]=true;
            }
        }
    }
}
struct re
{
    int h;
    int y;
};
re rep[510];
bool compare1(const re &a,const re &b)
{
    return a.h>b.h;
}
bool compare2(const LINE &a,const LINE &b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    return a.r<b.r;
}
int main()
{
    //freopen("testdata.in","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&map[i][j]);
    for(int i=1;i<=m;i++)
    {
        q.push(1);
        q.push(i);
        vis[1][i]=true;
    }
    BFS();
    if(tot==m)
    {
    /*  for(int i=1;i<=m;i++)
        {
            rep[i].h=map[1][i];
            rep[i].y=i;
        }
        sort(rep+1,rep+1+m,compare1);*/
        for(int i=1;i<=m;i++)
        {
            memset(vis,0,sizeof(vis));
            bfs(i);
            //printf("1 %d\n%d %d\n",i,line[l].l,line[l].r);
        }
        sort(line+1,line+1+l,compare2);
        tot=0;
        int now=0,nxt=1;
        /*for(int i=1;i<=l;i++)
        {
            tot+=1;
            now=max(nxt,line[i].l);
            for(;line[i].l<=now&&nxt!=m&&i<=l;i++)
                nxt=max(nxt,line[i].r);
            i-=1;
        }*/
        int last=1;
        int i=1;
        while(last<=m)/*坑爹的模板*/
        {
            int t=0;
            while(line[i].l<=last) t=max(t,line[i++].r);
            last=t+1;
            tot+=1;
        }
        printf("1\n%d",tot);
    }
    else
    {
        printf("0\n%d",m-tot);
        return 0;
    }
}

转载于:https://www.cnblogs.com/Lance1ot/p/8641507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值