P3102取数nkoj 堆的应用

问题描述

n个整数组成的一个环,现在要从中取出m个数,取走一个数字就不能取跟它相邻的数字(相邻的数不能同时取)。要求取出的数字的总和尽可能大,问这个最大和是多少? 如果无解,请输出“Error!”

输入格式

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

输出格式

仅一个整数,表示所求结果。如果无解输出“Error!”,不包含引号。

样例输入

8 4
8 5 6 2 3 4 8 9

样例输出

25

题解

我们先把每个数字以及它的编号拿入堆中。建大根堆。并记录下每个数左右元素的编号。L[k],R[k]。
最后要输出的答案是ans。初始化为0。
然后每次从堆里面拿出堆顶元素,ans+这个堆定元素的值。
假设我们当前取出的堆顶元素的编号为k。我们现在新生成一个数P[i]=P[L[k]]+P[R[k]]-P[k]。
编号为i的数左边的数的编号我们赋为L[L[k]]。右边的数赋为R[R[k]]。
然后R[R[k]]的左边是i。L[L[k]]右边是i。( 类似于链表的操作)

ps:注意数组范围

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include <queue>
#include<cstring>
using namespace std;
#define maxn 200005
struct node{
    int a,x;
};
bool operator<(node a,node b){return a.a<b.a;}
priority_queue<node>q;
int i,ans,n,m,x,y,tot;
int tp;
int a[maxn*10],l[maxn*10],r[maxn*10];
bool mark[maxn*10];
node temp,tmp;
int main()
{
    int i,j,k;
    scanf("%d%d",&n,&m);
    if(n<m*2) {cout<<"Error!";return 0;};
    for(i=1;i<=n;i++){scanf("%d",&a[i]);}
    l[1]=n;r[1]=2;temp.a=a[1];temp.x=1;q.push(temp);
    l[n]=n-1;r[n]=1;temp.a=a[n];temp.x=n;q.push(temp);
    for(i=2;i<n;i++)
    {
        l[i]=i-1;r[i]=i+1;temp.a=a[i];temp.x=i;
        q.push(temp);
    }
     tot=n;
    for(i=1;i<=m;i++)
    {
        tmp=q.top();
        q.pop();
        tp=tmp.x;
        if(mark[tp]){i--;continue;}

        ans+=tmp.a;tot++;

        a[tot]=a[l[tp]]+a[r[tp]]-a[tp];
        mark[l[tp]]=mark[r[tp]]=mark[tp]=true;

        r[l[l[tp]]]=l[r[r[tp]]]=tot;
         l[tot]=l[l[tp]];r[tot]=r[r[tp]];

         temp.a=a[tot]; temp.x=tot;
        q.push(temp);
    }
    cout<<ans;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值