周赛 战域 题解(优先队列+思维+贪心)

题目链接

题目思路

将x+1后函数的差值放入优先队列,每次的话就贪心选最小的,然后再更新差值放入队列中维护。

其实说白了就是把m分为m次加1,每次都贪心选择最小的那份。

但是结构体每次使用后都要改变,每次sort都会用时间。而优先队列可以解决这个问题

优先队列是STL中比较重要的一个部分,用起来非常方便,在很多排序题中要比sort一遍一遍排序快很多。

它能根据自己定义顺序来进行排序。

主要的两种表达形式(其实还有其他的,这里就先列举两个):

第一种是用friend bool operator来写:

 #include<stdio.h>
    #include<iostream>
    using namespace std;
    #include<queue>
    
    typedef struct node{
        int num;
        friend bool operator<(struct node a,struct node b)        //这里的形式是固定的
        {return    a.num>b.num;}
    }point;

第二种是用bool operator来写。

#include<stdio.h>
    #include<iostream>
    using namespace std;
    #include<queue>
    
    typedef struct node{
        int num;
        bool operator<(const node&b)const        //这里的形式是固定的
        {return    num>b.num;}
    }point;

要注意的是上面的两种都是从小到大来排顺序!!程序里面的大于小于号返回的是相反的。

大于号是从小到大排序,小于是从大到小排序,自己定义优先级的时候一定要注意,跟其他地方的不太一样,记住就好了。

注:第二种方法更好

扫描二维码关注公众号,回复: 10556073 查看本文章

易错警示

注意就算x=0也有 c 值,一定不要忘记,wa了无数发

代码

#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
int n,m,u,v,w;
ll ans;
struct node{
    int a,b,x,num;//a,b分表代表二次和一次系数,num代表可以增加多少值
    friend bool operator<(struct node a,struct node b){//重载
        return a.num>b.num;
    }
}fx;
priority_queue<struct node> pq;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d %d %d",&u,&v,&w);
        pq.push({u,v,1,u+v});
        ans=ans+w;
    }
    while(m--){
        int a=pq.top().a,b=pq.top().b;
        int x=pq.top().x,num=pq.top().num;
        pq.pop();
        ans=ans+num;
        pq.push({a,b,x+1,a*(2*x+1)+b});
    }
    printf("%lld",ans);
    return 0;
}

参考链接https://blog.csdn.net/weixin_44592881/article/details/86660678

发布了68 篇原创文章 · 获赞 2 · 访问量 2260

猜你喜欢

转载自blog.csdn.net/m0_46209312/article/details/105031973
今日推荐