集合问题

链接:https://ac.nowcoder.com/acm/contest/77/D
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

给你a,b和n个数p[i],问你如何分配这n个数给A,B集合,并且满足:

若x在集合A中,则a-x必须也在集合A中。

若x在集合B中,则b-x必须也在集合B中。

输入描述:

第一行 三个数 n a b  1<=n<=1e5  1<=a,b<=1e9
第二行 n个数 p1 p2 p3...pn 1<=pi<=1e9

输出描述:

如果可以恰好分开就输出第一行 YES
然后第二行输出 n个数 分别代表pi 是哪个集合的  0 代表是A集合 1代表是B 集合
不行就输出NO
放在哪个集合都可以的时候优先放B

示例1

输入

复制

4 5 9
2 3 4 5

输出

复制

YES
0 0 1 1

示例2

输入

复制

3 3 4
1 2 4

输出

复制

NO
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=5e3+5;
const int N=2e5+5;
int per[N],n,a,b,s[N];

int findd(int x){
    if(x==per[x])
        return x;
    return per[x]=findd(per[x]);
}

void U(int x,int y){
    int xx=findd(x);
    int yy=findd(y);
    if(xx!=yy)
        per[xx]=yy;
}
map<int,int>mapp;
int main(){
    scanf("%d %d %d",&n,&a,&b);
    for(int i=0;i<=n+1;i++) per[i]=i;

    for(int i=1;i<=n;i++){
        scanf("%d",&s[i]);
        mapp[s[i]]=i;
    }

    for(int i=1;i<=n;i++){
        if(mapp[b-s[i]])
            U(i,mapp[b-s[i]]);
        else
            U(0,mapp[s[i]]);
        if(mapp[a-s[i]])
            U(i,mapp[a-s[i]]);
        else
            U(n+1,mapp[s[i]]);
    }
    if(findd(0)==findd(n+1))
        printf("NO\n");
    else{
        printf("YES\n");
        int g=findd(0),c=0;
        for(int i=1;i<=n;i++){
            if(findd(i)==g)
                printf(c++==0?"0":" 0");
            else
                printf(c++==0?"1":" 1");
        }
        printf("\n");
    }
    return 0;
}

1557 两个集合

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 40 分
  6.  
  7. 4 级题

小X有n个互不相同的整数: p1,p2,...,pnp1,p2,...,pn 。他想把这些整数分到两个集合A和B里边。但是要符合下面两个条件。

·        如果x属于A,那么a-x也肯定属于A。

·        如果x属于B,那么b-x也肯定属于B。

判断一下是否存在一种方案来分配这些数字到集合A,B中。

注意:如果一个集合为空也是可以的。


 收起

输入

单组测试数据。
第一行有三个整数n,a,b (1≤n≤10^5; 1≤a,b≤10^9)。
第二行有n个不一样的整数 p1,p2,...,pn (1≤pi≤10^9).

输出

如果可行,那么输出YES,否则输出NO。

输入样例

样例输入1
4 5 9
2 3 4 5

输出样例

样例输出1
YES
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=5e3+5;
const int N=2e5+5;
int per[N],n,a,b,s[N];

int findd(int x){
    if(x==per[x])
        return x;
    return per[x]=findd(per[x]);
}

void U(int x,int y){
    int xx=findd(x);
    int yy=findd(y);
    if(xx!=yy)
        per[xx]=yy;
}
map<int,int>mapp;
int main(){
    scanf("%d %d %d",&n,&a,&b);
    for(int i=0;i<=n+1;i++) per[i]=i;

    for(int i=1;i<=n;i++){
        scanf("%d",&s[i]);
        mapp[s[i]]=i;
    }

    for(int i=1;i<=n;i++){
        if(mapp[a-s[i]])
            U(i,mapp[a-s[i]]);
        else
            U(0,mapp[s[i]]);
        if(mapp[b-s[i]])
            U(i,mapp[b-s[i]]);
        else
            U(n+1,mapp[s[i]]);
    }
    if(findd(0)==findd(n+1))
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值