Atcoder- Sports Festival(二分+暴力+思维+贪心)

11 篇文章 0 订阅
11 篇文章 0 订阅

B - Sports Festival


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

Takahashi is hosting an sports meet. There are N people who will participate. These people are conveniently numbered 1 through N. Also, there are M options of sports for this event. These sports are numbered 1 through M. Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.

Takahashi knows that Person i's j-th favorite sport is Sport Aij. Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.

Takahashi is worried that one of the sports will attract too many people. Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized. Find the minimum possible number of the participants in the sport with the largest number of participants.

Constraints

  • 1N300
  • 1M300
  • Ai1 , Ai2 ,  , AiM is a permutation of the integers from 1 to M.

Input

Input is given from Standard Input in the following format:

N M
A11 A12  A1M
A21 A22  A2M
:
AN1 AN2  ANM

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.


Sample Input 1

Copy
4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1

Sample Output 1

Copy
2

Assume that Sports 13 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport4. Here, the sport with the largest number of participants is Sport 3, with two participants. There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.


Sample Input 2

Copy
3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

Copy
3

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected. Therefore, the answer is 3.

思路:一道考验脑子冷静程度和码力的题,居然没写出来、、、,首先我们很容易想到枚举子集然后更新答案,但是显然是不行的复杂度为2^n,容易想到二分答案,但是怎么判断当前答案是否可行,这就是这道题的难点了,我们可以尝试这样的做法,对于每个人,我们从每个人喜欢程度依次下降的选起,如果此时不行,那当前的最大值的哪个项目一定是不能选的,我们把他变为0,重复这个过程,这样的话,巧妙的利用的贪心思想找出当前可选的项目,复杂度为logn*m*n*3
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<sstream>
#include<queue>
#include<set>
#include<cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int mp[300+5][300+56];
int n,m;
int idx[305];
int cnt[305];
int mp2[300+5][300+5];
int pos[305][305];
int check(int x){
    memset(cnt,0,sizeof(cnt));
    for(int i = 0;i<n;i++){
        cnt[mp2[i][idx[i]]]++;
    }
    int mx = -1;
    int res ;
    for(int i = 1;i<=m;i++){
        if(mx<cnt[i]){
        mx = max(mx,cnt[i]);
        res = i;
        }
    }
    if(mx>x)
        return -res;
    return res;
}
void print()
{
    for(int i = 0 ;i<n;i++){
        for(int j = 0;j<m;j++){
            printf("%d%c",mp2[i][j],j==m-1?'\n':' ');
        }
    }
}
int ok(int x){
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            mp2[i][j] = mp[i][j];
        }
    }
    memset(idx,0,sizeof(idx));
    for(int i = 0;i<m;i++)
    {
        int res = check(x);
        //print();
        if(res>0)
            return 1;
        for(int j = 0;j<n;j++){
            mp2[j][pos[j][-res]] = 0;
        }
        for(int j  = 0;j<n;j++){
            while(mp2[j][idx[j]]==0&&idx[j]<m){
                idx[j]++;
            }
            if(idx[j]>=m)
                return 0;
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
                scanf("%d",&mp[i][j]);
                pos[i][mp[i][j]] = j;
            }
        }
        int l = 0;
        int r = n+1;
        while(r-l>1){
            int mid = (l+r)>>1;
            if(ok(mid)){
                r = mid;
            }
            else
                l =mid;
        }
        cout<<r<<endl;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值