2151: 种树
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 626 Solved: 347
[ 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
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
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define N 200003
#define pa pair<int,int>
using namespace std;
int n,m;
int l[N],r[N],mark[N],a[N],ans;
priority_queue<pa,vector<pa> > p;
bool solve(int k)
{
bool f=false;
while (!p.empty())
{
int t=p.top().second;
if (!mark[t]) {
f=true;
break;
}
p.pop();
}
if (!f) return false;
int x=p.top().second; p.pop();
ans+=a[x];
a[x]=a[l[x]]+a[r[x]]-a[x];
mark[l[x]]=mark[r[x]]=1;
int pre=l[x],next=r[x];
l[x]=l[pre]; r[x]=r[next]; r[l[pre]]=x; l[r[next]]=x;
p.push(make_pair(a[x],x));
return true;
}
int main()
{
freopen("compile.in","r",stdin);
freopen("compile.out","w",stdout);
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++)
{
l[i]=i-1;
r[i]=i+1;
}
l[1]=n; r[n]=1;
if (m>(n/2))
{
printf("Error!\n");
return 0;
}
for (int i=1;i<=n;i++)
p.push(make_pair(a[i],i));
for (int i=1;i<=m;i++)
if (!solve(i)) {
printf("Error!\n");
return 0;
}
printf("%d\n",ans);
}