Link: http://acm.hdu.edu.cn/showproblem.php?pid=2037
今年暑假不AC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45038 Accepted Submission(s): 24139
Problem Description
“今年暑假不AC?”
“是的。”
“那你幹什麼呢?”
“看世界盃呀,笨蛋!”
“@#$%^&*%...”
確實如此,世界盃來了,球迷的節日也來了,估計很多ACMer也會拋開電腦,奔向電視了。
作爲球迷,一定想看盡量多的完整的比賽,當然,作爲新時代的好青年,你一定還會看一些其它的節目,比如新聞聯播(永遠不要忘記關心國家大事)、非常6+7、超級女生,以及王小丫的《開心辭典》等等,假設你已經知道了所有你喜歡看的電視節目的轉播時間表,你會合理安排嗎?(目標是能看盡量多的完整節目)
Input
輸入數據包含多個測試實例,每個測試實例的第一行只有一個整數n(n<=100),表示你喜歡看的節目的總數,然後是n行數據,每行包括兩個數據Ti_s,Ti_e (1<=i<=n),分別表示第i個節目的開始和結束時間,爲了簡化問題,每個時間都用一個正整數表示。n=0表示輸入結束,不做處理。
Output
對於每個測試實例,輸出能完整看到的電視節目的個數,每個測試實例的輸出佔一行。
Sample Input
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
Sample Output
5
Author
lcy
Source
ACM程序設計期末考試(2006/06/07)
Recommend
lcy | We have carefully selected several similar problems for you: 1050 1009 1051 1789 1257
解釋
網上大多數講的是簡單貪心,其實我感覺這個題的最樸素的思想是DAG,每次選的節目都會對後面的節目選擇加上限制,因此我們不妨從後往前看,每次看的這一個必然是選擇這個之後的可選擇的最大的一個,weight表示選擇這個後還可以選的個數。這樣便不會造成影響。
Code
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn(105);
int n;
struct node
{
int Ti_s,Ti_e;
int weight;
bool operator < (const node&a)const
{
if(this->Ti_s==a.Ti_s)
return this->Ti_e<a.Ti_e;
return this->Ti_s<a.Ti_s;
}
}program[maxn];
int main()
{
while(scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
{
scanf("%d%d",&program[i].Ti_s,&program[i].Ti_e);
program[i].weight=1;
}
sort(program+1,program+n+1);
int ans=1;
for(int i=n;i>=1;i--)
{
int res=0;
for(int j=i+1;j<=n;j++)
{
if(program[i].Ti_e<=program[j].Ti_s)
res=max(res,program[j].weight);
}
program[i].weight=res+1;
ans=max(ans,program[i].weight);
}
cout<<ans<<endl;
}
return 0;
}