loj1048Conquering Keokradong 二分
来源:互联网 发布:php hmacsha256 编辑:程序博客网 时间:2024/06/05 12:08
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
Hint
题意
题解:
和上一题一个类型题 记得走路次数+1 白天个数为晚上+1
输出是难点
代码
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int N = 1e6+100;int a[N];int f[N];int n,kk,pp;bool check(int k){ int cnt = 1; int p = 0; for (int i = 0; i < n+1; ++i){ if (p+a[i]<=k){ p+=a[i]; }else { f[pp++] = i; p = a[i]; cnt++; } } if (cnt>kk) return false; return true;}int main(){ int t; int kase = 1; scanf("%d",&t); while (t--){ scanf("%d%d",&n,&kk); int ans = 0,l = 0; for (int i = 0; i < n+1; ++i){ scanf("%d",&a[i]); ans+=a[i]; l = l>a[i]?l:a[i]; } kk+=1; printf("Case %d: ",kase++); int r = ans; while (l<=r){ int mid = (l+r)>>1; if (check(mid)) r = mid-1; else l = mid+1; } printf("%d\n",r+1); int p = 0; for (int i = 0; i < n+1; ++i){ if (a[i]+a[i+1]>r+1||n+1-i==kk-p){ printf("%d\n",a[i]);p++; }else a[i+1] += a[i]; } } return 0;}
- loj1048Conquering Keokradong 二分
- LightOJ 1048 Conquering Keokradong | 二分
- LightOJ 1048 - Conquering Keokradong 【二分】
- LightOJ 1048 Conquering Keokradong (二分)
- LightOJ 1048 - Conquering Keokradong(二分)
- lightoj 1048 - Conquering Keokradong 二分答案
- lightoj 1048 - Conquering Keokradong 【二分 + 贪心】
- LightOJ 1048 - Conquering Keokradong(二分逼近)
- LightOJ 1048 Conquering Keokradong(二分+贪心)
- LightOJ - 1048 Conquering Keokradong (二分)输出路径
- LIGHTOJ1048_Conquering Keokradong
- Conquering Keokradong LightOJ 1048
- lightoj1048 Conquering Keokradong
- 【Light】[1048]Conquering Keokradong
- Light oj 1048Conquering Keokradong
- LightOJ - 1048 Conquering Keokradong(贪心)
- 二分
- 二分
- hdu2594(KMP水题)
- linux编译安装软件 ./configure命令enable和with有什么区别
- C++设计模式——装饰者模式
- JSP核心基础——Servlet应用
- Floyd算法,求两点之间的最短路
- loj1048Conquering Keokradong 二分
- hdu 6034 2017 Multi-University Training Contest
- 04.安装eclipse及汉化
- 线段树,区间开方(Rikka with Sequence,HDU 5828)
- 10129
- [GNU/Linux] Linux系统调用-线程相关(二):线程同步
- 1068. Find More Coins (30)
- [POJ]2406Power Strings KMP
- Python爬虫学习纪要(一):BeautifulSoup相关知识点