zoukankan      html  css  js  c++  java
  • LightOJ 1048

    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
    

    Output for Sample Input

    Case 1: 8
    7
    8
    4
    5
    

    题目大意就是有一个N+1长度的数组,现在让你将它分为K+1份,求K+1份中,最大长度的最小值。

    二分枚举最大长度,O(n)的时间检查。

    注意最后输出,确保每一段都有值。做的时候没注意输出要求WA了两发。

    #include<bits/stdc++.h>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    
    int dis[1007];
    bool judge(int n, int mid, int k)
    {
        int t = 0;
        for(int i=0; i<n+1;)
        {
            int res = 0;
            while(i<n+1 && res + dis[i] <= mid)
                res += dis[i ++];
            t ++;
            if(t > k+1)
                return false;
        }
        return true;
    }
    
    void solve(int cases)
    {
        int n, k;
        scanf("%d%d", &n, &k);
        int b = 0, e = 0;
        for(int i=0; i<n+1; ++ i)
        {
            scanf("%d", &dis[i]);
            b = max(b, dis[i]);
            e += dis[i];
        }
        while(b < e)
        {
            int mid = b + (e - b) / 2;
            if(judge(n, mid, k))
                e = mid;
            else
                b = mid + 1;
        }
        printf("Case %d: %d
    ", cases, e);
        for(int i=0, j = 0; i<n+1; )
        {
            int res = 0;
            while(i<(n+1 - (k - j)) && res + dis[i] <= e)
                res += dis[i ++];
            j ++;
            printf("%d
    ", res);
        }
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
    
  • 相关阅读:
    shell脚本(多线程批量创建用户)
    听说你在从事前端开发?那这10个JavaScript的优化问题你不得不知道!
    解读网易易盾新一代IoT安全编译器Maze
    Java Web学习总结(12)——使用Session防止表单重复提交
    自动化内存管理和引用计数
    关于烂代码的那些事(下)
    rest_framwork序列化Serializer和ModelSerializer、url生成HyperlinkedIdentityField、深度depth、局部钩子校验
    版本控制
    解析器:request.body、request.POST、request.data
    权限之model对象权限,查看所有对象时,过滤指定对象。操作单条对象时,判断有没有操作权限
  • 原文地址:https://www.cnblogs.com/aiterator/p/6730893.html
Copyright © 2011-2022 走看看