问题描述
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;
}
本文介绍了一种解决环形数组中选取不相邻元素以获得最大和的问题算法。通过使用大根堆存储数组元素及其索引,并维护左右邻居索引的方式,确保每次选择元素后更新相邻元素状态及生成新的待选元素。
356

被折叠的 条评论
为什么被折叠?



