BZOJ 2151 种树
来源:互联网 发布:佳能wifi软件app 编辑:程序博客网 时间:2024/06/10 11:34
Description
A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树。园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n。并且每个位置都有一个美观度Ai,如果在这里种树就可以得到这Ai的美观度。但由于A城市土壤肥力欠佳,两棵树决不能种在相邻的位置(i号位置和i+1号位置叫相邻位置。值得注意的是1号和n号也算相邻位置!)。最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。
【题目分析】
可修正的贪心。
【代码】
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct node{ int pos; int val;}testa,testb;struct cmp{ bool operator () (const node & a,const node & b) {return a.val<b.val;}};priority_queue <node,vector<node>,cmp> q;int n,m,ans=0;int pre[200001],nxt[200001],a[200001],del[200001];void de(int x){ del[x]=1; nxt[pre[x]]=nxt[x]; pre[nxt[x]]=pre[x]; nxt[x]=pre[x]=0;}void add(){ while (del[q.top().pos]) q.pop(); int x=q.top().pos;ans+=a[x];// printf("%d %d %d\n",x,pre[x],nxt[x]); q.pop(); a[x]=a[pre[x]]+a[nxt[x]]-a[x]; de(pre[x]); de(nxt[x]); node xx; xx.pos=x; xx.val=a[x]; q.push(xx);}int main(){ scanf("%d%d",&n,&m); if (m>n/2) { printf("Error!\n"); return 0; } for (int i=1;i<=n;++i) {scanf("%d",&a[i]);} for (int i=1;i<n;++i) nxt[i]=i+1; nxt[n]=1; for (int i=2;i<=n;++i)pre[i]=i-1; pre[1]=n; for (int i=1;i<=n;++i) { node xx; xx.pos=i; xx.val=a[i]; q.push(xx); } for (int i=1;i<=m;++i) add(); printf("%d\n",ans);}
0 0
- bzoj-2151 种树
- bzoj 2151 种树
- BZOJ 2151 种树 贪心
- BZOJ 2151 种树
- bzoj 2151 种树(贪心+堆)(经典)
- BZOJ 2151 种树 贪心+优先队列+HASH
- 【BZOJ 2151】 种树|堆|贪心|链表
- bzoj 2151: 种树(贪心+堆+链表)
- bzoj 2151: 种树 贪心+优先队列
- BZOJ 2151 种树 贪心 双向链表优化
- 2151: 种树
- 2151: 种树
- 种树
- 种树
- [堆]种树
- 种树问题
- [bzoj2151]种树
- [BZOJ2151]种树
- 原码、补码其实很简单
- 嵌入式系统基础知识及常用接口终结
- [LeetCode] 104. Maximum Depth of Binary Tree
- lcd fb参数
- 洛谷 P1134 阶乘问题
- BZOJ 2151 种树
- npp主题切换
- java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener错误解决方案
- ubuntu16.04 安装apache2报错 解决方案
- 系统架构设计方法论
- 树、森林和二叉树的转换
- 前端面试中常见的算法问题总结
- <poj-1068>Parencodings
- [Vijos1055]奶牛浴场(极大子矩形)