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 than 10000.

Output

For each case of input you have to print the case number and the minimized cost as described above. Then print K+1lines, 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

题解:

就是一个简单的二分,数字通过合并,最小化最大值,输出合并后的情况;写的时候开始wa了,今天看了看,输出的时候出现的问题;例如这组数据

1

4 3

100

2

6

4

5

会出现输出不全;所以要记录下当前输出了多少数字,判断与剩余字的关系就好了;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int num[];
int n, m;
bool js(int x){
int k = , cnt = ;
for(int i = ; i < n; i++){
if(num[i] > x)return false;
k += num[i];
if(k > x){
k = num[i];
cnt++;
}
}
if(cnt <= m)return true;
else return false;
}
int erfen(int l,int r){
int mid, ans = ;
while(l <= r){
mid = (l + r) >> ;
if(js(mid)){
ans = mid;
r = mid - ;
}
else l = mid + ;
}
return ans;
} int main(){
int T, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
n++;m++;
int temp = , ms = ;
for(int i = ; i < n; i++){
scanf("%d", num + i);
ms = max(ms, num[i]);
temp += num[i];
}
ms = max(ms, erfen(, temp));
printf("Case %d: %d\n", ++kase, ms);
int k = , cnt = ;
for(int i = ; i < n; i++){
if(k + num[i] > ms || n - i < m - cnt){//注意
cnt++;
printf("%d\n", k);
k = num[i];
}
else{
k += num[i];
}
}
printf("%d\n", k);
}
return ;
}
 Get the Containers

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

A conveyor belt has a number of vessels of different capacities each filled to brim with milk. The milk from conveyor belt is to be filled into 'm' containers. The constraints are:

  1. Whenever milk from a vessel is poured into a container, the milk in the vessel must be completely poured into that container only. That is milk from same vessel cannot be poured into different containers.
  2. The milk from the vessel must be poured into the container in order which they appear in the conveyor belt. That is, you cannot randomly pick up a vessel from the conveyor belt and fill the container.
  3. The ith container must be filled with milk only from those vessels that appear earlier to those that fill jthcontainer, for all i < j.

Given the number of containers m, you have to fill the containers with milk from all the vessels, without leaving any milk in the vessel. The containers need not necessarily have same capacity. You are given the liberty to assign any possible capacities to them. Your job is to find out the minimal possible capacity of the container which has maximal capacity.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 1000), the number of vessels in the conveyor belt and then m (1 ≤ m ≤ 106), which specifies the number of containers to which you have to transfer the milk. The next line contains the capacity c (1 ≤ c ≤ 106) of each vessel in order which they appear in the conveyor belt. Note that, milk is filled to the brim of any vessel. So the capacity of the vessel is equal to the amount of milk in it.

Output

For each case, print the case number and the desired result. See the samples for exact formatting.

Sample Input

2

5 3

1 2 3 4 5

3 2

4 78 9

Sample Output

Case 1: 6

Case 2: 82

跟上题一样,比上题简单些:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int num[];
int n, m;
bool js(int x){
int k = , cnt = ;
for(int i = ; i < n; i++){
k += num[i];
if(k > x){
k = num[i];
cnt++;
}
}
if(cnt <= m)return true;
else return false;
}
int erfen(int l,int r){
int mid, ans = ;
while(l <= r){
mid = (l + r) >> ;
if(js(mid)){
ans = mid;
r = mid - ;
}
else l = mid + ;
}
return ans;
}
int main(){
int T, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
int temp = , ms = ;
for(int i = ; i < n; i++){
scanf("%d", num + i);
ms = max(ms, num[i]);
temp += num[i];
}
ms = max(ms, erfen(, temp));
printf("Case %d: %d\n", ++kase, ms);
}
return ; }

Conquering Keokradong && Get the Containers(二分)的更多相关文章

  1. lightoj.1048.Conquering Keokradong(二分 + 贪心)

    Conquering Keokradong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  2. 1048 - Conquering Keokradong

    1048 - Conquering Keokradong    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  3. 1076 - Get the Containers

    1076 - Get the Containers    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  4. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  5. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  6. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  7. IBM Bluemix体验:Containers持久存储

    上一篇介绍了在Bluemix Containers服务中使用docker hub镜像和container的高可用配置.接下来我们尝试如何在容器中使用持久存储. 在Bluemix的Containers服 ...

  8. IBM Bluemix体验:Containers进阶

    上一篇中介绍了Bluemix的Containers服务以及如何使用自定义的docker image创建一个容器实例并对外提供服务.除了自定义镜像之外,Bluemix Containers还可以使用Do ...

  9. IBM Bluemix体验:Containers

    国际版的Bluemix目前有三个region,US South,United Kingdom和Sydney.其中US South是功能最全的,UK其次,Sydney功能最少.Containers服务在 ...

随机推荐

  1. IOS 退出App

    UIApplication *app = [UIApplication sharedApplication]; [app performSelector:@selector(suspend)]; // ...

  2. 检测iOS系统的定位服务

    [CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关

  3. java与.net比较学习系列(5) 流程控制语句

    java中流程控制语句主要分为以下几类,第一,条件语句,主要包括if语句和switch语句.第二,循环语句,主要包括while循环语句,for循环语句.第三,跳转语句,主要包括三种,break跳出语句 ...

  4. [置顶] Android项目组织和代码重用

    在Android应用开发过程中,只要涉及两个或以上人的开发,就需要考虑分工和代码的组织和重用问题. 代码重用有三种方式: 1.APK: 2.JAR:通过Libs/ 和Build path集成,缺点是不 ...

  5. zabbix-check of pre-requisites

    LAMP搭建完成后,访问http://ip/zabbix,在检查环境界面,有的检查项目提示fail.常见如下:zabbix:Check of pre-requisites1.PHP bcmath fa ...

  6. HTML与CSS入门——第一章 理解Web的工作方式

    知识点: 1.万维网的简史 2."网页"的含义,以及该术语不能反映所涉及的所有内容的原因 3.如何从你的个人计算机进入别人的浏览器 4.选择Web托管提供商的方法 5.不同的Web ...

  7. PHP学习笔记二十七【重写】

    <?php // class Animal{ public $name; protected $price; function cry(){ echo "动物在叫....<br/ ...

  8. ios开发 block语句块

    ios开发 block语句块 1.block 理解为匿名函数 2.block变量的定义 //定义block变量,^表示定义block //技巧:函数名左右加括号,在函数名前面在加^ void (^bl ...

  9. 生成树题目泛做(AD第二轮)

    题目1: NOI2014 魔法森林 LCT维护MST.解题报告见LOFTER #include <cstdio> #include <iostream> #include &l ...

  10. ExtJS 饼状图报表

    简单的ExtJS饼状图报表. 先上源码,咱再慢慢解析: Ext.onReady(function(){ var store = Ext.create('Ext.data.JsonStore', { f ...