Conquering Keokradong && Get the Containers(二分)
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 ;
}
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:
- 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.
- 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.
- 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(二分)的更多相关文章
- lightoj.1048.Conquering Keokradong(二分 + 贪心)
Conquering Keokradong Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 1048 - Conquering Keokradong
1048 - Conquering Keokradong PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- 1076 - Get the Containers
1076 - Get the Containers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- IBM Bluemix体验:Containers持久存储
上一篇介绍了在Bluemix Containers服务中使用docker hub镜像和container的高可用配置.接下来我们尝试如何在容器中使用持久存储. 在Bluemix的Containers服 ...
- IBM Bluemix体验:Containers进阶
上一篇中介绍了Bluemix的Containers服务以及如何使用自定义的docker image创建一个容器实例并对外提供服务.除了自定义镜像之外,Bluemix Containers还可以使用Do ...
- IBM Bluemix体验:Containers
国际版的Bluemix目前有三个region,US South,United Kingdom和Sydney.其中US South是功能最全的,UK其次,Sydney功能最少.Containers服务在 ...
随机推荐
- vmware虚拟机迁移系统到其它磁盘(xjl456852原创)
有时我们将vmware安装的系统放在了磁盘空间比较小的盘里,后来磁盘空间不够用了,我们需要将文件移动到其它磁盘.腾出这个磁盘的空间. 我安装的系统有10个,总占空间大小170多GB.需要从D盘迁移到G ...
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...
- [Regex Expression] Match mutli-line number, number range
/^-?\d{,}\.\d+$/gm
- js获取 input file 图片缩略图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- html5 拖放---(二)转
draggable是一个枚举属性,用于指定一个标签是否可以被拖拽.有以下四种取值: true 表示此元素可拖拽 false 表示此元素不可拖拽 auto 除img和带href的标签a标签表示可拖拽外, ...
- java web移植 遇到Project facet Java version 1.7 is not supported
在移植eclipse项目时,如果遇到 "Project facet Java version 1.7 is not supported." 项目中的jdk1.7不支持.说明项目是其 ...
- KMP与扩展KMP
原文转自:http://www.cppblog.com/MatoNo1/archive/2011/04/17/144390.aspx KMP:给出两个字符串A(称为模板串)和B(称为子串),长度分别为 ...
- (原)lua使用ffi调用c程序的函数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5812763.html 参考网址: http://luajit.freelists.narkive.co ...
- git configuration
git的配置文件由section名和变量名组成: [user] name = abc emial = example.com []里面的user就是section名,section只能由字母,数字,- ...
- PHP函数参数的引用传递和值传递
函数的参数传递有两种方式 1,值传递 常见的 test($param) 方式就是值传递,在函数内部修改$param,不会影响外部变量$param的值 2,引用传递 参数是引用传递的方式,此时函数内部 ...