第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K
来源:牛客网
题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to the other side of the forest. Between every two stones there is a distance. Let di indicates the distance between the stone i and i+1.Initially you stand at the first stone, and your target is the N-th stone. You must stand in a stone all the time, and you can stride over arbitrary number of stones in one step. If you stepped from the stone i to the stone j, you stride a span of (di+di+1+...+dj-1). But there is a limitation. You're so tired that you want to walk through the forest in no more than K steps. And to walk more comfortably, you have to minimize the distance of largest step.
输入描述:
The first line contains two integer N and K as described above.
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone.
输出描述:
An integer, the minimum distance of the largest step.
示例1
输入
6 3
1 3 2 2 5
输出
5
【题意】:
题意就是说有n块石头,每块石头中间有一定的距离,一次可以跳过多个石头,但是不可以超过k步,求最大步的最小值 。
其实意思就是说,如果你每一步比较小,比如一块一块石头地过去,那么步数就太多了;但是如果你直接一步跳到最后,这样又太浪费体力了,而且不符合题意,而题意就是要你找到这么一个平衡点,可以恰好走到三步,然后求其中最大的一步的距离(相对于其他两步是最大的,但相对于所有情况的最大步它是最小的一种情况)
【出处】:POJ 3272 Monthly Expense
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
#define ll long long
#define mod 1000000007
int n, k;
ll a[100005];
int main() {
scanf("%d%d", &n, &k);
ll s = 0;
ll Max = 0;
for (int i = 1; i <= n - 1; ++i) {
scanf("%lld", &a[i]);
s += a[i];
Max = max(Max, a[i]);
}
ll l = Max, r = s, ans, mid;
while (l <= r) {
mid = (l + r) >> 1;
s = 0;
int c = 0;
for (int i = 1; i <= n - 1; ++i) {
s += a[i];
if (s > mid) { //多个跳不过,只能前面算跳一次,从这里重新开始跳
s = a[i];
c++;
}
}
if (c >= k) {
l = mid + 1;
}
else {
r = mid - 1;
ans = mid;
}
}
cout << ans << endl;
return 0;
}
第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】的更多相关文章
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest
链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...
- 第十四届华中科技大学程序设计竞赛--J Various Tree
链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...
随机推荐
- Google Chrome 自定义协议(PROTOCOL)问题的处理
最近在使用谷歌浏览器的时候遇到了自定义协议(PROTOCOL)的问题,比较折腾,特此记录,希望我浪费生命换来的结果能够帮助读到此文的朋友少浪费一点宝贵的时间! 由于某些原因,电脑里一直没有安装阿里旺旺 ...
- 《Cracking the Coding Interview》——第18章:难题——题目1
2014-04-29 00:56 题目:不用算数运算,完成加法. 解法:那就位运算吧,用加法器的做法就可以了. 代码: // 18.1 add two numbers wihout using ari ...
- Windows下安装jenkins,关闭jenkins,修改jenkins端口号
1.Jenkins安装部署 在官网下载Jenkins: https://jenkins.io/download/thank-you-downloading-windows-installer-stab ...
- chromedriver版本支持的Chrome版本
下载chromedriver,链接:http://chromedriver.storage.googleapis.com/index.html chromedirver版本 支持的Chrome版本 ...
- JMeter学习笔记(十一) 关于 CSV Data Set Config 的 Sharing mode 对取值的影响
关于 CSV Data Set Config 的一些介绍之前已经梳理过了,可以参考: https://www.cnblogs.com/xiaoyu2018/p/10184127.html . 今天主要 ...
- Linq语法和C#6.0
一. linq 1.简介: 能用linq实现的基本都可以用扩展方法实现: 举例: 查询ID>1的狗有如下两种写法 (1)var r1=dogs.where(d=>d.id>1) ( ...
- JSP/Servlet Web 学习笔记 DayFour
Servlet概述 Servelt是使用Java Servlet应用程序接口及相关类和方法的Java程序. Servlet是用Java编写的Server端程序,它与协议和平台无关.Servlet运行于 ...
- 创建虚拟机流程nova
这篇博文借鉴于http://www.cnblogs.com/yjbjingcha/p/6977741.html,感谢博友提供. 本文试图具体地描写叙述openstack创建虚拟机的完整过程.从用户发起 ...
- iOS runLoop 理解
目录 概述 run loop modes 一.概述 run loop叫事件处理循环,就是循环地接受各种各样的事件.run loop是oc用来管理线程里异步事件的工具.一个线程通过run loop可以监 ...
- 【CF Edu 28 C. Four Segments】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...