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

来源:互联网 发布:淘宝达人cps是什么意思 编辑:程序博客网 时间:2024/05/22 18:36

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 MA11 A12  A1MA21 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 55 1 3 4 22 5 3 1 42 3 1 4 52 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 32 1 32 1 32 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 0x3f3f3f3fusing 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;    }}


原创粉丝点击