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

Total Submission(s): 547    Accepted Submission(s): 101


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

题意:给你n个数,以及花费m,要你找到最小的k,每次你可以把至多k个数合并成一个数,每次这样操作的花费是这些数的总和,要使得最后合成一个数后的代价不超过m。

思路:先对整个a[]排序,二分k,然后用两个数组,第一个数组保存原来的a[]数组,第二个数组保存合并后的数,每次合并一个数后添加到这个数组的末位,每次取k个数是依次比较两个数组的头元素的大小,小的取出,直到取到k个数。这里要注意要先在a[]数组前补0,直到n补后%(k-1)==0.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lson th<<1
#define rson th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define Key_value ch[ch[root][1]][0]
#define MOD 1000003
#define maxn 100050
int a[maxn],b[maxn];
int n,m;
int cal(int k)
{
int f2=1,r2=0,i;
int f1,r1;
int t=n%(k-1);
int sum=0;
for(i=1;i<=t;i++){
sum+=a[i];
}
r2++;b[r2]=sum;
r1=n;f1=t+1; int num=n/(k-1);
int zong=sum;
while(num--)
{
sum=0;
for(i=1;i<=k;i++){
if(f1>r1){
sum+=b[f2];
f2++;
}
else if(f2>r2){
sum+=a[f1];
f1++;
}
else{
if(a[f1]<b[f2]){
sum+=a[f1];
f1++;
}
else{
sum+=b[f2];
f2++;
}
}
}
zong+=sum;
if(zong>m)return 0;
r2++;b[r2]=sum;
}
return zong<=m;
} int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
int l,r,mid;
l=2;r=n;
while(l<=r){
mid=(l+r)/2;
if(cal(mid))r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}

hdu5884 Sort(二分)的更多相关文章

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

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

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

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

  3. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  4. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  5. uva--11991 - Easy Problem from Rujia Liu?(sort+二分 map+vector vector)

    11991 - Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for e ...

  6. noiac64 sort (二分答案)

    首先如果L=1,那就可以直接用一个优先队列来做 但它并不是1 所以要换个做法 假设我们已经知道第L的数是x,第R的数是y 那其实就只需要找到[x+1,y+1]这一段,然后再加上一定数量的x和y就是答案 ...

  7. hdu5884 Sort

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

  8. HDU 5884 Sort (二分+k叉哈夫曼树)

    题意:n 个有序序列的归并排序.每次可以选择不超过 k 个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问 k最小是多少. 析:首先二分一下这个 k .然后在给定 k 的情况下, ...

  9. Codeforces 1197 E (dp+sort+二分) (Rust)

    原题链接 2300分 大意 俄罗斯套娃,每个有内容半径in和外围半径out in_i<out_i 如果 in_i >= out_j ,那么j可以放在i内 定义残留空间 = 一列嵌套的套娃 ...

随机推荐

  1. Mac M1原生(ARM64)Golang dev&debug

    前言 通过本文最终实现了在M1芯片的Mac mini上的Goland的开发,并通过编译源码解决了无法DEBUG的问题. Go 1.16版将正式支持Apple Silicon M1芯片,即arm64架构 ...

  2. 剑指Offer58-左转字符串

    题目 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=" ...

  3. Linux监控工具vmstat命令

    当linux服务器的发生告警,我们要查看当前系统的状态值,包括CPU使用率,内存使用情况,虚拟内存交换情况,IO读写情况等. top与vmstat这两个监控工具都满足要求,当然top还可以看到各个进程 ...

  4. 【Linux】ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705

    ------------------------------------------------------------------------------------------------- | ...

  5. windows下部署Grafana +prometheus平台监控

      1.Prometheus简介 Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖.用户只需要下载对应平台包,解压并且添加基本的配置即可正常启Prometheus S ...

  6. ECC 6 debuging中create points

    2013-12-07 今天无意中,发现,在ECC6中debug的时候,创建动态断点,对于command中的delete from语句居然无效,唉 虽然设置了DELETE 和DELETE FROM两个动 ...

  7. pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作

    1)指定行索引和列索引标签 index 属性可以指定 DataFrame 结构中的索引数组,  columns 属性可以指定包含列名称的行, 而使用 name 属性,通过对一个 DataFrame 实 ...

  8. 【工具篇】Mysql的安装和使用

    [导读]Mysql是数据分析师入门级的技能之一,对于很多小白同学来说,可能还没有机会接触SQL知识.那么我们如何熟悉和练习SQL呢,今天教大家安装两个软件:MySQL和Navicat.后续我们会推出S ...

  9. RESTful风格、异常处理、Spring框架

    1.RESTful风格 什么是RESTful风格? REST是REpressentational State Transfer的缩写,中文翻译为表述性状态转移,REST是一种体系结构,而HTTP是一种 ...

  10. 庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境

    庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境 一.介绍 说起微服务架构来,有一个环节是少不了的,那就是CI/CD持续集成的环境.当然,搭建CI/CD环境的工具很多, ...