Codeforces 868C Qualification Rounds

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard 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 ( 1n105,1k4 ) — 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.


初看很复杂,其实答案简单到让人怀疑人生。
这么说吧,因为1.是subset,2.对subset的大小没有要求,3.要求所有队都最多只会一半的题,那么就挑所有队要么都不会,要么只会其中一题的两题就好了。由于最多只会有4个队,于是每一题的情况最多只有16种:没有人会,只有第一个队会,只有第二个队会,只有第一和第二个队会,只有第三个队会,只有第一和第三个队会,etc……换言之就是0000 0001 0010 0011 0100 0101 etc……那么这样的两题(假设分别为a和b)必须有a^b==0,但是不可能直接两层for的,因为那是 O(n2) ,n最大可以有 105 ,这样会TLE。
于是便有了这样的策略:只要有0000,那么再来一题(无论是怎样的一题)就好;只要有0001,那么再来一题0010/0100/0110/1000/1010/1100/1110就好;etc。将题目情况转换为数字的操作,记录题目的有无都可以在输入的时候顺带进行;剩下的就 O(1) 了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
#include <iterator>
#include<set>
using namespace std;
typedef long long ll;

int n,k;
int solved[100005];
bool has[16];
int nthbit[4] = {1,2,4,8};
bool nohas(){
    for(int i=1;i<16;i++){
        if(has[i])return false;
    }
    return true;
}
int main(){
    scanf("%d%d",&n,&k);
    int A = nthbit[k]-1;
    bool flag=false;
    for(int i=0;i<n;i++){
        for(int j=0;j<k;j++){
            int a; scanf("%d",&a);
            if(a==1)solved[i]|=nthbit[j];
        }
        has[solved[i]]=true;
    }
//     for(int i=0;i<n;i++)printf("%d ",solved[i]);printf("\n");
    if(
        has[0]&&(has[1]||has[2]||has[3]||has[4]||has[5]||has[6]||has[7]||has[8]||has[9]||has[10]||has[11]||has[12]||has[13]||has[14]||has[15])
        ||
        has[1]&&(has[2]||has[4]||has[6]||has[8]||has[10]||has[12]||has[14])
        ||
        has[2]&&(has[1]||has[4]||has[8]||has[5]||has[9]||has[12]||has[13])
        ||
        has[3]&&(has[4]||has[8]||has[12])
        ||
        has[4]&&(has[1]||has[2]||has[3]||has[8]||has[9]||has[10]||has[11])
        ||
        has[5]&&(has[2]||has[8]||has[10])
        ||
        has[6]&&(has[1]||has[8]||has[9])
        ||
        has[7]&&(has[8])
        ||
        has[8]&&(has[1]||has[2]||has[3]||has[4]||has[5]||has[6]||has[7])
        ||
        has[9]&&(has[2]||has[4]||has[6])
        ||
        has[10]&&(has[1]||has[4]||has[5])
        ||
        has[11]&&(has[4])
        ||
        has[12]&&(has[1]||has[2]||has[3])
        ||
        has[13]&&(has[2])
        ||
        has[14]&&(has[1])
        ||
        nohas()
    ) {flag=true;}
    printf((flag?"YES\n":"NO\n"));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值