您的位置:首页 > 其它

HOJ 3729 I'm Telling the Truth

2015-08-23 20:08 218 查看

After this year’s college-entrance exam, the teacher did a survey in his class on students’ score. There are n students in the class. The students didn’t want to tell their teacher their exact score; they only told their teacher their
rank in the province (in the form of intervals).

After asking all the students, the teacher found that some students didn’t tell the truth. For example, Student1 said he was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4
said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.



Input
There is an integer in the first line, represents the number of cases (at most 100 cases). In the first line of every case, an integer n (n <= 60) represents the number of students. In the next n lines of every case, there are 2 numbers
in each line, Xi and Yi (1 <= Xi <= Yi <= 100000), means the i-th student’s rank is between Xi and Yi, inclusive.



Output
Output 2 lines for every case. Output a single number in the first line, which means the number of students who told the truth at most. In the second line, output the students who tell the truth, separated by a space. Please note
that there are no spaces at the head or tail of each line. If there are more than one way, output the list with maximum lexicographic. (In the example above, 1 2 3;1 2 4;1 3 4;2 3 4 are all OK, and 2 3 4 with maximum lexicographic)


Sample Input
2
4
5004 5005
5005 5006
5004 5006
5004 5006
7
4 5
2 3
1 2
2 2
4 4
2 3
3 4




Sample Output
3
2 3 4
5
1 3 5 6 7






题目大意:

输出最大数量的说真话的同学,并按照最大的字典序2输出.

思路:比赛的时候没想到用二分匹配来做。这道题可以看作n位同学为左集合,所对应的区间为右集合。然后在最大可能区间内进行匹配。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL __int64
#define inf 0x3f3f3f3f
bool g[100][100010];
int link[100010],use[100010];
int ans[100010],sum;
int l,r;
int a,b;
using namespace std;
bool dfs(int v)
{
    int i,j,k;
    for( i=l; i<=r; i++ )//在最大可能的区间进行增广
    {
        if(!use[i]&&g[v][i])
        {
            use[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=v;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int n,m,i,j,cla;
    scanf("%d",&cla);
    while(cla--)
    {
        scanf("%d",&n);
        sum=0;
        memset(g,false,sizeof(g));
        memset(link,-1,sizeof(link));
        l=0x3f3f3f3f;
        r=-1;
        for(i=1; i<=n; i++)
        {
            scanf("%d%d",&a,&b);
            if(l>a )//分别找到最大区间的界限
                l=a;
            if(r<b)
                r=b;
            for(j=a; j<=b; j++)//匹配可能的情况
                g[i][j]=true;
        }
        for(i=n; i>=1; i--)//由于按照字典序大的输出所以从编号大的同学开始
        {
            memset(use,0,sizeof(use));
            if(dfs(i))
            {
                ans[sum++]=i;
            }
        }
        printf("%d\n",sum);
        for(i=sum-1; i>=0; i--)
            i==0?printf("%d\n",ans[i]):printf("%d ",ans[i]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: