USACO 2017 January Contest, Silver Problem 1. Cow Dance Show 二分+优先队列 普及/提高-
USACO 2017 January Contest, Silver Problem 1. Cow Dance Show 二分+优先队列+贪心
题目链接:
http://www.usaco.org/index.php?page=viewproblem2&cpid=690
P3611 [USACO17JAN]Cow Dance Show S https://www.luogu.com.cn/problem/P3611
二分+优先队列+贪心
1 #include<iostream> 2 #include<utility> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 const int N=1000010; 7 int n,d[N],t; 8 priority_queue<int,vector<int>,greater<int> > q; 9 bool check(int mid) {//mid块场地,计算时间 10 for(int i=1; i<=mid; i++) { 11 q.push(d[i]); 12 } 13 for(int i=mid+1; i<=n; i++) { 14 int k=q.top()+d[i]; 15 if(k>t) return false; 16 q.pop(); 17 q.push(k); 18 } 19 for(int i=1; i<=mid; i++) { 20 int tt=q.top(); 21 q.pop(); 22 } 23 return q.top()<=t; 24 } 25 26 int binary_search() { 27 int l=1,r=n;//最小一块场地,最大n块场地 28 while(l<r) { 29 int mid=(l+r)/2; 30 if(check(mid)) r=mid; 31 else l=mid+1; 32 } 33 return l; 34 } 35 36 bool cmp(const int x,const int y) { 37 return x<y; 38 } 39 40 int main() { 41 scanf("%d %d",&n,&t); 42 for(int i=1; i<=n; i++) { 43 scanf("%d",&d[i]); 44 } 45 cout<<binary_search();46 return 0; 47 }