Cpp环境【POJ3258】【Usaco2006】【Vijos1329】River Hopscotch跳石游戏

二分搜索法-POJ3258-算法笔记 二分搜索法 是一种求最优解的方法。 思路 其思路是通过二分法,不断缩小解的可能范围,直到最优解。 其特点是,解有一定顺序特征,比如越大越可能是解,并且解可快速验证。 我们就可以假设,一个中间大小的数,验证是否是解,然后缩小一半范围。 典型问题,给定长度n的单调不降序列a0…an−1a_0…a_{n-1}a0​…an−1​和一个数K,求满足ai≥ka_i \ge kai​≥k条件下最小的i。不存在就... 阅读详情
Description  问题描述

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, L units 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.


  每年奶牛们举行一场奇特的活动。他们从左岸出发,一个个小心地跳过河上的石头,最后跳到右岸。左右岸上各有一块石头,分别是起点和终点,而它们之间有N块石头在河上,它们与两岸的两块石头处在同一条直线上。左右岸相距L个单位长度,而每块石头到左岸都有一个距离Di(0 < Di < L)。
  Farmer John很自豪地看着他的奶牛们一个个跳过河,但是渐渐地他厌倦了。他希望移走河上的一些石头,使得剩下的石头(包括岸上的)中,最近的两个石头间的距离增加。然而因为精力有限,他不能移走太多石头,最多移走M(0 <= M <= N )块。他想知道石头间的距离最长是多少。

Input  【输入格式】

Line 1: Three space-separated integers: L, N, and M
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.


  第1行:三个整数L,N和M。
  第2..N+1行:每行一个整数,表示石头到左岸的距离。输入保证没有两块石头会在同一处。

Output  【输入格式】

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks


  仅一个整数,表示移走若干块石头后的最长的最短距离。

Sample Input  【输入样例】

25 5 2
2
14
11
21
17

Sample Output  【输出样例】

4

【数据范围】  

  1<=N<=50000
  1<=L<=1000000000

Hint  【样例解释】

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).


  移走距离左岸2和14的石头,还剩下0,11,17,21,25,他们之间的最小距离为4.

Source  

原题传送门

【思路梳理】

  仔细思考还是会发现是不难的一道题,虽然说有点绕、感觉不好处理,但是考虑到“瓶颈(最小值最大)”的问题不难想到使用二分猜答案的思想,猜测能达到的瓶颈的最大值是多少,在判断时采用一点贪心思想就可以。
  贪心策略:以第一个石头(手动加入一个左岸的起点)为起点,移走与它的距离小于x的石头并统计移走的石头个数removal_num,直到下一块石头j与它的距离大于等于x或者移走的石头数大于等于m,再以第j块石头为起点搜索后面需要移走的石头即可。

【Cpp代码】
#include<cstdio>
#include<iostream>
#include<algorithm>
#define maxn 50005
using namespace std;
int l,n,m,dist[maxn];

bool Judge(int x)//判断猜测值是否成立
{
    int removal=0,i=0;//i从0(手动加入的左岸起点)开始,防止漏掉第一块石头
    while(i<=n)
    {
        int j=i+1;//移走所有与i距离小于x的石头
        while(j<=n && dist[j]-dist[i]<x)
        {
            removal++;
            if(removal>m)   return false;//移走的个数大于了m说明已经不成立
            j++;
        }
        i=j;    
    }
    return true;
}


void solve()//二分猜答案
{
    int s=0,d=l,ans=0;
    while(s<=d)
    {
        int mid=s+d>>1;
        if(Judge(mid))
            ans=mid,s=mid+1;
        else d=mid-1;
    }

    printf("%d",ans);
}

int main()
{
//  freopen("in.txt","r",stdin);
    scanf("%d%d%d",&l,&n,&m);

    for(int i=1;i<=n;i++)
        scanf("%d",&dist[i]);

    dist[0]=0;//左岸,起点 
    sort(dist,dist+1+n);

    solve();
    return 0;
}
Windows系统下LaTeX环境搭建与编辑器配置全攻略 本文详细介绍了在Windows系统下搭建LaTeX环境的完整流程,包括TeX Live的安装、TeXStudio编辑器的配置优化以及常见问题排查。特别针对中文用户推荐使用清华镜像源加速下载,并提供了XeLaTeX编译器配置、中文支持等实用技巧,帮助用户快速上手LaTeX文档编写。 阅读详情

