BNUOJ 44662 水题 (贪心+优先级队列)

水题

Time Limit: 500ms

Memory Limit: 32768KB

This problem will be judged on HRBUST.
Original ID: 2223

64-bit integer IO format: %lld     
Java class name: Main

Prev

Submit Status Statistics Discuss

Next

因为是有关于接水的问题,便简称为水题了(。

N个人排队在M个出水口前接水,第i个人接水需时为t[i],

请问接水的最短用时是多少?

Input

第一行一个整数 T ,代表有 T 组数据。

每组数据

第一行两个整数 N(<=100000) , M(<=10000) 代表有 N 个人 M 个出水口。

第二行N个整数,第i个数字t[i](<=10000)代表第i个人接水用时t[i]。

Output

对于每组数据输出一个整数,代表所需的最少接水时间。

Sample Input


2

5 3

1 2 3 4 5

6 3

1 2 3 3 4 5

Sample Output


5

6

Hint

小桥流水哗啦啦,我和小岛去偷瓜~。

Source

"诚德软件杯"哈尔滨理工大学第四届ACM程序设计团队赛

解析:就是一个贪心思想,就像往一个瓶子里装核桃和沙子一样,怎么样才能在有限的空间里容纳更多重量,那就是先尽可能装最多大的,让后再处理小的,同样我们这题的思想也是,尽可能的先把最大的处理了,直到处理完为止。按从大到小的顺序处理,先加入m个最大的元素进入队首为最小元素的优先级队列,然后再不断从队首取出元素(队首为队列中的最小值),让它加上当前未加入的最大元素,再放入队列中,重复上述操作,直到把所有的n-m个全部加进去即可,此时队列中的最大值(也就是队尾元素),就是答案。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define INF 0x7fffffff
#define LL long long
#define MID(a, b)  a+(b-a)/2
const int maxn = 1000 + 10;

#pragma comment(linker, "/STACK:1024000000,1024000000")

template <class T>
inline bool scan_d(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0; //EOF
    while(c!=' -' &&(c<'0' ||c>'9' )) c=getchar();
    sgn=(c==' -' )?-1:1;
    ret=(c==' -' )?0:(c-'0' );
    while(c=getchar(),c>='0' &&c<='9' ) ret=ret*10+(c-'0' );
    ret*=sgn;
    return 1;
}

inline void out(int x) {
    if(x>9) out(x/10);
    putchar(x%10+'0' );
}

int a[100005];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int t, n, m;
    scanf("%d", &t);
    while(t--){
        priority_queue<int, vector<int>, greater<int> > p;         //最小值优先的优先级队列
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; i++) scanf("%d", &a[i]);
        sort(a, a+n);
        for(int i=n-1; i>=0; i--){
            if(i >= n-m) p.push(a[i]);           //先入队m个
            else{
                int x = p.top();
                p.pop();
                p.push(x + a[i]);
            }
        }
        int ans;
        while(p.size()){
            ans = p.top();
            p.pop();
        }
        printf("%d\n", ans) ;
    }
    return 0;
}
时间: 2024-08-01 10:34:36

BNUOJ 44662 水题 (贪心+优先级队列)的相关文章

POJ 2431 Expedition (贪心 + 优先级队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7696   Accepted: 2260 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3253 Fence Repair 贪心 优先级队列

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

POJ 2431 Expedition 贪心 优先级队列

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed t

CodeForces 719B Anatoly and Cockroaches (水题贪心)

题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <c

【POJ 3614 Sunscreen】贪心 优先级队列

题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头牛用). 求这L瓶防晒液最多能让多少头牛安全地晒太阳. 思路:贪心策略,按spf从小到大或从大到小的顺序取出防晒液,供给尽可能多的剩余的牛. 具体如何判断当前这瓶防晒液最多能供给几头牛呢? 以spf从小到大排序所有防晒液为例,可以维护一个小顶堆,每取出一瓶防晒液l,就把剩余的所有min值低于l.sp

51nod 1344 走格子(水题+贪心)

1344 走格子 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 取消关注 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去.机器人有一个初始能量,每个格子对应一个整数A[i],表示这个格子的能量值.如果A[i] > 0,机器人走到这个格子能够获取A[i]个能量,如果A[i] < 0,走到这个格子需要消耗相应的能量,如果机器人的能量 < 0,就无法继续前进了.问机器人最少需要有多少初始能量,才能完成

CF 1008B Turn the Rectangles(水题+贪心)

There are n rectangles in a row. You can either turn each rectangle by 90 degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can t

HDU1009_FatMouse&amp;#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

LightOJ 1166 Old Sorting 置换群 或 贪心 水题

LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : 2017-07-20 14:45:30 * @FileName: LightOJ 1166 贪心 或 置换群 水题.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.co