登录  /  注册
Codeforces Round #268 (Div. 1)-B. Two Sets_html/css_WEB-ITnose
php中文网
发布: 2016-06-24 11:57:22
原创
864人浏览过

B. Two Sets

time limit per test 1 second

memory limit per test 256 megabytes

Little X has n distinct integers: p1,?p2,?...,?pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied:

  • If number x belongs to set A, then number a?-?x must also belong to set A.
  • If number x belongs to set B, then number b?-?x must also belong to set B.
  • Help Little X divide the numbers into two sets or determine that it's impossible.

    Input

    The first line contains three space-separated integers n,?a,?b (1?≤?n?≤?105; 1?≤?a,?b?≤?109). The next line contains n space-separated distinct integers p1,?p2,?...,?pn (1?≤?pi?≤?109).

    Output

    If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1,?b2,?...,?bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B.

    If it's impossible, print "NO" (without the quotes).

    Sample test(s)

    Input

    4 5 92 3 4 5
    登录后复制

    Output

    YES0 0 1 1
    登录后复制

    Input

    3 3 41 2 4
    登录后复制

    Output

    NO
    登录后复制

    Note

    It's OK if all the numbers are in the same set, and the other one is empty.


    意解:这道题有很多的解法,我是用BFS做的,但是比赛因为一点小错误fail system text. 我把a - x 不存在的暂时确定为b

    集合,存进队列,然后BFS,但是要注意一个问题,但当前的数配对的数是a集合里面的话,我们就要把a集合里面的那

    个数取到b集合,如果没发取的,则输出"NO",详细看代码吧!

    AC代码:

    #include #include #include #include using namespace std;const int M = 1e5 + 10;mapms;queuels;int ma[M];int main(){    int n,a,b;    scanf("%d %d %d",&n,&a,&b);    for(int i = 0; i < n; i++)    {        scanf("%d", ma + i);        ms[ma[i]] = 1;    }    for(int i = 0; i < n; i++)        if(!ms[a - ma[i]]) ls.push(ma[i]);    while(!ls.empty())    {        int u = ls.front(),tp;        ls.pop();        if(ms[u] > 0 && ms[a - u] == 0 && ms[b - u] == 0)        {            puts("NO");            return 0;        }        --ms[u]; //表示已被取到b集合去了;        --ms[b - u];        tp = a - b + u;        if(ms[tp]) ls.push(tp);  //如果我所取的那个数是a集合里的,在bfs把另一个数取出来.    }    puts("YES");    for(int i = 0; i < n; i++)        if(ms[ma[i]] == 1) printf("0 ");        else printf("1 ");    puts("");    return 0;}
    登录后复制


    相关标签:
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

     | 本站CDN由 数掘科技 提供

    登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学