ICPC Central Europe Regional Contest 2019 F. Light Emitting Hindenburg(二進制)

Lothar is organizing a concert tour of his friends’ rock band. The tour will take place in November and each day there will be at most one concert. The tour will be very representativeand many musicians are willing to take part in it. The number of musicians in the tour isstrictly prescribed and cannot be changed. Each concert on the tour must be attended by allthe musicians taking part in the tour.

The good news for Lothar is that the number of candidate musicians is at least as big as theprescribed number of musicians in the tour. The bad news is that a typical musician is not available during the whole month and that various musicians’ schedules diffffer a lot from each other.

Long ago, Lothar wrote a core of a computer scheduling system, and he is exploiting it now to organize the tour. He repeatedly and somewhat randomly chooses a group of musicians of prescribed size, and lets the system calculate an acceptable tour schedule. The system dependson a very specifific data format. The schedules of musicians and the tour schedules are represented as numerical codes. The days in November are labeled by their numbers in the month:1, 2, . . . , 30.

For a given musician, each November day is assigned a particular numerical code. A day with label L is coded by integer 2^{30-L}2
30−L
if the musician is available on that day. Otherwise, the day iscoded by 0. The musician schedule code is the sum of all his or her day codes.

For a given group of musicians, each November day is assigned a particular numerical code. Aday with label L is coded by integer 2^{30-L}2
30−L
if all musicians in the group are available on thatday. Otherwise, the day is coded by 0. The group availability code is the sum of all day codesof the group.

For many additional subtle reasons, Lothar thinks that the best tour would be the one with thehighest possible value of the availability code of the group of musicians taking part in it.

Input Specification
The first line contains two integers N, K (1 ≤ K ≤ N ≤ 2 · 10^5)N,K(1≤K≤N≤2⋅10
5
) . NN is the number of available musicians, KK is the prescribed number of musicians taking part in the tour. The next line contains a sequence of NN positive integers. Each integer in the sequence represents a code of a schedule of one musician. The codes are listed in arbitrary order.

Output Specification
Print the best possible availability code of any group of K musicians.

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入1複製
5 2
6 15 9 666 1
樣例輸出1複製
10
樣例解釋1
Choose 15 666 to get the answer

樣例輸入2複製
8 4
13 30 27 20 11 30 19 10
樣例輸出2複製
18
樣例解釋2
Choose 30 27 30 19 to get the answer

題意:
一個月有30天,要求辦音樂會且至少有k個藝術家。每個藝術家空閒的天數用二進制表示(第i位爲1代表第i天空閒)。要求怎樣選擇k個藝術家使得辦音樂會的天數對應二進制和最大。

思路:
肯定要從最高位選起。那麼假設對於第i個二進制位有至少k個數爲1,則選取這幾個數,之後只用考慮這幾個數了。往後同理。

#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

const int maxn = 2e5 + 7;

int a[maxn];
int vis[maxn];

int main() {
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    
    int ans = 0;
    for(int i = 30;i >= 0;i--) {
        int cnt = 0;
        for(int j = 1;j <= n;j++) {
            if(vis[j]) continue;
            if(a[j] & (1 << i)) {
                cnt++;
            }
        }
        if(cnt >= m) {
            ans |= (1 << i);
            for(int j = 1;j <= n;j++) {
                if(vis[j]) continue;
                if(!(a[j] & (1 << i))) {
                    vis[j] = 1;
                }
            }
        }
    }
    
    printf("%d\n",ans);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章