萌新求助优先队列+反悔贪心
查看原帖
萌新求助优先队列+反悔贪心
738306
chasing_dream楼主2022/7/18 14:42
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    register int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=(x<<3)+(x<<1)+(c^48); 
        c=getchar();
    }
    return x*f;
}
const int maxn=5e5+10;
int n,m,a[maxn],sum=0;
bool ai[maxn*10];
priority_queue<pair<int,int> >q;
int main()
{
    int n=read();
    for(int i=1;i<=n;i++) a[i]=read(),q.push(make_pair(i,a[i]));
    int cnt=1;
    while(cnt<=m)
    {
        if(q.top().second<=0) break;
        if(ai[q.top().first])
        {
            q.pop();
            continue;
        }
        cnt++;
        sum+=q.top().second;
        ai[q.top().first+1]=ai[q.top().first-1]=1;
        q.push(make_pair(5000001,a[q.top().first-1]+a[q.top().first+1]-a[q.top().first]));
        q.pop();
    }
    cout<<sum;
    return 0;
}

根据有一篇题解的思路写的

2022/7/18 14:42
加载中...