atcoder E - Jigsaw(思维)

题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_e

 

题解:这题很巧妙运用了cnt[i]抽象的表示了上下互补的状态,最后上面突出的一定要处理完。下面突出的可以多。而且最后要处理一下下面多的情况不能自己左右互补具体看一下代码。

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int M = 1e5 + 10;
int f[600] , cnt[600] , c[600];
vector<int>vc[600];
void init() {
    for(int i = 0 ; i <= 555 ; i++) {
        cnt[i] = 0;
        c[i] = 0;
        f[i] = i;
    }
}
int find(int x) {
    if(x == f[x]) return x;
    int tmp = find(f[x]);
    return f[x] = tmp;
}
int main() {
    int n , h;
    scanf("%d%d" , &n , &h);
    init();
    for(int i = 1 ; i <= n ; i++) {
        int a , b , c , d;
        scanf("%d%d%d%d" , &a , &b , &c , &d);
        int x = c , y = -d;
        if(c == 0) x = -a;
        if(d == 0) y = b;
        x += 250 , y += 250;
        vc[x].push_back(y);
        cnt[x]++ , cnt[y]--;
        f[find(x)] = find(y);
    }
    int flag = 1;
    for(int i = 0 ; i <= 500 ; i++) {
        if(i > 250 && cnt[i] > 0) flag = 0;//大于250的表示上面突出的上面突出的不能多。
        if(i < 250 && cnt[i] < 0) flag = 0;//小于250的表示下面突出的下面突出的不能少。
        if(cnt[i] > 0) {
            c[find(i)] = 1;//确保不是左右互补
        }
    }
    for(int i = 0 ; i <= 500 ; i++) {
        if(vc[i].size() > 0 && !c[find(i)]) flag = 0;
    }
    flag ? puts("YES") : puts("NO");
    return 0;
}

转载于:https://www.cnblogs.com/TnT2333333/p/7149953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值