I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1190 Accepted Submission(s): 610
Problem Description
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.
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, X
i
and Y
i
(1 <= X
i
<= Y
i
<= 100000), means the i-th student’s rank is between X
iand Y
i, 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
Source
Recommend
1 //187MS 1024K 1113 B C++ 2 /* 3 4 题意: 5 给出n个学生的排名范围,问最多有多少个可以实现(为真) 6 7 二分匹配: 8 匈牙利算法. 9 我承认我犯傻了...我开始用map以为可以降低时间复杂度, 10 后来发现不行,会超时,后来看别人思路原来可以用结构体存储 11 每个学生的排名范围,然后直接在该范围内深搜就行,的确可以 12 降低数据较散时的时间复杂度. 13 14 */ 15 #include<iostream> 16 #include<map> 17 #include<algorithm> 18 using namespace std; 19 struct node{ 20 int l; 21 int r; 22 }a[65]; 23 int vis[100005]; 24 int match[100005]; 25 int n,m; 26 int dfs(int x) 27 { 28 for(int i=a[x].l;i<=a[x].r;i++){ 29 if(!vis[i]){ 30 vis[i]=1; 31 if(match[i]==-1 || dfs(match[i])){ 32 match[i]=x; 33 return 1; 34 } 35 } 36 } 37 return 0; 38 } 39 void hungary() 40 { 41 memset(match,-1,sizeof(match)); 42 int ret=0; 43 for(int i=n;i>=1;i--){ 44 memset(vis,0,sizeof(vis)); 45 if(dfs(i)) ret++; 46 } 47 48 printf("%d\n",ret); 49 int ans[65]; 50 int id=0; 51 for(int i=1;i<100005;i++){ 52 //printf("*%d %d\n",i,match[i]); 53 if(match[i]>0) ans[id++]=match[i]; 54 } 55 sort(ans,ans+id); 56 for(int i=0;i<id;i++) 57 printf(i==id-1?"%d\n":"%d ",ans[i]); 58 } 59 int main(void) 60 { 61 int t,l,r; 62 scanf("%d",&t); 63 while(t--) 64 { 65 scanf("%d",&n); 66 for(int i=1;i<=n;i++) 67 scanf("%d%d",&a[i].l,&a[i].r); 68 hungary(); 69 } 70 return 0; 71 }