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;
}

猜你喜欢

转载自blog.csdn.net/erikabeats/article/details/78169151