LightOJ - 1048 Conquering Keokradong (二分)输出路径

24 篇文章 0 订阅


LightOJ - 1048
Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %lld & %llu

Submit Status

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+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

Source

Problem Setter: Jane Alam Jan


题意:s - t中间有n+1段路径,现已经给出长度。要求你k+1天走完,让你最小化ans 即 = 所有天所走路程的最大值。然后构造出来一个方案,优先保证k+1天每天都有路程走,然后保证第1天所走路程尽量大、接着第2天……依次类推。。。

思路:这题还算比较简单,最大值最小化问题。。。但是没看到上面红字,成功wa了7发,另外check数组一开始我也写的麻烦了,一开始我枚举k次休息,然后看最后一次特判第k次休息的位置到最后的终点的距离是不是符合二分的值。。。煞笔了,其实直接看需要休息几次就好了 <= k次就return 1。。另外所谓的输出路径,就是再扫一遍就好了。。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e3 + 5;
int n, k, a[maxn], ansl[maxn];
int check(int x)
{
    int temp = 0, sum = 0;
    for(int i = 1; i <= n+1; i++)
    {
        if(a[i] > x) return 0;  //必须要休息
        if(sum + a[i] <= x)
        {
            sum += a[i];
        }
        else
        {
            temp++;
            sum = a[i];
        }
//        if(temp == 0)  //一开始特判当前到最后的位置的距离。。
//        {
            if(i == n+1) return 0;
//            int tsum = 0;
//            for(int j = i; j <= n+1; j++)
//            {
//                tsum += a[j];
//                if(tsum > x)
//                {
//                    return 0;
//                }
//            }
//            break;
//        }
    }
    return temp <= k;
}
int main()
{
    int t, ca = 1;
    scanf("%d", &t);
    while(t--)
    {
        memset(ansl, 0, sizeof(ansl));
        scanf("%d%d", &n, &k);
        int maxv = 0;
        int l = 0, r = 0, mid, ans;
        for(int i = 1; i <= n+1; i++)
            scanf("%d", &a[i]), r += a[i], l = max(l, a[i]);
        while(l <= r)
        {
            mid = (l+r)/2;
            if(check(mid))
            {
                ans = mid;
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
        int index = 0;
        for(int i = 1; i <= n+1; i++)
        {
            if(ansl[index] + a[i] > ans || n+1-i<k-index)  //保证必须有k次休息
                ansl[++index] = a[i];
            else
                ansl[index] += a[i];
        }
        printf("Case %d: %d\n", ca++, ans);
        for(int i = 0; i < k+1; i++)
        {
            printf("%d\n", ansl[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值