lightoj 1048 - Conquering Keokradong 【二分 + 贪心】

题目链接:lightoj 1048 - Conquering Keokradong

1048 - Conquering Keokradong
PDF (English) Statistics Forum
Time Limit: 1 second(s) Memory Limit: 32 MB
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
Output for Sample Input
1
4 3
7
2
6
4
5
Case 1: 8
7
8
4
5

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

思路:找出最优答案,然后贪心构造路径。严格保证第i天后面还有k-i+1段就可以了。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
int d[MAXN], n, k;
int sum[MAXN];
bool judge(int o) {
    int ans = 0; sum[n+2] = INF;
    for(int i = 0; i < n+1; ) {
        int W = o + sum[i] + 1; ans++;
        int j = lower_bound(sum+i, sum+n+2, W) - sum;
        if(j == i + 1) return false;
        i = j - 1;
    }
    return ans <= k + 1;
}
int main()
{
    int t, kcase = 1; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &k); sum[0] = 0;
        for(int i = 1; i <= n+1; i++) {
            scanf("%d", &d[i]);
            sum[i] = sum[i-1] + d[i];
        }
        int l = 1, r = n*10000;
        int ans;
        while(r >= l) {
            int mid = (l + r) >> 1;
            if(judge(mid)) {
                ans = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        printf("Case %d: %d\n", kcase++, ans);
        int cnt = 0; sum[n+2] = INF;
        for(int i = 0; i < n+1; ) {
            int W = ans + sum[i] + 1; cnt++;
            int pos = lower_bound(sum+i, sum+n+2, W) - sum - 1;
            int have = n + 1 - pos;
            int need = k + 1 - cnt;
            if(have < need) {
                pos = n + 1 - need;
            }
            printf("%d\n", sum[pos] - sum[i]);
            i = pos;
        }
    }
    return 0;
}

你可能感兴趣的:(lightoj 1048 - Conquering Keokradong 【二分 + 贪心】)