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. 数据结构(主席树):HZOI 2016 采花

    [题目描述] 给定一个长度为n,包含c种颜色的序列,有m个询问,每次给出两个数l,r,表示询问区间[l,r]中有多少种颜色的出现次数不少于2次. 本题强制在线,对输入的l,r进行了加密,解密方法为: ...

  2. mysql数据库还原出错ERROR:Unknown command ‘\\’解决手记

    使用mysql命令行客户端,使用source导入备份文件,但导入中出错, ERROR: Unknown command ‘\\’. ERROR: Unknown command ‘\”. ERROR: ...

  3. C语言实现两栈空间共享

    一个同学让我改一段两栈共享的C语言代码,实现进栈.出栈.输出栈里元素的功能. 代码如下: #include <stdio.h> #include <stdlib.h> #def ...

  4. Nearly prime numbers - SGU 113(素数)

    题目大意:判断一个数是否是两个素数的乘积,如果是,输出Yes,否则No. 分析:先打表求出来一部分素因子,用素数对素数判定还是比较快的. 代码如下: ========================= ...

  5. 朴素贝叶斯(naive bayes)算法及实现

    处女文献给我最喜欢的算法了 ⊙▽⊙ ---------------------------------------------------我是机智的分割线----------------------- ...

  6. Python安装scipy

    1.下载numpy+mkl 下载链接:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 2.下载scipy 下载链接:http://www.lfd.uc ...

  7. Python程序的执行原理(转载)

    Python程序的执行原理 2013-09-17 10:35 佚名 tech.uc  1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令 ...

  8. 下载的chm手册打不开的解决方法?

    用ie或者chrome等浏览器下载文件的时候,都会给文件加上一个默认的保护,右键这个文件,打开属性对话框,然后在第一个的选项卡的安全的部分,有个解除这个保护的按钮点下然后确定保存,再打开chm文件就不 ...

  9. C#-Mdi多文档窗体及其子窗体的排列 ---ShinePans

    MdiLayout枚举成员及说明 Casecade s全部Mdi层叠在父窗体 TileHorizontal 水平平铺 TitleVertical 垂直平铺 Form1.cs (mdi) using S ...

  10. [RxJS] Combination operator: combineLatest

    While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...