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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值