【Bzoj2151】种树
来源:互联网 发布:耳环饰品淘宝推荐 编辑:程序博客网 时间:2024/06/05 22:49
题意
A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树。园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n。并且每个位置都有一个美观度Ai,如果在这里种树就可以得到这Ai的美观度。但由于A城市土壤肥力欠佳,两棵树决不能种在相邻的位置(i号位置和i+1号位置叫相邻位置。值得注意的是1号和n号也算相邻位置!)。最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。
解析
这题有一点特殊的就是不能种在相邻位置,对于连续的i-1,i,i+1。我们要不就选i,要不就同时选i-1和i+1。然后我们可以建一个优先队列,每次取完a[x]后,放入a[pre[x]] + a[nxt[x]] - a[x]。以后选到这个的话,就相当于取了旁边两个。也就是说把三个打包看做一个。具体删点之类的用双向链表实现。
#include <queue>#include <cstdio>#include <algorithm>#define Rep( i , _begin , _end ) for(int i=(_begin);i<=(_end);i++)#define For( i , _begin , _end ) for(int i=(_begin);i!=(_end);i++)using std :: sort;using std :: pair;using std :: priority_queue;const int maxx = 200000 + 25;typedef pair<int,int> aii;priority_queue< aii > quq;int n,m,x,y,z,ans;int pre[maxx],nxt[maxx],a[maxx];bool vis[maxx];void remove(int x){ vis[x] = true; int l = pre[x],r = nxt[x]; nxt[l] = r;pre[r] = l; pre[x] = nxt[x] = 0;}void Get(){ while(vis[quq.top().second]) quq.pop(); int x = quq.top().second;quq.pop(); ans += a[x];a[x] = a[pre[x]] + a[nxt[x]] - a[x]; remove(pre[x]);remove(nxt[x]); quq.push(aii(a[x],x));}int main(){ scanf("%d%d",&n,&m); Rep( i , 1 , n ) scanf("%d",&a[i]); if(m > n/2) {puts("Error!");return 0;} Rep( i , 1 , n ) pre[i] = i-1,nxt[i] = i+1; nxt[n] = 1;pre[1] = n; Rep( i , 1 , n ) quq.push(aii(a[i],i)); Rep( i , 1 , m ) Get(); printf("%d",ans); return 0;}
0 0
- [bzoj2151]种树
- [BZOJ2151]种树
- bzoj2151 种树
- BZOJ2151 种树
- bzoj2151 种树
- 【Bzoj2151】种树
- 【bzoj2151】种树
- 【bzoj2151】【种树】【堆+贪心】
- 【国家集训队2011】【BZOJ2151】种树
- [BZOJ2151]种树(贪心+堆)
- 【BZOJ2151】种树,贪心+Splay乱搞
- bzoj2151[国家集训队2011] 种树
- BZOJ2151 种树 贪心+双向链表+优先队列
- 种树
- 种树
- [堆]种树
- 种树问题
- 蓝桥杯 种树
- 模拟select下拉框,解决ie9以下不支持替换小箭头
- ORACLE使用row_number() over(...)对查询数据进行分组并排序
- WebStorm设置JSX
- 关于Java wait()方法
- 基于软件的负载均衡(Nginx与ZooKeeper解析)
- 【Bzoj2151】种树
- AngularJS 核心概览
- .net 解析Transfer-Encoding:chunked 秒掉网上无用方案
- java 整合redis缓存 SSM 后台框架 rest接口 shiro druid maven
- 加载本地HTML文件
- Java基本数据类型
- zookeeper入门系列-理论基础-分布式事务
- 二叉树
- 赛普拉斯 cypress Cyapi使用心得