hiho week 136(二分+优先队列)
2017-02-11 14:35
197 查看
题目链接:http://hihocoder.com/contest/hiho136/problem/1
题意:中文题诶~
思路:直接对缓存区长度二分就好了,注意计算当前长度的"延迟惩罚值"我们可以用优先队列。
时间复杂度为O(nlogn)
代码:
题意:中文题诶~
思路:直接对缓存区长度二分就好了,注意计算当前长度的"延迟惩罚值"我们可以用优先队列。
时间复杂度为O(nlogn)
代码:
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 5 const int MAXN=1e5+10; 6 ll a[MAXN], key; 7 int n; 8 9 bool geloutingyu(int k){ 10 ll ans=0; 11 int cc=1; 12 priority_queue<int, vector<int>, less<int> > q; 13 for(int i=0; i<k; i++){ 14 q.push(a[i]); 15 } 16 for(int i=k; i<n; i++){ 17 ans+=q.top()*(cc++); 18 q.pop(); 19 q.push(a[i]); 20 } 21 while(!q.empty()){ 22 ans+=q.top()*(cc++); 23 q.pop(); 24 } 25 if(ans>key){ 26 return false; 27 }else{ 28 return true; 29 } 30 } 31 32 int main(void){ 33 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); 34 cin >> n >> key; 35 for(int i=0; i<n; i++){ 36 cin >> a[i]; 37 } 38 int l=1, r=n, gg, mid=(l+r)>>1; 39 bool flag=true; 40 while(mid>=1&&mid<=n&&l<r){ 41 if(geloutingyu(mid)){ 42 flag=false; 43 gg=mid; 44 r=mid; 45 }else{ 46 l=mid+1; 47 } 48 mid=(l+r)>>1; 49 } 50 cout << gg << endl; 51 return 0; 52 }
相关文章推荐
- hihocoder 136 #1269 优化延迟 二分+优先队列
- hihoCoder--1469 优化延迟(二分+优先队列)
- uva 1422 - Processor(二分+优先队列)
- hiho 1269 优化延迟 二分 优先队列
- UVALive - 4254 Processor 二分+优先队列
- UVA.136 Ugly Numbers (优先队列)
- UVA 136 Ugly Number(优先队列)
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 - Processor(二分+优先队列+贪心)
- UVA 1422 - Processor (二分+贪心+优先队列)
- Uva 136 Ugly Numbers 优先队列
- CF-#402D String Game(二分+优先队列)
- LA 4254 Processor(二分+贪心+优先队列)
- uva1422 - Processor 二分+优先队列
- uva 1422 - Processor(二分+优先队列)
- UVA 1422 Processor(二分极大值极小化+优先队列)
- UVA136 - Ugly Numbers 优先队列
- UVa 1422 - Processor(二分 + 贪心 + 优先队列)
- UVA 1422 Processor(优先队列+贪心+二分)
- HihoCoder 1269 优化延迟(二分、优先队列)