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 ; }