【貪心】【堆】bzoj1029 [JSOI2007]建築搶修

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   div   sp   log   on   

按完成時限排序,一個個修複。
若當前建築花費時間+之前花費的總時間不超過時限,則ans++;
否則,從之前已修複的建築中挑一個耗時最多的,與當前建築比較,若當前建築更優,則更新ans。

 1 #include<cstdio> 2 #include<queue> 3 #include<algorithm> 4 using namespace std; 5 priority_queue<int,vector<int> >Heap; 6 struct Point{int x,y;}; 7 bool cmp(const Point &a,const Point &b){return a.y<b.y;} 8 int n,v,used,ans=1; 9 Point a[150001];10 int main()11 {12     scanf("%d",&n);13     for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);14     sort(a+1,a+n+1,cmp);15     used=a[1].x;Heap.push(a[1].x);16     for(int i=2;i<=n;i++)17       {18           if(used+a[i].x<=a[i].y)19             {20                 Heap.push(a[i].x);21                 used+=a[i].x;22                 ans++;23                 continue;24             }25           v=Heap.top();26           if(v>a[i].x)27             {28                 Heap.pop();29                 Heap.push(a[i].x);30                 used+=(a[i].x-v);31             }32       }33     printf("%d\n",ans);34 }

 

【貪心】【堆】bzoj1029 [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.