G - River Hopscotch(二分)
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
题意:小牛过河,可以移除M块石头,寻找所跳最小距离的最大值
题解:1 —— L之间进行二分查找,如果存在当前位置last+mid > a[i] 说明此种方案存在更小的距离,要想保证此时的mid为最小距离就要将当前的石块去除,用cnt记录已经去除石块的个数,如果cnt > M(所给的能够去除石块的个数),则更新 right = mid - 1,否则 left = mid + 1;
AC代码
1 #include<stdio.h>
2 #include<algorithm>
3
4 using namespace std;
5
6
7 int l, n, m;
8 int a[50005];
9 int solve(int x)
10 {
11 int cnt = 0;
12 int last = 0;
13 for(int i = 1; i <= n+1; i++)
14 {
15 if(a[i] < last + x) //当前的位置加上跳的距离能够到达下一个石头
16 cnt++; //存在更小的跳跃距离
17 else
18 last = a[i]; //更新当前位置
19 }
20 return cnt;
21 }
22
23 int main()
24 {
25 int right, mid, left;
26 while(~scanf("%d%d%d", &l, &n, &m))
27 {
28 for(int i = 1; i <= n; i++)
29 scanf("%d", &a[i]);
30 sort(a+1, a+n+1);
31 a[n+1] = l;
32 int ans = 0;
33 right = l;
34 left = 1;
35 while(left <= right)
36 {
37 mid = (left + right) / 2;
38 if(solve(mid) > m)
39 right = mid - 1;
40 else
41 left = mid + 1;
42 }
43 printf("%d\n", left - 1);
44 }
45 return 0;
46 }
G - River Hopscotch(二分)的更多相关文章
- River Hopscotch(二分POJ3258)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9263 Accepted: 3994 Descr ...
- POJ 3258 River Hopscotch(二分答案)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- POJ3258 River Hopscotch —— 二分
题目链接:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3258:River Hopscotch 二分的好想法
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9326 Accepted: 4016 D ...
- River Hopscotch(二分)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5473 Accepted: 2379 Description Every ...
- poj 3258 River Hopscotch(二分+贪心)
题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...
- poj 3258 River Hopscotch 二分
/** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...
- POJ 3258 River Hopscotch 二分枚举
题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...
随机推荐
- 剑指 Offer 67. 把字符串转换成整数 + 字符串
剑指 Offer 67. 把字符串转换成整数 Offer_67 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGa ...
- HDOJ-6651(数学推导)
Final Exam HDOJ-6651 这里主要考察我们的思维能力,要想自己至少可以通过k道题目,那么可以从老师的角度出发:怎么才能尽可能让你每一道题目都不通过,但是分数却是固定的. 假设我们每道题 ...
- `vi`——终端中的编辑器
`vi`--终端中的编辑器 目标* `vi` 简介* 打开和新建文件* 三种工作模式* 常用命令* 分屏命令* 常用命令速查图 01. `vi` 简介 1.1 学习 `vi` 的目的 * 在工作中,要 ...
- Java 面向对象 02
面向对象·二级 构造方法Constructor概述和格式 * A:构造方法概述和作用 * 给对象的数据(属性)进行初始化 * B:构造方法格式特点 * a:方法名与类名相同(大小也要与 ...
- 解决.NET Core Ajax请求后台传送参数过大请求失败问题
解决.NET Core Ajax请求后台传送参数过大请求失败问题 今天在项目上遇到一个坑, 在.Net Core中通过ajax向mvc的controller传递对象时,控制器(controller)的 ...
- 从零学脚手架(三)---webpack属性详解
如果此篇对您有所帮助,在此求一个star.项目地址: OrcasTeam/my-cli 在上一篇中,介绍了webpack的entry.output.plugins属性. 在这一篇,接着介绍其它配置属性 ...
- GTID介绍
从MySQL5.6开始增加GTID这个特性,Global Transaction ID,全局事务ID,用来强化主从数据库的一致性,故障恢复,以及容错能力,来替代传统的人工的主从复制: 有了GTID,在 ...
- salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容
本篇参考: https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader https://github.com/SheetJS/sheetjs ...
- 快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
Visual C# 8.0中引入了可空引用类型(Nullable reference type),通过编译器提供的强大功能,帮助开发人员尽可能地规避由空引用带来的代码问题.这里我大致介绍一下可空引用类 ...
- 第一个win32程序
vs2017下自动创建的窗口程序 // win_test.cpp : 定义应用程序的入口点. // #include "framework.h" #include "wi ...