相关推荐

科研党必备:用MATLAB批量处理.mat文件数据的3种高效方法

本文为科研人员提供了使用MATLAB批量处理.mat文件数据的三种高效方法,涵盖智能数据加载、专业可视化与自动化报告生成。通过构建自动化框架、多维度数据画图策略及批量处理流程,帮助用户显著提升数据处理效率与图表专业性,解决海量实验数据的管理与分析难题。

weixin_29089057的博客 71

usaco 2006 Dec【River Hopscotch跳石头】

Description 奶牛们喜欢参加一种特别的运动——跳石头。它们分别在一条小河的两岸设置了起点和终点,各 放了一块石头,起点和终点间的跨度有 L 米。然后又在河中间放置了 N 块石头,这些石头和起点终 点处于同一条直线上,第 i 块石头距离起点有 Di 米。 游戏的时候,奶牛从起点出发,依次跳过每块石头,最后达到终点。如果两块相邻石头之间的距 离太大,有些奶牛就会跳不过去,掉到河里。约

Breathe. 1161

Lumerical INTERCONNECT 中的自相位调制 (SPM) 仿真实践指南

本文详细介绍了在Lumerical INTERCONNECT中进行自相位调制(SPM)仿真的实践指南。从SPM的基础物理原理出发,深入解析非线性薛定谔方程和克尔效应,到INTERCONNECT中NLSE波导元件的配置技巧,以及时域与频域中SPM效应的观测方法。文章还提供了常见问题排查和进阶联合仿真技巧,帮助用户高效完成从仿真到实测的校准过程。

weixin_42565652的博客 187

POJ 3258-River Hopscotch(二分答案)

River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25633 Accepted: 10556 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves...

在读NLP硕士 335

软件工程导论实践:从需求到架构的机票预订系统全流程解析

本文深入解析机票预订系统从需求分析到架构设计的全流程实践,涵盖业务流程、功能建模、模块化设计、性能优化等关键环节。通过实际项目经验,分享软件工程导论在复杂系统开发中的应用技巧,包括数据流图绘制、E-R图设计、微服务架构及高并发处理等实战策略,助力开发者构建高效可靠的机票预订系统。

weixin_29202213的博客 412

UDS诊断入门第一课:手把手教你玩转0x10会话控制服务(附ISO14229实战解析)

刚接触汽车电子诊断的工程师们,往往会被厚厚的ISO 14229标准文档和各种专业术语吓退。但就像学习编程要从"Hello World"开始一样,掌握UDS协议的最佳切入点就是0x10诊断会话控制服务。这个看似简单的服务,实际上是整个诊断系统的"钥匙"——它决定了ECU(电子控制单元)当前处于何种工作模式,直接影响着其他诊断服务的可用性。 在实际车辆诊断中,ECU默认处于DefaultSession模式,这种模式下许多高级诊断功能是被禁用的。想象一下你正在开发一个需要刷写ECU固件的工具,如果连最基本的会

weixin_30402343的博客 392

E - River Hopscotch POJ - 3258(二分)

E - River Hopscotch POJ - 3258 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 p...

做一只熊猫不好吗? 282

深入解析摄像头硬件接口:DVP、MIPI-CSI2与USB的技术对比与应用场景

本文深入解析了摄像头三大主流硬件接口DVP、MIPI-CSI2与USB的核心技术差异。DVP接口作为并行传输的“老兵”,成本低但带宽和抗干扰能力有限;MIPI-CSI2凭借差分串行设计成为移动高清设备的首选,具备高带宽和强抗干扰性;USB接口则依托UVC标准实现了跨平台的即插即用。文章通过对比传输方式、带宽、应用场景等关键维度,为智能硬件、嵌入式系统等领域的工程师提供了清晰的选择指南。

c7d8e的博客 513

【二分答案】POJ - 3258 C - River Hopscotch

C - River Hopscotch  POJ - 3258 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...

qq_41037114的博客 530

