文章

14

粉丝

49

获赞

0

访问

2.0k

头像
q.top()
我要提问
发布于2024年2月29日 22:44
阅读数 183

priority_queue<int,vector<int>,greater<int>> q;

int weight+=q.top();

第二行语法有问题吗?

登录查看完整内容


登录后发布评论

3 条评论
snake VIP
2024年2月29日 23:06

语法可以的,不过应该加的不是q.top()而是temp2

因为这个是优先队列,不是说temp2入栈就会在栈顶,这里要注意

赞(0)

yanmo : 回复 snake: 好的,蟹蟹

2024年2月29日 23:25
yanmo VIP
2024年2月29日 22:45

//完整代码,1544合并果子

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,temp1,temp2;
    int weight=0;
    cin>>n;
    priority_queue<int,vector<int>,greater<int>> q;
    for(int i=0;i<n;i++){
        cin>>temp1;
        q.push(temp1);
    }
    for(int i=1;i<=n-1;i++){
        int a=q.top();
        q.pop();
        int b=q.top();
        q.pop();
        temp2=a+b;
        q.push(temp2);
        weight+=q.top();
    }
    cout<<weight<<endl;
}

赞(0)