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(最大化最小值之二分查找)的更多相关文章
- POJ 3258:River Hopscotch (最大化最小值)
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
- LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...
- E - River Hopscotch POJ - 3258(二分)
E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...
- 【BZOJ】1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分+贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1650 看到数据和最小最大时一眼就是二分... 但是仔细想想好像判断时不能贪心? 然后看题解还真是贪心 ...
- BZOJ 1650 [Usaco2006 Dec]River Hopscotch 跳石子:二分
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1650 题意: 数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次条都从一个石 ...
- POJ 3258 River Hopscotch (最大最小距离)【二分】
<题目链接> 题目大意:现在有起点和终点两个石块,这两个石块之间有N个石块,现在对这N个石块移除M个石块,使得这些石块之间的最短距离最大,注意,起点和终点这两个石块不能被移除. 解题分析: ...
- poj 2976 Dropping tests (最大化平均值:二分查找)
#include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...
- River Hopscotch(二分最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9923 Accepted: 4252 D ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
随机推荐
- Node.js权威指南 (5) - 使用Buffer类处理二进制数据
5.1 创建Buffer对象 / 705.2 字符串的长度与缓存区的长度 / 725.3 Buffer对象与字符串对象之间的相互转换 / 74 5.3.1 Buffer对象的toString方法 / ...
- asp.net生成二维码的方法
asp.net生成二维码的方法 [复制链接] 这个要利用一个Dll文件. 如下 Gma.QrCodeNet.Encoding.dll (105.5 KB, 下载次数: 27) 当然大家也可以直 ...
- SRM 389(1-250pt)
题意:按一定方法生成n个分数,求他们的和.n <= 20 解法:暴力.我只是没想到,10000^20用double算也能被接受0 0 tag:brute-force // BEGIN CUT H ...
- 你不一定能做对的JavaScript闭包面试题
由工作中演变而来的面试题 这是一个我工作当中的遇到的一个问题,似乎很有趣,就当做了一道题去面试,发现几乎没人能全部答对并说出原因,遂拿出来聊一聊吧. 先看题目代码: function fun(n,o) ...
- RabbitMQ 概念
RabbitMQ快速概念入门 转(http://blog.csdn.net/qq_16414307/article/details/50585630) 本文适有一定消息队列基础的,但没有接触过Ra ...
- Excel.Application SaveAs 把excel转换为html
Excel.Application SaveAs 中的第二个参数的值: 可以直接用 10 进制的值代替左边的这些 xl 类型 . 例如:把excel转换为html的js: var oWB = oXL. ...
- Windows Live Writer的Markdown插件MarkdownInLiveWriter支持语法高亮了
我前几天开发的Windows Live Writer的Markdown的插件MarkdownInLiveWriter支持语法高亮了.参见下图: 基本上就是把我的另一个插件CodeInLiveWrite ...
- head first-----decorate design pattern
浅谈设计模式之------装饰者模式 首先给出装饰者模式的定义吧: 动态的将责任附加到对象上,若是要扩展功能,装饰者提供了比继承更加具有弹性的替代方案. 其中 ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...