BZOJ 1029: [JSOI2007]建築搶修(貪心)

來源:互聯網
上載者:User

標籤:stream   images   ima   分享   color   while   bsp   pll   set   

http://www.lydsy.com/JudgeOnline/problem.php?id=1029

題意:

 

思路:
先按T2排序,並維護一個T1的優先隊列,然後對於每個建築,如果當前這個建築能修的話,那麼就修。如果不能修,那麼就從優先隊列中彈出之前的建築中所需修複最長的時間,如果該時間比我當前建築時間還長並且去掉它後就能修理當前建築,那麼我們就放棄那個,改為修理當前建築,因為這樣一來總的時間會更小。

 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #include<vector> 7 #include<stack> 8 #include<queue> 9 #include<cmath>10 #include<map>11 #include<set>12 using namespace std;13 typedef long long ll;14 typedef pair<int,ll> pll;15 const int INF = 0x3f3f3f3f;16 const int maxn=50000+5;17 18 int n;19 20 struct node21 {22     int cost,end;23 }a[150005];24 25 bool cmp(node a, node b)26 {27     return a.end<b.end;28 }29 30 bool operator<(node& a, node& b)31 {32     return a.cost>b.cost;33 }34 35 int main()36 {37     //freopen("in.txt","r",stdin);38     while(~scanf("%d",&n))39     {40         for(int i=1;i<=n;i++)   scanf("%d%d",&a[i].cost,&a[i].end);41         sort(a+1,a+1+n,cmp);42         priority_queue<int> Q;43 44         int t=0;45         int ans=0;46         for(int i=1;i<=n;i++)47         {48             if(t+a[i].cost<=a[i].end)   {ans++;t+=a[i].cost;Q.push(a[i].cost);}49             else50             {51                 if(t-Q.top()+a[i].cost<=a[i].end && Q.top()>=a[i].cost)52                 {53                     t=t-Q.top()+a[i].cost;54                     Q.pop();55                     Q.push(a[i].cost);56                 }57             }58         }59         printf("%d\n",ans);60     }61     return 0;62 }

 

BZOJ 1029: [JSOI2007]建築搶修(貪心)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.