lightoj.1048.Conquering Keokradong(二分 + 贪心)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
This winter we are going on a trip to Bandorban. The main target is to climb up to the top of Keokradong. So, we will use a trail. The trail is a continuous marked footpath that goes from Bandorban to Keokradong.
Part of the experience is also the route planning of the trip. We have a list of all possible campsites that we can use along the way and we want to do this trip so that we only stop K nights to camp. We also know in advance the distance between consecutive campsites and we are only allowed to camp at a campsite. Our goal is to plan the trip so that we minimize the maximum amount of walking done in a single day. In other words, if our trip involves 2 nights (3 days of walking), and we walk 9, 10, 5 miles on each day respectively, the cost (maximum amount of walking done in one day) is 10. Another schedule that involves walking 9, 6, 9 miles on each day has cost 9.
Given the distances between N consecutive campsites of a trail and given the number of nights for your trip, K, your task is to devise a camping strategy for the specified trail such that it minimizes the maximum amount of walking done in a single day. Note that the first distance value given is the distance from our start-point of the trail to our 1st campsite, and the last distance value given is the distance from our Nth campsite to our end-point of the trail.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains of two integers, the number of campsites, N (1 ≤ N ≤ 1000) and the number of nights of the trip, K (1 ≤ K ≤ min(N, 300)). The following N + 1 lines indicate the distance in miles between consecutive campsite locations. All the integers will be positive and less than10000.
Output
For each case of input you have to print the case number and the minimized cost as described above. Then print K+1 lines, each containing the amount of distance covered in ith day. As there can be many solutions, the primary target is to find the one which ensures that each day we have to walk some distance. For ties, print the one where the distance covered in first day is maximum, then the distance covered in second day is maximum and so on.
Sample Input
1
4 3
7
2
6
4
5
Sample Output
Case 1: 8
7
8
4
5
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int inf = 0x3f3f3f3f ;
int n , m ;
int T ;
int a[] ;
int l , r ; bool solve (int mid)
{
int tmp = , cnt = ;
for (int i = ; i < n ; i ++) {
tmp += a[i] ;
if (tmp > mid) {
tmp = a[i] ;
cnt ++ ;
}
}
cnt ++ ;
return cnt > m ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
int cas = ;
while (T --) {
scanf ("%d%d" , &n , &m ) ;
n ++ ; m ++ ;
l = - inf ;
r = ;
for (int i = ; i < n ; i ++) {
scanf ("%d" , &a[i]) ;
l = std::max (a[i] , l ) ;
r += a[i] ;
}
while (l <= r) {
int mid = ( l + r ) / ;
if (solve (mid) ) l = mid + ;
else r = mid - ;
}
printf ("Case %d: %d\n" , cas ++ , l ) ;
int sum = ;
int cnt = ;
for (int i = ; i < n ; i ++) {
sum += a[i] ;
if (sum > l) {
printf ("%d\n" , sum - a[i]) ;
sum = a[i] ;
cnt ++ ;
}
if (m - cnt + i >= n) {
printf ("%d\n" , sum ) ;
for (int j = i + ; j < n ; j ++) printf ("%d\n" , a[j]) ;
break ;
}
}
}
return ;
}
initial l = max{a[]} , r = sum {a[]} ;
很明显我们要求的x肯定在[l,r]这个区间内。
我们也能很容易求出:当组合后各个堆中最大的x已知时,至少需要走的天数 day。
所以我们令mid = (l + r)/ 2 ; 并求出对应的 day,if day > (k + 1) , 说明x的值应在[mid + 1,r]上 ; else , 便在[l,mid - 1]上(ps:至于为什么day == k + 1是也定位在这一块,是因为我们想令day “minimize”)。
而且因为我们所求的最后x,是指至少需要走的天数,所以很多情况下会比k + 1小,所以输出时应尽量让前面的a[i]相加贴近x,在最后到。。。就单个输出啊a[i]来补到k+1个。
lightoj.1048.Conquering Keokradong(二分 + 贪心)的更多相关文章
- 1048 - Conquering Keokradong
1048 - Conquering Keokradong PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- Conquering Keokradong && Get the Containers(二分)
Conquering Keokradong Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- Codeforces_732D_(二分贪心)
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- $CF949D\ Curfew$ 二分/贪心
正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...
- $bzoj2067\ szn$ 二分+贪心
正解:二分+贪心 解题报告: 传送门$QwQ$ 题目大意就说有一棵树,然后要用若干条线覆盖所有边且不能重叠.问最少要用几条线,在用线最少的前提下最长的线最短是多长. 昂首先最少用多少条线这个还是蛮$e ...
随机推荐
- MVC5-8 ViewData、ViewBag、TempData分析
MVC中Contoller与视图的数据传输 后台的值显示到界面上,我们有几种方式呢.MVC给我们提供了ViewData.ViewBag.TempData.Model这几种方式,当然我们也可以用ajax ...
- scala中集合的交集、并集、差集
scala中有一些api设计的很人性化,集合的这几个操作是个代表: 交集: scala> Set(1,2,3) & Set(2,4) // &方法等同于interset方法 sc ...
- Spring表单参数绑定中对“is”开头的boolean类型字段的的处理
之前在新浪微博上面发了一个微薄: 弱弱的发现在定义boolean类型的时候最好不要使用“is”开头,可以避免一些问题哦 然后有一些朋友朋友问我为什么,当时比较忙,现在写篇文章举一个例子,回复一下这个问 ...
- C#实现 Eval
主要是使用 CSharpCodeProvider,动态调用来进行返回. 首先会拼接成一个类的一个方法,然后返回这个方法的值.不过,因为还是使用反射,所以效率不高. public static clas ...
- HTML5系列三(多媒体播放、本地存储、本地数据库、离线应用)
各浏览器对编码格式的支持情况 audio和video元素的属性介绍 1.src:媒体数据的URL地址 <video src="pr6.mp4"></video&g ...
- 提交 git 项目 到 github 在 centos 7
Git 是分布式版本控制系统(Distributed Version Control System,简称 DVCS),它可以备份文件的历史信息,多个终端可以同时对文件作修改. 文件内容如果有了变化和之 ...
- Yocto开发笔记之《U-boot启动内核流程》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 执行命令 ...
- 电脑技巧:Win8/Win10无法打开这个应用|无法使用内置管理员账户的完美解决方法
现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...
- 使用Keil uVision下载hex文件
在uVision3/uVision4/uVision5中,可以创建一个项目用来下载HEX文件到flash里面. 具体步骤如下: 1. 在菜单中,选择 Project - New Project... ...
- 数组Arrays
1.toString 方法 Arrays的toString方法可以方便的输出一个数组的字符串形式,方便查看,它有九个重载的方法,包括八种基本类型数组和一个对象类型数组,这里列举两个: public s ...