hdu 3729 I'm Telling the Truth
I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1377 Accepted Submission(s): 700
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, 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
二分匹配,注意一下建边就可以了
1 #include<string> 2 #include<cstdio> 3 #include<iostream> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<cstring> 9 #include<stdlib.h> 10 #include<string> 11 #include<cmath> 12 using namespace std; 13 #define pb push_back 14 #define ll __int64 15 struct node{ 16 int x,y; 17 }p[100]; 18 int n,cx[100010],vit[100010]; 19 int Find(int pos){ 20 for(int i=p[pos].x;i<=p[pos].y;i++) 21 if(!vit[i]){ 22 vit[i]=1; 23 if(cx[i]==0||Find(cx[i])){ 24 cx[i]=pos; 25 return 1; 26 } 27 } 28 return 0; 29 } 30 void solve(){ 31 int cnt=0; 32 memset(cx,0,sizeof(cx)); 33 for(int i=n;i>=1;i--){ 34 memset(vit,0,sizeof(vit)); 35 if(Find(i)) cnt++; 36 } 37 cout<<cnt<<endl; 38 priority_queue<int>ko; 39 stack<int>ok; 40 for(int i=1;i<=100000;i++) 41 if(cx[i]) ko.push(cx[i]); 42 cnt=0; 43 while(!ko.empty()){ 44 ok.push(ko.top()); 45 ko.pop(); 46 } 47 while(!ok.empty()){ 48 if(cnt ) cout<<" "; 49 cnt=1; 50 cout<<ok.top(); 51 ok.pop(); 52 } 53 cout<<endl; 54 } 55 int main(){ 56 int t;cin>>t; 57 while(t--){ 58 cin>>n; 59 for(int i=1;i<=n;i++) 60 cin>>p[i].x>>p[i].y; 61 solve(); 62 } 63 }