POJ3285 River Hopscotch

  此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题.

  C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找到最大的X,由于c(x)=m时满足题意,所以最后输出的是ub-1或者lb(lb==ub-1

  注意相邻距离小于x的要删除(此处不是小于等于),对于相邻的距离小于x的两个石子,当删除其中一个后,又会产生其他的相邻的石子,直接计数不好计数,不妨用两个标记last,cur,其中last表示上一个石子,cur表示当前的石子

    将起点的石子和终点的石子加入数组,距离分别初始化为0,1,让last=0,cur=last+1=1开始,由于起点和终点不能删除,每次只需判断cur是否需要删除,若需要,cur++;

再次循环时,last=cur,cur=last+1,对于循环的终止条件,当cur=n+1时,若a[cur]-a[last]<x(此时last一定不为0),前面一直删除的是右边的,此处其实删除的是左边的,对结果没影响,此时cur++,cur的值变为n+2,跳出最内层循环,再跳出外层循环.

注意 last一定不为0的假设是基于n!=0的假设,当n=0时,可以单独判断一下.当n=0时,mid会一直减小到l,所以下面程序也没问题

我一开始把 int c(x) 函数写成了bool类型,在调试时又把INF改小了,后来交上去就一直wa,其实不需要将ub初始化为INF,直接初始化为l+1就够了

以下为AC代码

/*
* Created: 2016年03月31日 16时07分30秒 星期四
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; int l,n,m,cnt;
int a[];
int C(int x){ //返回值是int类型,不是bool类型
cnt=;
int last,cur;
last=;
while(true){
cur=last+;
if(cur>=n+) break;
while(a[cur]-a[last]<x){
cnt++;
cur++;
if(cur==n+) break;
}
last=cur;
}
return cnt;
}
int main()
{
in2(l,n);
in(m);
a[]=;//a[0]=0为起点
rep(i,,n+){
in(a[i]);
}
a[n+]=l; //a[n+1]为终点
sort(a,a+n+);
int lb,ub,mid;
lb=,ub=l+1;//设为l+1可以减小查找时间
while(ub-lb>){
mid=(ub+lb)/;
if(C(mid)>m) ub=mid;//距离大了
else if(C(mid)<=m) lb=mid;
}
out(lb);//或者out(ub-1);
return ;
}

POJ3285 River Hopscotch(最大化最小值之二分查找)的更多相关文章

  1. POJ 3258:River Hopscotch (最大化最小值)

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

  2. LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)

    题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...

  3. E - River Hopscotch POJ - 3258(二分)

    E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...

  4. 【BZOJ】1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1650 看到数据和最小最大时一眼就是二分... 但是仔细想想好像判断时不能贪心? 然后看题解还真是贪心 ...

  5. BZOJ 1650 [Usaco2006 Dec]River Hopscotch 跳石子:二分

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1650 题意: 数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次条都从一个石 ...

  6. POJ 3258 River Hopscotch (最大最小距离)【二分】

    <题目链接> 题目大意:现在有起点和终点两个石块,这两个石块之间有N个石块,现在对这N个石块移除M个石块,使得这些石块之间的最短距离最大,注意,起点和终点这两个石块不能被移除. 解题分析: ...

  7. poj 2976 Dropping tests (最大化平均值:二分查找)

    #include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...

  8. River Hopscotch(二分最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9923   Accepted: 4252 D ...

  9. [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697   Accepted: 2893 D ...

随机推荐

  1. Linux Shell编程(16)——循环

    循环就是重复一些命令的代码块,如果条件不满足就退出循环. for loops for arg in [list] 这是一个基本的循环结构.它与C的for结构有很大不同. for arg in [lis ...

  2. 初遇ping++

    运行遇到的bug java.lang.NoClassDefFoundError: Failed resolution of: Lcom/pingplusplus/android/PingppLog; ...

  3. 万能的Volley

    v1olley能干那些事?发送get请求 public void getJson() { String url = "http://"+host+":8080/web/j ...

  4. WebClient的超时问题及解决

    WebClient的超时问题及解决 转自:http://blog.163.com/xiaozhi797@126/blog/static/62440288201112245345838/   Webcl ...

  5. 关于将客户端移植到Lua的解决方案设想。

    现在发行商都需要cp们做热更新,而对于unity制作的游戏来讲,这个恐怕是个噩梦,而项目已经进行到中后期,确实很麻烦,有UniLua,但是如果全部手动解决恐怕上不了线了工作量太大,初步设想如果做一个基 ...

  6. 【PHP】php+txt实现网页计数器(限IP统计方式和不限IP统计方式)

    一般的网页计数器制作实现思路:首先设定存放统计数据的文件(counter.txt)——读取文件中的内容存入字符串——自加操作——以写入方式打开文件写入数据——从文件中输出统计数据——关闭文件. 代码: ...

  7. C++Primer第5版学习笔记(三)

    C++Primer第5版学习笔记(三) 第四/五章的重难点内容           你可以点击这里回顾第三章内容       因为第五章的内容比较少,因此和第四章的笔记内容合并.       第四章是 ...

  8. mysql登录时,ERROR 1045 (28000): 错误解决办法

    错误问题的描述: ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ERROR 10 ...

  9. hdoj 2473 Junk-Mail Filter【并查集节点的删除】

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. winform 导出TXT 分类: WinForm 2014-05-15 15:29 128人阅读 评论(0) 收藏

    截图: 代码实现:(导出txt按钮事件) using System.IO; using System.Data.OleDb; private void btnOutTxt_Click(object s ...