链接: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【二分答案/最小化最大值】的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  2. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  3. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  4. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  5. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  7. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  8. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  9. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

随机推荐

  1. ios开发3.5和4.0寸屏幕自适应中的一点问题

    在开发iso应用中需要考虑到ip4的3.5寸屏幕和ip5的4寸屏幕的高度不一样的问题.常见的问题有滚动条位置,底部被挡住等情况:我遇见是tableview中添加下拉上提刷新功能时刷新指示器显示位置的问 ...

  2. 剑指Offer - 九度1355 - 扑克牌顺子

    剑指Offer - 九度1355 - 扑克牌顺子2014-01-30 23:19 题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^). ...

  3. python-成员修饰符

    python的面相对象中,拥有3个成员,字段.方法.属性 class Foo: def __init__(self,name): #公有字段name在类中与类外均能调用 self.name = nam ...

  4. hnust 神奇的序列

    问题 E: 神奇的序列 时间限制: 1 Sec  内存限制: 128 MB提交: 635  解决: 84[提交][状态][讨论版] 题目描述        Aurora在南宁发现了一个神奇的序列,即对 ...

  5. PEAR DB 事务相关

    1.autoCommit().commit().rollback() function autoCommit($onoff=false) 指定是否自动提交事务.有的后端数据库不支持. function ...

  6. HTML5初识Canvas

    HTML5初识Canvas <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  7. 201621123034 《Java程序设计》第12周学习总结

    作业12-流与文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车 ...

  8. PHP 自制分页类

    思路: 通过给页面url传递get参数,来控制每页的sql查询(mysql关键词:limit),实现分页查询 代码: class getpage{ public $pagenum; public $p ...

  9. Codeforces Round #386 (Div. 2) 746F(set的运用)

    题目大意 给出一个歌单(有n首歌),每个歌都有愉悦值和时间,你可以选择从第x首歌开始听(也就是选择连续的一段),并且你可以选择w首歌让它的时间减半,限制时间为k,求最大的愉悦值 首先我们需要贪心一下, ...

  10. spring in action 学习笔记六:bean在不同情况下的默认id号或者将名字

    bean如果不知名id是什么它一般都有一个id或者讲名字. 第一种情况:组件扫描的情况:默认的id号或者bean的name是类名的首字母小写. 代码如下: package com.qls.beanli ...