FPGA波束形成:从算法到硬件的艺术之旅

本文深入探讨了FPGA在波束形成技术中的应用,从算法原理到Verilog硬件实现的全过程。文章详细分析了FPGA并行处理、低延迟和可重构特性如何优化波束形成系统性能,并提供了Verilog关键模块代码示例和资源优化策略,为工程师实现高效FPGA波束形成解决方案提供实用指导。

tea88的博客 1044

Vue - 萤石云监控 ezuikit 视频实例销毁方案,解决使用stop方法无法销毁EZUIKit实例或销毁后依然占用内存并且监控画面依然有声音问题(解决页面多个监控摄像头画面同时播放,翻页或关闭页

vue2,vue3,萤石,监控摄像头,萤石云销毁视频实例,退出页面销毁,stop销毁原视频流,占用内存怎么办,销毁后视频依旧在后台播放有声音,销毁视频播放器,页面上有多个视频画面怎么销毁某一个,stop方法不生效怎么办,浏览器非常卡,dom销毁,ezuikit-js萤石云播放器Vue中使用beforeDestory销毁播放器实例,调用萤石云ezuikit-js的视频对象销毁,vue使用萤石云监控视频录像回放,不销毁实例会出现离开页面后依旧有监控声音并且占内存问题,不好使,页面崩了,崩溃,视频监控太多,没有用

🏆 前端领域优质创作者 1万+

POJ-3258 River Hopscotch 解题报告

Description 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, straigh

我要变强!直到我的名字响彻天堂! 1289

315、弱电基础知识介绍

常见的弱电系统包括:闭路电视监控系统、防盗报警系统、门禁系统、电子巡更系统、停车场管理系统、可视对讲系统、家庭智能化系统及安防系统、背景音乐系 统、LED显示系统、等离子拼接屏系统、DLP大屏系统、三表抄送系统、楼宇自控系统、防雷与接地系统、寻呼对讲及专业对讲系统、弱电管道系统、UPS不 间断电源系统、机房系统、综合布线系统、计算机局域网系统、物业管理系统、多功能会议室系统、有线电视系统、卫星电视系统、卫星通讯系统、消防系统、电话 通讯系统、酒店管理系统、视频点播系统、人力资源管理系统等等。

qq_43416206的博客 1364

算法-二分

暑期学习—二分 题目描述(poj-3258) 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, s...

qq_44621644的博客 232

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

River Hopscotch(POJ-3258)

Problem Description 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, st...

Alex_McAvoy的博客 385

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

POJ 3258 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 ro...

zifengningyu的博客 259

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

POJ 3258 River Hopscotch

River Hopscotch Time Limit:2000MS Memory Limit:65536K Total Submissions:11031 Accepted:4737 Description Every year the cows hold an event featuring a pecu...

dezhen7015的博客 173

POJ 3258 River Hopscotch (二分)

Description 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 ri

小坏蛋_千千 1640

River Hopscotch POJ - 3258(最小值最大化 二分)

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 ro...

starlet_kiss的博客 362

River Hopscotch

River Hopscotch Every year thecows hold an event featuring a peculiar version of hopscotch that involvescarefully jumping from rock to rock in a river. The excitement takes place on along, straight ri...

smoggyxhdz的博客 317

Hopscotch

Hopscotch The cows play the child’s game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel t...

Slatter的博客 1143

POJ 3258 River Hopscotch(二分法)

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, straigh...

zhe 286

POJ3258-River Hopscotch

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L...

ζёСяêτ - 小優YoU 7061

POJ 3258:River Hopscotch 二分的好想法

River Hopscotch Time Limit:2000MS Memory Limit:65536K Total Submissions:9326 Accepted:4016 Description Every year the cows hold an event featuring a pec...

weixin_34146410的博客 201

The Frog's Games<hdoj4004>

Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog a

hello world 1263
上一篇: Cpp环境【POJ3622】【Vijos2990】【Usaco2007】挑剔的美食家
下一篇: Cpp环境【POJ3622】【Usaco2008 Jan Silver】【Vijos1874】Telephone Lines架设电话线
cqyz_Lebmond
博客等级 码龄10年 18粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值