Sort HDU - 5884(优先队列+二分)

Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4673    Accepted Submission(s): 1177


Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest k to make the program complete in time.
 

 

Input
The first line of input contains an integer t0, the number of test cases. t0 test cases follow.
For each test case, the first line consists two integers N (2N100000) and T (Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(i,0ai1000).
 

 

Output
For each test cases, output the smallest k.
 

 

Sample Input
1 5 25 1 2 3 4 5
 

 

Sample Output
3
 

 

Source
 
题意:
  给出n个数和一个花费c 每次合并不大于k个数 最后把这n个数合并到一起 每次合并所花的费用为k个数的和 总花费不超过c 求最大的k
解析:
  先排序 然后二分答案变为判定性问题
  每次合并k个最小的值
  第一次如果(n-1)%(k-1)  != 0 则先合并余数+1个数
  为什么是(n-1)%(k-1) 呢  因为最后剩一个数 就相当于删除了n-1个数  每次合并k个数生成1个数  就相当于删除了k-1个数
   为什么要余数+1呢  看图吧 。。。
余数+1就是加了第n个数 然后生成1个数   删除前面几个k-1的段 最后还剩一个
不过我们这里加的1不是第n个数
是余数+1大的数  只是为了形象一点 能明白吧。。。emm。。
 
所以不算余数的那次计算  一共是进行了(n-1)/(k-1)次操作
 
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 100100, INF = 0x7fffffff;
int n;
int c;
int sum[maxn], a[maxn];

bool solve(int k)
{
    int  res = 0;
    priority_queue<int, vector<int>, greater<int> > q;
    int tmp = (n-1)%(k-1);
    if(tmp > 0)
    {
        tmp++;
        res += sum[tmp];
        q.push(sum[tmp]);
    }
    rap(i, tmp+1, n)
        q.push(a[i]);
    int t = (n-1)/(k-1);
    rap(i, 1, t)
    {
        int ans = 0;
        rep(j, 0, k)
        {
            ans += q.top();
            q.pop();
        }
        res += ans;
        q.push(ans);
    }
    if(res > c) return false;
    return true;
}

int main()
{
    int T;
    rd(T);
    while(T--)
    {
        sum[0] = 0;
        rd(n); rd(c);
        rap(i, 1, n)
        {
            rd(a[i]);
        }
        sort(a+1, a+n+1);
        rap(i, 1, n)
            sum[i] = sum[i-1] + a[i];
        int l = 2, r = n;
        while(l <= r)
        {
            int mid = l + (r - l) / 2;
            if(solve(mid)) r = mid - 1;
            else l = mid + 1;
        }
        if(n <= 1) l = 1;
        cout<< l <<endl;

    }

    return 0;
}

 

 
 
 
 
posted @ 2018-08-22 09:38  WTSRUVF  阅读(330)  评论(0编辑  收藏  举报