您的位置:首页 > 其它

[BZOJ2151] 种树 贪心

2018-02-28 17:57 357 查看

2151: 种树

Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 1151 Solved: 613
[Submit][Status][Discuss]

Description

A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树。园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n。并且每个位置都有一个美观度Ai,如果在这里种树就可以得到这Ai的美观度。但由于A城市土壤肥力欠佳,两棵树决不能种在相邻的位置(i号位置和i+1号位置叫相邻位置。值得注意的是1号和n号也算相邻位置!)。最终市政府给园林部门提供了m棵树苗并要求全部种上,请你帮忙设计种树方案使得美观度总和最大。如果无法将m棵树苗全部种上,给出无解信息。

Input

输入的第一行包含两个正整数n、m。第二行n个整数Ai。

Output

输出一个整数,表示最佳植树方案可以得到的美观度。如果无解输出“Error!”,不包含引号。

Sample Input

【样例输入1】

7 3

1 2 3 4 5 6 7

【样例输入2】

7 4

1 2 3 4 5 6 7

Sample Output

【样例输出1】

15

【样例输出2】

Error!

【数据规模】

对于全部数据:m<=n;

-1000<=Ai<=1000

N的大小对于不同数据有所不同:

数据编号 N的大小 数据编号 N的大小

1 30 11 200

2 35 12 2007

3 40 13 2008

4 45 14 2009

5 50 15 2010

6 55 16 2011

7 60 17 2012

8 65 18 199999

9 200 19 199999

10 200 20 200000

HINT

Source

每次在优先队列中找到美观度最大的点并删除与它相连的点后,将它本身的权值a[i]=a[pre[i]]+a[nxt[i]]-a[i],并再加入到优先队列中,重复操作m次。

1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #include<queue>
8 #define maxn 200005
9 using namespace std;
10 int n,m;
11 int a[maxn];
12 int nxt[maxn],bef[maxn];
13 struct data {
14     int pos,v;
15     bool operator <(const data tmp) const {
16         return v<tmp.v;
17     }
18 };
19 priority_queue<data> q;
20 int ans;
21 bool vis[maxn];
22 int main() {
23     scanf("%d%d",&n,&m);
24     for(int i=1;i<=n;i++) {
25         scanf("%d",&a[i]);
26         nxt[i]=i+1;bef[i]=i-1;
27         q.push((data){i,a[i]});
28     }
29     if(m>n/2){puts("Error!");return 0;  }
30     nxt
=1;bef[1]=n;
31     for(int i=1;i<=m;i++) {
32         data h;
33         while(vis[q.top().pos]) q.pop();
34         h=q.top();q.pop();
35         ans+=h.v;
36         a[h.pos]=a[nxt[h.pos]]+a[bef[h.pos]]-a[h.pos];
37         vis[nxt[h.pos]]=vis[bef[h.pos]]=1;
38         nxt[h.pos]=nxt[nxt[h.pos]];
39         bef[nxt[h.pos]]=h.pos;
40         bef[h.pos]=bef[bef[h.pos]];
41         nxt[bef[h.pos]]=h.pos;
42         q.push((data){h.pos,a[h.pos]});
43     }
44     printf("%d\n",ans);
45 }


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: