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



PROBLEM SETTER: JANE ALAM JAN

题意:将N+1个数分成K+1段,并且求这些段中的最大值的最小是多少,并且保证最小的情况下按照第一段最大优先,然后第二段。。。。

思路:二分+贪心

先用二分去找最大值的最小是多少,我们可以知道当我们分的段数越小那么这个最大的值就越大,所以我们二分找到,可以分成<=k+1段的最小的最大值

然后我们知道这个值的时候,贪心组合按前到后,贪心选到每段的最大,最后只要保证取到K+1段就行

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<math.h>
 9 using namespace std;
10 int ans[2000];
11 int uu[2000];
12 bool check(int k,int n,int m)
13 {
14         int i,j;
15         int sum=0;
16         int cnt=1;
17         for(i=0; i<=n; i++)
18         {
19                         if(sum+ans[i]>k)
20                         {
21                                 uu[cnt-1]=sum;
22                                 sum=ans[i];
23                                 cnt++;
24                         }
25                         else if(sum+ans[i]<=k)
26                         {
27                                 sum+=ans[i];
28                         }
29         }uu[cnt-1]=sum;
30         if(m>=cnt)
31                 return true;
32         else return false;
33 }
34 int main(void)
35 {
36         int i,j,k;
37         int s;
38         scanf("%d",&k);
39         for(s=1; s<=k; s++)
40         {       memset(uu,0,sizeof(uu));
41                 int n;
42                 int m;
43                 int maxx=0;
44                 int  sum=0;
45                 scanf("%d %d",&n,&m);
46                 for(i=0; i<=n; i++)
47                 {
48                         scanf("%d",&ans[i]);
49                         maxx=max(maxx,ans[i]);
50                         sum+=ans[i];
51                 }
52                 int l=maxx;
53                 int r=sum;
54                 int answer=-1;
55                 while(l<=r)
56                 {
57                         int mid=(l+r)/2;
58                         bool us=check(mid,n,m+1);
59                         if(us)
60                         {
61                                 answer=mid;
62                                 r=mid-1;
63                         }
64                         else l=mid+1;
65                 }
66                 printf("Case %d:",s);
67                 printf(" %d\n",answer);
68                 check(answer,n,m);
69                 int ac=0; sum=0;
70                 int cnt=1;
71                for(i=0;i<=n;i++)
72                {
73                    if(sum+ans[i]>answer||(n-i-1)<m-cnt)
74                    {
75                        uu[cnt-1]=sum;
76                        sum=ans[i];
77                        cnt++;
78                    }
79                    else
80                    {
81                        sum+=ans[i];
82                    }
83                }
84                uu[cnt-1]=sum;
85                for(i=0;i<m+1;i++)
86                {
87                    printf("%d\n",uu[i]);
88                }
89         }
90         return 0;
91 }
时间: 2024-08-28 05:00:02

1048 - Conquering Keokradong的相关文章

LightOJ 1048 - Conquering Keokradong 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1048 题意:有N 个数,将它们按顺序分成M份,M<=min(N,300).使得每一份的和的最大值最小.有很多中情况,这些情况中第一天.第二天--的和最大的情况. 思路 : 二分最大的和,得到一个数mid.判断是否符合条件. 代码:(不能AC 实在是找不出来了,先放着) #include <stdio.h> #include <ctime> #include

1048: 导弹防御系统

1048: 导弹防御系统 时间限制: 1 Sec  内存限制: 128 MB提交: 836  解决: 349[提交][状态][讨论版] 题目描述 某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹来袭,并观测到导弹依次飞来的高度,请计算这套系统最多能拦截多少导弹.拦截来袭导弹时,必须按来袭导弹袭击的时间顺序,不允许先拦截后面的导弹,再拦截前面的导弹. 输入

HDU-4848 Wow! Such Conquering! (回溯+剪枝)

Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on all Doge Planets. The inspection starts from Doge Planet 1 where DOS (Doge Olympic Statue) was built.

UVA 1048 - Low Cost Air Travel(最短路)

UVA 1048 - Low Cost Air Travel 题目链接 题意:给定一些联票,在给定一些行程,要求这些行程的最小代价 思路:最短路,一张联票对应几个城市就拆成多少条边,结点表示的是当前完成形成i,在城市j的状态,这样去进行最短路,注意这题有坑点,就是城市编号可能很大,所以进行各种hash 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #in

HDOJ 4848 Wow! Such Conquering!

dfs+减枝.... Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 651    Accepted Submission(s): 195 Problem Description There are n Doge Planets in the Doge Space. The conquero

The Hardest Problem Ever(杭电1048)

/*The Hardest Problem Ever Problem Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This c

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

1048 石子归并

1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使得总合并代价达到最小. 输入描述 Input Description 第一行一个整数n(n<=100) 第二行n个整数w1,w2...wn  (wi <= 100) 输出描述 Output Des

九度oj 题目1048:判断三角形类型

题目1048:判断三角形类型 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8240 解决:3992 题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输出: 对于每组输入,输出直角三角形.锐角三角形.或是钝角三角形. 样例输入: 3 4 5 样例输出: 直角三角形 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 i