1. 程式人生 > >E - 今年暑假不AC HDU - 2037

E - 今年暑假不AC HDU - 2037

個數 一行 數據 () end 開始時間 行數 最短 clu

“今年暑假不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

其實道理還是簡單,但是一開始思路不好找,我做了近兩個小手,還是不明所以,最後還是借鑒的麻佬(隔壁好基友)的,主要思路就是找到進行時間最短的那個,然後以它為基準找開始時間比結束時間大

的開始時間,找到一個,計數加一,以此類推,代碼:

#include<iostream>
#include<cstring>
#include
<string> #include<sstream> #include<algorithm> using namespace std; int n,ma,cnt; struct te{ int sta,sto; }t[105]; int cmp(te a,te b){ if(a.sta<b.sta)return a.sta<b.sta; return a.sto<b.sto; } // //int dfs(int a,int b,int c){ // // for(int i=c;i<=n;i++){ // // if(t[i].sta>=b&&i<n-1){
// // cnt++; // if() // dfs(t[i].sta,t[i].sto,i+1); // } //else //if(i==n-1&&t[i].sta>=b){ // // cnt++; // // cout<<"####"<<cnt<<endl; // if(ma<cnt) // {ma=cnt;} // cnt=0; // break; // } // //} // return ma; //} int main(){ int a,b; while(cin>>n&&n){ cnt=0; memset(t,0,sizeof(t)); for(int i=0;i<n;i++ ){ cin>>a>>b; t[i].sta=a; t[i].sto=b; } sort(t,t+n,cmp); struct te a=t[0]; cnt=1; for(int i=1;i<n;i++){ // if(t[i].sta>=t[i-1].sto)cnt++; if(a.sta<=t[i].sta&&a.sto>=t[i].sto) { cnt--; a=t[i]; cnt++; //cout<<a.sta<<" "<<a.sto<<" "<<cnt<<endl; } else if(t[i].sta>=a.sto) { cnt++; a=t[i]; //cout<<a.sta<<" "<<a.sto<<" "<<cnt<<endl; } } cout<<cnt<<endl; } }

E - 今年暑假不AC HDU - 2037