Sort

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

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 (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
 
Output
For each test cases, output the smallest k.
 
Sample Input
1
5 25
1 2 3 4 5
 
Sample Output
3
思路:二分+哈夫曼;
首先二分枚举k,然后关键就是check的时候,首先看一组数据:12345,当k为3的时候,按照如果我们取的话会得到21,然后再看k=4,我们的设想肯定是递减的,但是,如果我们开始就取4个的答案为25,所以这样二分就不行,但是是我们的取法不对,我们应该保证后面的大的数字 竟量只取1次,那么最后k-1个数肯定被一次加完,不会有剩余,那么N个数如果从开始能选k个,那么剩下的肯定要是k-1的倍数,那么当剩下的不是k-1的倍数,我们要在前面添加0,个数就是k-1-(N-1)%(k-1);这样就可以保证最优,也就单调了。
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #include<iostream>
5 #include<queue>
6 #include<stdlib.h>
7 #include<math.h>
8 #include<set>
9 using namespace std;
10 typedef long long LL;
11 queue<int>que1;
12 queue<int>que2;
13 LL ans[100005];
14 bool check(int mid,int N,LL T);
15 int main(void)
16 {
17 int n;
18 scanf("%d",&n);
19 while(n--)
20 {
21 int N;
22 LL T;
23 scanf("%d %lld",&N,&T);
24 for(int i = 0; i < N; i++)
25 {
26 scanf("%lld",&ans[i]);
27 }
28 sort(ans,ans+N);
29 int id = 1;
30 int l = 2;
31 int r = N;
32 while(l<=r)
33 {
34 int mid = (l+r)/2;
35 if(check(mid,N,T))
36 {
37 r = mid-1;
38 id = mid;
39 }
40 else l = mid+1;
41 }
42 printf("%d\n",id);
43 }
44 return 0;
45 }
46 bool check(int mid,int N,LL T)
47 {
48 LL ask = 0;
49 while(!que1.empty())
50 {
51 que1.pop();
52 }
53 while(!que2.empty())
54 {
55 que2.pop();
56 }
57 int x = (N-1)%(mid - 1);
58 if(x!=0)
59 {
60 x = (mid-1-x)%(mid-1);
61 while(x)
62 {
63 que1.push(0);
64 x--;
65 }
66 }
67 for(int i = 0; i < N ; i++)
68 {
69 que1.push(ans[i]);
70 ask-=ans[i];
71 }
72 while(true)
73 {
74 LL sum = 0;
75 int c = mid;
76 while(c)
77 {
78 int flag = 0;
79 if(!que1.empty()&&!que2.empty())
80 {
81 int t = que1.front();
82 int tt = que2.front();
83 if(t<=tt)
84 {
85 que1.pop();
86 }
87 else
88 {
89 t = tt;
90 que2.pop();
91 }
92 sum+=t;
93 c--;
94 flag = 1;
95 }
96 else if(!que1.empty())
97 {
98 int t = que1.front();
99 que1.pop();
100 sum+=t;
101 c--;
102 flag = 1;
103 }
104 else if(!que2.empty())
105 {
106 int t = que2.front();
107 que2.pop();
108 sum+=t;
109 c--;
110 flag = 1;
111 }
112 if(!flag)
113 break;
114 }
115 que2.push(sum);
116 ask += sum;
117 if(c>0)break;
118 }
119 if(ask > T)
120 return false;
121 else return true;
122 }
 

Sort(hdu5884)的更多相关文章

  1. Sort HDU5884(二分+多叉哈夫曼树)

    HDU5884 Sort 题意:有n个序列要进行归并,每次归并的代价是两个序列的长度的和,要求最终的代价不能超过规定的T,求在此前提下一次能同时进行归并的序列的个数k. 思路:还是太单纯,看完题目一直 ...

  2. hdu5884 Sort(二分+k叉哈夫曼树)

    题目链接:hdu5884 Sort 题意:n个有序序列的归并排序.每次可以选择不超过k个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问k最小是多少. 题解:先二分k,然后在k给 ...

  3. hdu5884 Sort

    //--------------------------------------------------------------- /*---贪心策略+二分+队列 -----将原数组排序,然后每次取k ...

  4. hdu5884 Sort(二分)

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  5. hdu5884(多叉哈夫曼树)

    hdu5884 题意 给出 n 个数,每次选择不超过 k 个数合并(删掉这些数,加入这些数的和),花费为合并的这些数的和,要求最后只剩下一个数,问 k 最小取多少. 分析 二分 k,合并数的时候可以按 ...

  6. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  7. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

  8. shell之sort命令

    1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@rocrocket progr ...

  9. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

随机推荐

  1. js判断undefined nan等

    1,js判断undefined 主要用typeof(),typeof的返回值有:undefined,object,boolean,number,string,symbol,function等, if( ...

  2. centos 安装reids

    1.安装tcl支持 yum install tcl 2.安装redis我们以最新的2.8.9为例 $ wget http://download.redis.io/releases/redis-2.8. ...

  3. Mysql笔记(3)

    查询总数count(1)查询总和sum(数据名) 查询最大值max(数据名) 查询最小值min(数据名) 查询平均值avg(数据名) 去除重复 通过having来过滤group by字句的结果信息 i ...

  4. CAN总线常见的两种编码格式(Intel/Motorola)

    在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...

  5. day02 Linux基础

    day02 Linux基础 1.什么是服务器 服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因 此一般来说服务器应具备承担服务并且保障服务的能力. windows: ...

  6. Java Swing布局管理器GridBagLayout的使用示例 [转]

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  7. 【vector+pair】洛谷 P4715 【深基16.例1】淘汰赛

    题目:P4715 [深基16.例1]淘汰赛 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 这道题因为数据范围不大,所以做法可以非常简单,使用一个vector加上pair就可以了: ...

  8. mysql外键策略

    1.外键 建表时添加外键:constraint 外键名 foreign key 从表字段 references 主表字段 级联操作 create table dage( create table xi ...

  9. 如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach

    如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach 2016年01月13日 16:04:20 炸鸡叔 阅读数:7277   转发自:http://baike.ba ...

  10. 如何使用table布局静态网页

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...