首页 > 搜索 > DFS搜索 > HDU 3729-I’m Telling the Truth-分治-[解题报告]HOJ
2015
02-21

HDU 3729-I’m Telling the Truth-分治-[解题报告]HOJ

I’m Telling the Truth

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

3
2 3 4
5
1 3 5 6 7

I’m Telling the Truth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1033    Accepted Submission(s): 510

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.
 
 

 

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

一个水的二分匹配题,我用的是DFS找增广路算法:

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 #include <algorithm>
 #include <string.h>
 #include <set>
 using namespace std;
 typedef struct abcd
 {
     int x,y;
 }abcd;
 abcd a[100];
 int b[100001];
 int c[100001];
 set<int> s;
 int dfs(int t)
 {
     int i;
     for(i=a[t].x;i<=a[t].y;i++)
     {
         if(!c[i])
         {
             c[i]=1;
             if(!b[i]||dfs(b[i]))
             {
                 b[i]=t;
                 s.insert(t);
                 return 1;
             }
         }
     }
     return 0;
 }
 int main()
 {
     int n,m,i,j;
     cin>>n;
     for(i=0;i<n;i++)
     {
         s.clear();
         memset(b,0,sizeof(b));
         cin>>m;
         for(j=1;j<=m;j++)
         {
             cin>>a[j].x>>a[j].y;
         }
         int sum=0;
         for(j=m;j>0;j--)
         {
             memset(c,0,sizeof(c));
             if(dfs(j))
             sum++;
         }
         cout<<sum<<endl;
         set<int> ::iterator it;
         for(it=s.begin();it!=s.end();it++)
         {
             sum--;
             if(sum)
             cout<<(*it)<<' ';
             else cout<<(*it)<<endl;
         }
     }
 }

 

 

参考:http://www.cnblogs.com/ERKE/archive/2013/08/14/3256957.html


  1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。

  2. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”