51nod_learn_greedy_活動安排2

來源:互聯網
上載者:User

標籤:

有若干個活動,第i個開始時間和結束時間是[Si,fi),同一個教室安排的活動之間不能交疊,求要安排所有活動,最少需要幾個教室? 

第一行一個正整數n (n <= 10000)代表活動的個數。第二行到第(n + 1)行包含n個開始時間和結束時間。開始時間嚴格小於結束時間,並且時間都是非負整數,小於1000000000
輸出 
一行包含一個整數表示最少教室的個數。
 輸入樣本
31 23 42 9

輸出樣本
2


其實就是求某個時間點的最大厚度...
一開始傻逼了...
想著什麼從1開始推過去...必然tle..就沒寫==
然後今天再開...我只要把開始時間和結束時間放在一起排序...從小到大
然後用一個boolean做好標記就好...
有點像海洋兄的收費站的比喻?
 1 /************************************************************************* 2     > File Name: code/51nod/learn/greedy/2.cpp 3     > Author: 111qqz 4     > Email: [email protected]  5     > Created Time: 2015年10月05日 星期一 19時24分32秒 6  ************************************************************************/ 7  8 #include<iostream> 9 #include<iomanip>10 #include<cstdio>11 #include<algorithm>12 #include<cmath>13 #include<cstring>14 #include<string>15 #include<map>16 #include<set>17 #include<queue>18 #include<vector>19 #include<stack>20 #include<cctype>21                  22 #define yn hez111qqz23 #define j1 cute111qqz24 #define ms(a,x) memset(a,x,sizeof(a))25 using namespace std;26 const int dx4[4]={1,0,0,-1};27 const int dy4[4]={0,-1,1,0};28 typedef long long LL;29 typedef double DB;30 const int inf = 0x3f3f3f3f;31 const int N=1E4+5;32 int n;33 struct Q34 {35     int t;36     bool sta;37 }q[2*N];38 39 bool cmp(Q a,Q b)40 {41     return a.t<b.t;42 }43 int main()44 {45   #ifndef  ONLINE_JUDGE 46    freopen("in.txt","r",stdin);47   #endif48 49    scanf("%d",&n);50    for ( int i = 0 ; i < 2*n ; i++)51    {52        scanf("%d",&q[i].t);53        if (i%2==0)54        {55        q[i].sta = true;56        }57        else58     {59        q[i].sta = false;60     }61    }62    sort(q,q+2*n,cmp);63    int ans = -1;64    int cnt = 0;65    for ( int i = 0 ; i < 2*n ; i++)66    {67        if (q[i].sta)68        {69         cnt++;70        }71        else72     {73         cnt--;74     }75        ans = max(ans,cnt);76    }77    printf("%d\n",ans);78   79    80  #ifndef ONLINE_JUDGE  81   fclose(stdin);82   #endif83     return 0;84 }
View Code

 


 

51nod_learn_greedy_活動安排2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.