比赛 |
20110412 |
评测结果 |
AAAAAAAAAT |
题目名称 |
山顶问题 |
最终得分 |
90 |
用户昵称 |
Pom |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2011-04-12 10:47:18 |
显示代码纯文本
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAXN=100000;
const int oo=100000000;
struct peak
{
int start,cost;
}P[MAXN];
int n,ans,a[MAXN],i,k1,k2,j,k,K,m=0,MIN,t,sum=0,xiao;
bool used[MAXN];
inline int cmp(const void *p,const void *q)
{
return ((peak*)p)->cost-((peak*)q)->cost;
}
int cal(int i)
{
int j,k;
int ans=0;
for (j=P[i].start+1;a[j]==a[P[i].start];) ++j;
--j;
t=oo;
for (k1=P[i].start-1;k1>=1;k1--)
if (a[k1]<a[k1-1]) break;
for (k2=j+1;k2<=n;k2++)
if (a[k2]<a[k2+1]) break;
if (!k1) MIN=a[k2];
if (!k2) MIN=a[k1];
if (k1 && k2) MIN=max(a[k1],a[k2]);
for (k=k1;k<=k2;k++)
if (a[k]>MIN)
ans+=a[k]-MIN;
return ans;
}
int main()
{
freopen("peaks.in","r",stdin);
freopen("peaks.out","w",stdout);
scanf("%d%d",&n,&K);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=a[n+1]=-oo;
for (i=1;i<=n;i++)
if (a[i]>a[i-1])
{
for (j=i+1;a[j]==a[i];) ++j;
--j;
if (a[j]<a[j+1]) continue;
++m;
P[m].start=i;
}
memset(used,false,sizeof(used));
K=m-K;
for (;K>0;K--)
{
xiao=oo;
for (i=1;i<=m;i++)
if (!used[i])
{
t=cal(i);
if (t<xiao)
{
xiao=t;
k=i;
}
}
i=k;
for (j=P[i].start+1;a[j]==a[P[i].start];) ++j;
--j;
t=oo;
for (k1=P[i].start-1;k1>=1;k1--)
if (a[k1]<a[k1-1]) break;
for (k2=j+1;k2<=n;k2++)
if (a[k2]<a[k2+1]) break;
if (!k1) MIN=a[k2];
if (!k2) MIN=a[k1];
if (k1 && k2) MIN=max(a[k1],a[k2]);
for (k=k1;k<=k2;k++)
if (a[k]>MIN)
{
ans+=a[k]-MIN;
a[k]=MIN;
}
used[i]=true;
}
printf("%d\n",ans);
return 0;
}