codefoces 868C(二进制,状态压缩)

C. Qualification Rounds
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.

k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.

Determine if Snark and Philip can make an interesting problemset!

Input
The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 4) — the number of problems and the number of experienced teams.

Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.

Output
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.

You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

Examples
input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
output
NO
input
3 2
1 0
1 1
0 1
output
YES
Note
In the first example you can't make any interesting problemset, because the first team knows all problems.

In the second example you can choose the first and the third problems.


题意:题库中有n道题,有k队参加比赛,每一队赛前都知道题库中的部分题,问能不能找出题库的一个非空集合使得,这个集合中的每一队知道的题目数量不超过集合大小的一半。


题解:第一个卡的地方就是分析,题目可以简化为找到2个题目。可以用反证法证明这一点,如果选择的比两个多符合题意,那么选择两个自然也可以了,反之,如果选择的2个都不行的,那么选择再多也是没用的。第二个地方是,既然题目这样简化了,但是介于题目数量的原因还是不能暴力枚举啊,那么这里可以采用状态压缩,虽然题目有1e5

个,但是状态不过16种,所以可以采用二进制转化为10进制,然后求按位与的结果就可以了。


代码:

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<utility>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1000;
const int mod = 1e9+7;
const int INF = 1<<30;
const ll llINF = 1e18+999;

int n, k, Hash[16];
int main( )
{
    //freopen("input.txt", "r", stdin);
    while(~scanf("%d%d", &n, &k))
    {
        memset(Hash, 0, sizeof(Hash));
        //对于每一个问题的所知状态的2进制形式转化为10进制,状态压缩
        for(int i=0; i<n; i++)
        {
            int num = 0, t;
            for(int j=0; j<k; j++)
            {
                scanf("%d", &t);
                num += (1<<j)*t;
            }
            Hash[num] = 1;
        }
        int flag = 0;
        for(int i=0; i<16; i++)
            for(int j=0; j<16; j++)
                if((i&j)==0 && Hash[i] && Hash[j])
                    flag = 1;
        if(flag)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值