hdu 1678 优先队列

本文提供了一种使用优先队列解决特定类型问题的方法,具体步骤包括排序物品并按照一定规则进行选择。通过实例演示,阐述了算法的实现过程及效率。

水题,理解题意后不难想到,只要对所有物品排个序,然后依次从小到大,三个三个地拿即可。

/*
* hdu1678/win.cpp
* Created on: 2011-10-1
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}

void work() {
int T;
scanf("%d", &T);
while (T--) {
int n, temp, ans = 0;
scanf("%d", &n);
priority_queue<int, vector<int>, greater<int> > pq;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
pq.push(temp);
}
if (n % 3 != 0) {
pq.pop();
n--;
}
if (n % 3 != 0) {
pq.pop();
n--;
}
while (!pq.empty()) {
ans += pq.top();
pq.pop();
pq.pop();
pq.pop();
}
printf("%d\n", ans);
}
}



转载于:https://www.cnblogs.com/moonbay/archive/2011/10/01/2197762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值