【CSU - 1023】【HRBUST - 1039】修路(二分,枚举)

题干:

前段时间,某省发生干旱,B山区的居民缺乏生活用水,现在需要从A城市修一条通往B山区的路。假设有A城市通往B山区的路由m条连续的路段组成,现在将这m条路段承包给n个工程队(≤ ≤ 300)。为了修路的便利,每个工程队只能分配到连续的若干条路段(当然也可能只分配到一条路段或未分配到路段)。假设每个工程队修路的效率一样,即每修长度为1的路段所需的时间为1。现在给出路段的数量m,工程队的数量n,以及m条路段的长度(这m条路段的长度是按照从A城市往B山区的方向依次给出,每条路段的长度均小于1000),需要你计算出修完整条路所需的最短的时间(即耗时最长的工程队所用的时间)。

Input

 

第一行是测试样例的个数T ,接下来是T个测试样例,每个测试样例占2行,第一行是路段的数量m和工程队的数量n,第二行是m条路段的长度。

Output

 

对于每个测试样例,输出修完整条路所需的最短的时间。

Sample Input

2
4 3
100 200 300 400
9 4
250 100 150 400 550 200 50 700 300

Sample Output

400
900

Hint

 

解题报告:

   二分找到答案。因为我这里是l从0开始的,所以fit中需要先循环判断一遍是否单个路段的满足。而如果读数据的时候维护一个maxx,然后l从maxx开始,那就不需要fit函数中这个第一个for的这一行了。

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;int n,m,sum,l,r;//m个线段,最多分成n段、 
int a[305];
bool fit(int x) {int cnt = 1,cur=0;for(int i = 1; i<=m; i++) if(a[i] > x) return 0;for(int i = 1; i<=m; i++) {if(cur+a[i] <= x) cur+=a[i];else cnt++,cur=0,i--;}if(cnt > n) return 0;else return 1;
}
int main()
{int t;cin>>t;while(t--) {sum = 0;scanf("%d%d",&m,&n);for(int i = 1; i<=m; i++) {scanf("%d",&a[i]);sum += a[i];}l=0,r=sum;int mid = (l+r)/2;while(l<r) {mid = (l + r) / 2;if(fit(mid)) r=mid;else l=mid+1;}printf("%d\n",l);}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441644.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php继承和重载区别,php继承中方法重载(覆盖)的应用场合

php继承中方法重载(覆盖)的应用场合发布于 2015-02-23 17:57:23 | 91 次阅读 | 评论: 0 | 来源: 网友投递PHP开源脚本语言PHP(外文名: Hypertext Preprocessor&#xff0c;中文名&#xff1a;“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点&am…

【CodeForces - 570A】Elections(模拟,水题)

题干&#xff1a; The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate. The electoral system in the country is pretty unusual. At the first …

linux查看文件从底部开始,linux中怎样从底部向上查看log文件

Leetcode&colon; sliding window maximumAugust 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...用 pyvenv 创建几个不相互影响的python虚拟环境IN MY UBUNTU python2的环…

【CodeForces - 569B】Inventory (水题)

题干&#xff1a; Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep …

linux文件目录作用,linux 文件目录,及其作用

/boot:系统启动相关的相关文件&#xff0c;如内核&#xff0c;initrd&#xff0c;以及grub(bootloader)/dev: 设备文件设备文件&#xff1a;块设备&#xff1a;随机访问&#xff0c;数据块字符设备&#xff1a;线性访问&#xff0c;按字符文单位设备号&#xff1a;主设备号(maj…

算法笔记 -- 离散化

引入 离散化&#xff0c;就是把一些很离散的点给重新分配。 举个例子&#xff0c;如果一个坐标轴很长(>1e10)&#xff0c;给你1e4个坐标&#xff0c;询问某一个点&#xff0c;坐标比它小的点有多少。 很容易就知道&#xff0c;对于1e4个点&#xff0c;我们不必把他们在坐…

【CodeForces - 580D】Kefa and Dishes (状压dp)

题干&#xff1a; kefa进入了一家餐厅&#xff0c;这家餐厅中有n个菜&#xff08;0<n≤18&#xff09;,kefa对第i个菜的满意度为ai&#xff08;0≤ai≤10^9&#xff09;&#xff0c;并且对于这n个菜有k个规则&#xff0c;如果kefa在吃完第xi个菜之后吃了第yi个菜&#xff0…

使用linux内核编译独立系统,编译linux内核以及depmod的使用

转载&#xff1a;http://blog.lmtw.com/b/18215/archives/2010/71074.htmldepmod(depend module)功能说明&#xff1a;分析可载入模块的相依性。语  法&#xff1a;depmod [-b basedir] [-e] [-F System.map] [-n] [-v] [version] [-A]depmod [-e] [-FSystem.map] [-…

【CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)

题干&#xff1a; Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable propert…

Linux中软件安装使用的命令是,Linux软件安装命令

1.tartar命令用于把多个文件合并于一个档案文件中,并提供分解的合并后的档案文件的功能.它独立于压缩工具,因此可以选择在合并前是否压缩.tar命令的基本用法为:tar[选项]文件名常用的选项包括-c创建一个新的档案文件-t查看档案文件的内容-x分解档案文件的内容-f指定档案文件的名…

linux data文件如何打开,DATA 文件扩展名: 它是什么以及如何打开它?

DATA 问题的来源常见的 DATA 打开问题Microsoft Excel 消失如果您尝试加载 DATA 文件&#xff0c;则会收到 “无法打开 DATA 文件” 等消息。 通常情况下&#xff0c;这意味着 Microsoft Excel 没有安装在 %%os%% 上。 通过双击打开 DATA 的典型路径将不起作用&#xff0c;因为…

【CodeForces - 574D】Bear and Blocks (dp,思维)

题干&#xff1a; Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat th…

linux 编译文件mm,Linux编译C++文件,说没有找到头文件,怎么啊?新手,不太会用...

源程序&#xff1a;#include int main (int argc,char**argv){cout<return 0;}终端命令&#xff1a;zhaoweizhaowei-desktop:~$ g -c hello.cpphello.cpp:1:22: error: iostream.h: No such file or directoryhello.cpp: In function ‘int main(int, char**)’:hello.cpp:4…

【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

题干&#xff1a; Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to …

linux下apache配置文件测试,重启 Apache 服务器及测试配置

在 CentOS / RHEL / Fedora Linux 服务器上安装的 Apache 网页服务器程序在系统中的进程名称为 httpd。每次更改 Apache 服务器的配置&#xff0c;不管是 httpd.conf 还是 vhost.conf 或者自己配置的什么 .conf&#xff0c;只要有改动都需要重新加载配置或者重启 httpd 服务才能…

【CodeForces - 574C】Bear and Poker(思维,剪枝,数学)

题干&#xff1a; Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Ea…

linux变量赋值取值,linuxshell编程对变量的赋值

linux shell编程对变量的赋值shell对变量的赋值&#xff0c;所有的变量都是由字符串组成&#xff0c;不需要对变量名预先声明&#xff0c;而且有很多关键字供编程者使用。下面列举例子详细说明。 在终端下建立文件 #vi s2.sh#!/bin/sh#set a variable aa"hello world"…

*【CodeForces - 574A】Bear and Elections (优先队列,水题模拟)

题干&#xff1a; Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th …

stm32linux区别,STM32MPU和OpenSTLinux你了解多少?

早在2019年年初的时候&#xff0c;ST就发布了首款STM32MPU&#xff1a;STM32MP1。 STM32MP1通用微处理器产品系列&#xff0c;系基于混合的 双Arm Cortex-A7核 和 Cortex-M4核架构产品。 一、支持STM32MPU 生态系统熟悉 Cortex-M4 MCU 环境的开发人员能轻松实现他们的目标&…

算法总结 -- 博弈论(PN图)

博弈论&#xff1a;组合博弈 * 必败点(P点) :前一个选手(Previous player)将取胜的位置称为必败点。 * 必胜点(N点) :下一个选手(Next player)将取胜的位置称为必胜点。 * 必败&#xff08;必胜&#xff09;点的属性&#xff1a;* (1) 所有终结点是必败点&#xff08;P点&#…