zoukankan      html  css  js  c++  java
  • Sort HDU

    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;
    }
     
     
     
     
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Word 转换为 PDf 的技术方案
    [转载]sql server 常用存储过程
    Redmine 初体验
    Quartz.net Tutorial Lesson 1
    [转载]sql server TSQL 区分字符串大小写 的两种方法
    [原创]sql server inner join 效率测试
    java实现树的一般操作
    【转】Mybatis中进行批量更新
    【转载】单例模式的7种写法(整合自两篇文章)
    mybtis批量insert传入参数为list
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9515840.html
Copyright © 2011-2022 走看看