Sort(hdu5884)
Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1038 Accepted Submission(s): 231
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.
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).
5 25
1 2 3 4 5
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)的更多相关文章
- Sort HDU5884(二分+多叉哈夫曼树)
HDU5884 Sort 题意:有n个序列要进行归并,每次归并的代价是两个序列的长度的和,要求最终的代价不能超过规定的T,求在此前提下一次能同时进行归并的序列的个数k. 思路:还是太单纯,看完题目一直 ...
- hdu5884 Sort(二分+k叉哈夫曼树)
题目链接:hdu5884 Sort 题意:n个有序序列的归并排序.每次可以选择不超过k个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问k最小是多少. 题解:先二分k,然后在k给 ...
- hdu5884 Sort
//--------------------------------------------------------------- /*---贪心策略+二分+队列 -----将原数组排序,然后每次取k ...
- hdu5884 Sort(二分)
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdu5884(多叉哈夫曼树)
hdu5884 题意 给出 n 个数,每次选择不超过 k 个数合并(删掉这些数,加入这些数的和),花费为合并的这些数的和,要求最后只剩下一个数,问 k 最小取多少. 分析 二分 k,合并数的时候可以按 ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- [算法]——快速排序(Quick Sort)
顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...
- shell之sort命令
1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@rocrocket progr ...
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
随机推荐
- Netty | 第1章 Java NIO 网络编程《Netty In Action》
目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...
- C语言之内核中的struct list_head 结构体
以下地址文章解释很好 http://blog.chinaunix.net/uid-27122224-id-3277511.html 对下面的结构体分析 1 struct person 2 { 3 in ...
- 巩固javaweb第十七天
巩固内容: 文本域 文本域主要用于输入多行文字,如果输入的文字比较多,则可以采用文本域. 文本域的基本格式如下: <textarea rows="行数" name=" ...
- IDEA2021.2安装与配置
https://blog.csdn.net/qq_37242720/article/details/119349394
- archive后upload to app store时遇到app id不可用的问题
问题如下图 出现此问题的原因有两种: 1.此app id在AppStore中已经存在,也就是说你使用别人注册的app ID , 如果是这样,你只能更换app ID 2.此app ID是自己的,突然之 ...
- Linux基础命令---apachectl
apachectl apachectl指令是apache http服务器的前端控制程序,可以协助控制apache服务的守护进程httpd. 此命令的适用范围:RedHat.RHEL.Ubuntu.Ce ...
- 什么是微服务,SpringBoot和SpringCloud的关系和区别
什么是微服务? 就目前而言对于微服务业界没有一个统一的,标准的定义.但通常而言,微服务是一种架构模式或者说是一种架构风格,它提倡单一应用程序划分为一组小的服务,每个服务在其独立的自己的进程中,服务之间 ...
- Spring Cloud中,如何解决Feign整合Hystrix第一次请求失败的问题
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- 【C/C++】最大连续子序列和/动态规划
思路主要是看了晴神的算法笔记,实现是自己重新用vector实现了一下,对付逗号隔开的输入 #include <iostream> #include <string> #incl ...
- markDodn使用技巧
markdown 标题 一级标题书写语法: 井符(#)加上空格加上标题名称 二级标题书写语法: 两个井符(#)加上空格加上标题名称 三级标题书写语法: 三个井符(#)加上空格加上标题名称 字体 字体加 ...