BZoj 1293 生日礼物(小顶堆)
来源:互联网 发布:2016网络歌曲合集下载 编辑:程序博客网 时间:2024/04/30 02:49
传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27400
Anayse:小顶堆;
1 1 5 - > 1 3 5 -> 3 5 7 -> 5 7 8
pop(1) p(1) p(3) 最优解。
每次剔除堆顶,然后换push与堆顶相同种类的珠子,由于单调,,距离会被缩短,于是找出最优解。详见上式。
push() log(n)
so,复杂度 nlog(n).
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<queue>#include<deque>#include<stack>#include<map>#include<set>#define INF 0x7fffffff#define SUP 0x80000000#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int N=100007;int **a;int nu[100];struct node{ int id,pos; node(int a,int b):id(a),pos(b){}; bool operator <(node a) const { return pos<a.pos; }};vector<node> aa;int main(){ int n,k; while(scanf("%d%d",&n,&k)==2) { mem(nu,0); priority_queue<int,vector<int>,less<int> > que; a=new int *[k+1]; int num; for(int i=1;i<=k;i++) { scanf("%d",&num); a[i]=new int[num+1]; nu[i]=a[i][0]=num; for(int j=1;j<=num;j++) scanf("%d",&a[i][j]); } int cnt=k; for(int i=1;i<=k;i++) //构造初始堆 { aa.push_back(node(i,a[i][1])); nu[i]=2; } int ans=INF; while(cnt<=n) //依次剔除堆顶 { sort(aa.begin(),aa.end()); ans=min(ans,aa[k-1].pos-aa[0].pos); int ii=aa[0].id; if(nu[ii]>a[ii][0]){ break; } aa[0]=node(ii,a[ii][nu[ii]++]); cnt++; } printf("%d\n",ans); } return 0;}
0 0
- BZoj 1293 生日礼物(小顶堆)
- BZOJ 1293 生日礼物(堆)
- BZOJ 1293 生日礼物 (链表)
- BZOJ 1293-生日礼物(模拟)
- bzoj 1293: [SCOI2009]生日礼物 (单调队列)
- BZOJ 1293 生日礼物
- 【bzoj 1293】[SCOI2009]生日礼物
- bzoj 1293: [SCOI2009]生日礼物
- BZOJ 1293 [SCOI2009] 生日礼物
- bzoj 1293: [SCOI2009]生日礼物
- BZOJ 1293: [SCOI2009]生日礼物
- BZOJ 1293 SCOI2009 生日礼物 堆
- bzoj 1293: [SCOI2009]生日礼物 暴力
- 【BZOJ 1293】【SCOI 2009】生日礼物
- bzoj 1293: [SCOI2009]生日礼物 STL
- [bzoj-1293][SCOI2009]生日礼物 题解
- 【bzoj 1293】[SCOI2009]生日礼物(乱搞|单调队列)
- 【BZOJ】1293 [SCOI2009]生日礼物 (这题有多种解法)
- RecyclerView,SwipeRefreshLayout,CardView最新控件的使用
- (转)android 小知识
- 家装旺季莫错过 熟记墙面装修流程
- 第6周项目-程序阅读5
- IOS开发中常用的 宏命令整理
- BZoj 1293 生日礼物(小顶堆)
- IE,FF下getElementsByName无效
- 获取指定目录下的文件名列表
- Add Binary
- Android获取手机号码
- Android中多次点击控件问题
- Aspose.Cells Excel删除行
- Hybird APP 开发 总结
- Java中高精度问题的bigdecimal解决方式