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;
    }
     
     
     
     
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    国际关注,Panda 交易所获悉美银监机构批准特许银行托管加密资产
    Panda 交易所快报 央行数字货币测试进入C端流量入口
    Panda交易所获悉,五地股权市场获批参与「区块链建设试点」
    K2“拍了拍”你,这里有你想要的医药行业整体解决方案—K2 BPM
    K2 BPM 给你不一样的产品体验,有兴趣了解一下吗?
    BPM产品解读之规则设计器-K2 BPM-工作流引擎
    idea 使用Springboot 编译报错
    vue 表格中的下拉框单选、多选处理
    Kibana的安装和使用
    .net core 如何向elasticsearch中创建索引,插入数据。
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9515840.html
Copyright © 2011-2022 走看看