1. 程式人生 > >upc組隊賽16 Winner Winner【位運算】

upc組隊賽16 Winner Winner【位運算】

nts ask orm bits enter likely 從大到小 yellow con

Winner Winner

題目鏈接

題目描述

The FZU Code Carnival is a programming competetion hosted by the ACM-ICPC Training Center of Fuzhou University. The activity mainly includes the programming contest like ACM-ICPC and strive to provide participants with interesting code challenges in the future.
Before the competition begins, YellowStar wants to know which teams are likely to be winners. YellowStar counted the skills of each team, including data structure, dynamic programming, graph theory, etc. In order to simplify the forecasting model, YellowStar only lists M skills and the skills mastered by each team are represented by a 01 sequence of length M. 1 means that the team has mastered this skill, and 0 does not.

If a team is weaker than other teams, this team cannot be a winner. Otherwise, YellowStar thinks the team may win. Team A(a1, a2, ..., aM ) is weaker than team B(b1, b2, ..., bM ) if ?i ∈ [1, M], ai ≤ bi and ?i ∈ [1, M], ai < bi.
Since YellowStar is busy preparing for the FZU Code Carnival recently, he dosen’t have time to forecast which team will be the winner in the N teams. So he asks you to write a program to calculate the number of teams that might be winners.

輸入

Input is given from Standard Input in the following format:

輸出

Print one integer denotes the number of X.

樣例輸入

3 3
2 5 6

樣例輸出

2

題解

通過位運算 從大到小遍歷(因為大的一定會留下來),把有比當前值對應二進制各個位置小的(即0)值打上記號(改為-1)。

代碼

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
int a[1<<20];
int n,m,x;
int main()
{
    sca2(n,m);
    int mx = -1;
    rep(i,0,n)
    {
     sca(x);
     a[x]++;
     mx = max(x,mx);
    }
    int ans = 0;
    for(int i = mx;i>=0;i--)
    {
        if(a[i])
        {
            if(a[i]>0) ans+=a[i];
            for(int j=m-1;j>=0;j--)
            {
                if(i&(1<<j))
                    a[i^(1<<j)] = -1; //標記第j位取反的數
            }
        }
    }
    pri(ans);
    return 0;
}

upc組隊賽16 Winner Winner【位運算】