首页 > 其他分享 >LightOJ - 1048 Conquering Keokradong (二分)输出路径

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

时间:2023-06-08 14:04:37浏览次数:64  
标签:distance int Keokradong 1048 LightOJ ++ num ms day

Time Limit: 1000MS

Memory Limit: 32768KB

64bit IO Format: %lld & %llu

LightOJ - 1048


Conquering Keokradong



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




//题意:输入n,m,接下来输入n+1个数



表示有n+1短路要走,现在只能在m天内走完,问一天最多要走多长的路?




#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int num[1010];
int n, m;
bool js(int x)
{
	int k = 0, cnt = 1;
	for(int i = 0; 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 = 0;
	while(l <= r)
	{
		mid = (l + r) >> 1;
		if(js(mid))
		{
			ans = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return ans;
}

int main()
{
	int T, kase = 0;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		n++;m++;
		int temp = 0, ms = 0;
		for(int i = 0; i < n; i++)
		{
			scanf("%d", num + i);
			ms = max(ms, num[i]);
			temp += num[i];
		}
		ms = max(ms, erfen(1, temp));
		printf("Case %d: %d\n", ++kase, ms);
		int k = 0, cnt = 0;
		for(int i = 0; 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 0;
}





标签:distance,int,Keokradong,1048,LightOJ,++,num,ms,day
From: https://blog.51cto.com/u_16079508/6439489

相关文章

  • LightOJ - 1076 Get the Containers (二分)模板题
    TimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1076GettheContainersSubmit StatusDescriptionAconveyorbelthasanumberofvesselsofdifferentcapacitieseachfilledtobrimwithmilk.Themilkfromconveyorbeltis......
  • LightOJ - 1374 Confusion in the Problemset (模拟)
    TimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1374ConfusionintheProblemsetSubmit StatusDescriptionAsmallconfusioninaproblemsetmayruinthewholecontest.So,mostoftheproblemsetterstrytheirbesttorem......
  • django.db.utils.integrityerror: (1048, "Column 'phone' cannot be null")
    1背景:模型表中字段为:phone=models.CharField(default='',max_length=64,verbose_name=u'电话',blank=True) 2分析:在保存模型实例时,‘phone’被设置为空值.但是该字段在数据库中被设置为(NOTNULL),因此导致完整性约束错误. blank=True,在Django模型验证中,......
  • LightOJ - 1058 Parallelogram Counting (数学几何&技巧)给n个点求组成平行四边形个数
    LightOJ-1058ParallelogramCountingTimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluSubmit StatusDescriptionThereare n distinctpointsintheplane,givenbytheirintegercoordinates.Findthenumberofparallelogramswhosever......
  • 1048. 最长字符串链
    题目描述给了一个单子数组words给了字母前身的定义:A在任何地方加一个字符,凑成B,A就是B的前身问从words中怎么选,能构成最长的词链?f1-记忆化搜索基本分析怎么找到子问题?假如s是词链的最后一个单词,那么枚举去掉s某位后的构成新的词s-1,s-1就是s的更小一级的子问题dfs怎么实现?......
  • 力扣---1048. 最长字符串链
    给出一个单词数组 words ,其中每个单词都由小写英文字母组成。如果我们可以 不改变其他字符的顺序 ,在wordA 的任何地方添加恰好一个字母使其变成 wordB ,那么我们认为 wordA 是 wordB 的前身。例如,"abc" 是 "abac" 的前身 ,而 "cba" 不是 "bcad" 的前身......
  • LightOJ1007---Mathematically Hard (欧拉函数)
    Mathematicallysomeproblemslookhard.Butwiththehelpofthecomputer,someproblemscanbeeasilysolvable.Inthisproblem,youwillbegiventwointegersaandb.Youhavetofindthesummationofthescoresofthenumbersfromatob(inclusive).T......
  • LightOJ 1348 Aladdin and the Return Journey (树链剖分)
    树链剖分模板题。最近一直有比赛。。好长时间没写了。明显生疏了。。找个模板题熟悉一下。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib.h>#include<map>#include<set>#include......
  • LightOJ - 1044 Palindrome Partitioning(DP)
    题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分接着枚举j,如果[i,j]回文,那么dp[i]=min(dp[i],dp[j-1]+1)#include<cstdio>#include<cstring>#include<al......
  • LightOJ - 1300 Odd Personality(边双连通+奇圈判定)
    题目大意:给出一张无向图,要求找出符合条件的点条件如下:从该点出发,经过一定数量的边,又回到该点,经过的边不能重复经过,且经过的边的数量为奇数解题思路:要回到原点,且不能重复经过边,只能在边双连通分量中找了接着要判断的是有多少个点,只要边双连通分量中有奇圈,那么这个连通分量中的所......