【NOIP2010】【贪心】【覆盖问题】T4 引水入城 题解

这里写图片描述
这里写图片描述

第一行贪心选点,最后一行判断覆盖,中间BFS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cctype>
#include <algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define ms(a,x) memset(x,a,sizeof(x))
#define LL long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int n,m;
int a[505][505],mp[505][505],l[505][505],r[505][505];
int qx[250005],qy[250005];

struct data { int l,r; } c[505];

inline bool operator < (data a,data b) { return a.l == b.l ? a.r < b.r : a.l < b.l; }

void bfs(int b[505][505], int x, int y, int v, bool f) {
    int head = 0, tail = 1;
    qx[0] = x; qy[0] = y; b[x][y] = v;
    while(head != tail) {
        int x = qx[head], y = qy[head++];
        for(int k = 0; k < 4; k++) {
            int nowx = x+dx[k], nowy = y+dy[k];
            if(nowx < 1 || nowy < 1 || nowx > n || nowy > m || b[nowx][nowy]) continue;
            if(!f && a[nowx][nowy] >= a[x][y]) continue;
            if(f == 1 && a[nowx][nowy] <= a[x][y]) continue;
            b[nowx][nowy] = b[x][y];
            qx[tail] = nowx;
            qy[tail++] = nowy;
        }
    }
}

int main() {
    freopen("flow.in","r",stdin);
    freopen("flow.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d",&a[i][j]);
    for(int i = 1; i <= m; i++) bfs(mp, 1, i, 1, 0);
    int tot = 0;
    for(int i = 1; i <= m; i++) if(!mp[n][i])tot++;
    if(tot) { printf("0\n%d\n",tot); return 0; }
    for(int i = 1; i <= m; i++) if(!l[n][i]) bfs(l, n ,i, i, 1);
    for(int i = m; i ; i--) if(!r[n][i]) bfs(r, n, i, i, 1);
    for(int i = 1; i <= m; i++) {
        if(!l[1][i]) l[1][i] = r[1][i];
        if(!r[1][i]) r[1][i] = l[1][i];
        c[i].l = l[1][i]; c[i].r = r[1][i];
    }
    int now = 0,to = 0;
    sort(c+1, c+m+1);
    for(int i = 1; i <= m; i++)
        if(now+1 >= c[i].l) to = max(to, c[i].r);
        else now = to, to = max(to, c[i].r), tot++;
    if(now != m) tot++;
    printf("1\n%d\n",tot);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值