蓝桥杯 种树
来源:互联网 发布:网络电玩游mg123.com 编辑:程序博客网 时间:2024/06/14 09:50
算法提高 种树
时间限制:1.0s 内存限制:256.0MB
种树
问题描述
A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树。园林部门 得到指令后,初步规划出n个种树的位置,顺时针编号1到n。并且每个位置都有一个美观度Ai,如果在这里种树就可以得到这Ai的美观度。但由于A城市土壤 肥力欠佳,两棵树决不能种在相邻的位置(i号位置和i+1号位置叫相邻位置。值得注意的是1号和n号也算相邻位置!)。
最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。
最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。
输入格式
输入的第一行包含两个正整数n、m。
第二行n个整数Ai。
第二行n个整数Ai。
输出格式
输出一个整数,表示最佳植树方案可以得到的美观度。如果无解输出“Error!”,不包含引号。
样例输入
7 3
1 2 3 4 5 6 7
1 2 3 4 5 6 7
样例输出
15
样例输入
7 4
1 2 3 4 5 6 7
1 2 3 4 5 6 7
样例输出
Error!
数据规模和约定
对于全部数据,满足1<=m<=n<=30;
其中90%的数据满足m<=n<=20
-1000<=Ai<=1000
其中90%的数据满足m<=n<=20
-1000<=Ai<=1000
#include <iostream>#include <stdio.h>#include<vector>#include<queue>#include<algorithm>#define N 200010using namespace std;struct Data{ int i, ai;};struct cmp{ bool operator()(Data &t1, Data &t2) { return t1.ai < t2.ai; }};priority_queue<Data,vector<Data>, cmp> q;int n, m;int a[N];int pre[N], next[N];bool visit[N];void del(int k){ visit[k] = true; int temp1 = pre[k], temp2 = next[k]; pre[k] = next[k] = -1; next[temp1] = temp2; pre[temp2] = temp1;}void chuli(Data k){ Data data; data.ai = a[pre[k.i]] + a[next[k.i]] - k.ai; a[k.i] = data.ai; data.i = k.i; q.push(data); del(pre[k.i]); del(next[k.i]);}int main(){ scanf("%d%d", &n, &m); int i; for (i = 0; i < n; i++) scanf("%d", &a[i]); if (m > n / 2) { printf("Error!\n"); return 0; } for (i = 0; i < n - 1; i++) next[i] = i + 1; next[n - 1] = 0; for (i = 1; i < n; i++) pre[i] = i - 1; pre[0] = n - 1; for (i = 0; i < n; i++) { Data data; data.i = i; data.ai = a[i]; q.push(data); } int res = 0; while (m--) { while (!q.empty() && visit[q.top().i]) q.pop(); Data data = q.top(); q.pop(); res += data.ai; //printf("%d\n", res); //getchar(); chuli(data); } printf("%d\n", res); return 0;}
0 0
- 蓝桥杯 种树
- 种树
- 种树
- 蓝桥杯算法提高—种树问题
- [堆]种树
- 种树问题
- [bzoj2151]种树
- [BZOJ2151]种树
- bzoj2151 种树
- BZOJ2151 种树
- codevs1342 种树
- bzoj2151 种树
- 【Bzoj2151】种树
- 2151: 种树
- 2151: 种树
- 【bzoj2151】种树
- 我想去种树
- 几种树形菜单
- 配置文件Python报错
- 微信支付—— 扫码支付
- SPOJ DQUERY 区间内不同数的个数 主席树
- LINUX SIGNAL信号标志详解
- POI--word2003/2007转文本
- 蓝桥杯 种树
- ruby下css文件无法通过编译
- android百度地图 添加覆盖物Marker与InfoWindow的使用
- (一)Linux网络编程--网络知识介绍
- WP Super Cache <=1.4.2 存储型XSS漏洞分析
- 逆波兰表达式
- CentOS 6.5 下 WebDAV 配置
- 世上竟有此女子,她把自己的穿衣搭配都画了下来
- Windows下使用socket,连接之前用非阻塞方式,连接成功后用阻塞方式