题目:http://poj.org/problem?id=3258

题意:

一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L。

河中有n块石头,每块石头到S都有唯一的距离

问现在要移除m块石头(S和E除外),每次移除的是与当前最短距离相关联的石头,

要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离。

和3273差不多。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = +;
int a[maxn]; int main()
{
int l, n, m, i;
int low, mid, high, sum, cnt;
cin>>l>>n>>m;
a[n+] = l;
low = l;
high = l;
for(i = ; i <= n; i++)
{
cin>>a[i];
}
sort(a+, a+n+);
for(i = ; i <= n+; i++)
if(a[i]-a[i-]<low)
low = a[i]-a[i-]; while(high>=low) //注意‘=’号
{
sum = ; cnt = ;
mid = (high+low)/;
for(i = ; i <= n+; i++)
{
sum += a[i]-a[i-];
if(sum<mid)
cnt++;
else
sum = ;
}
if(cnt<=m) //注意‘=’号
low = mid+;
else
high = mid-;
}
cout<<high<<endl;
return ;
}

以后还是用这种形式的二分吧

while(left<right)

{

if()

left=mid+1;

else right=mid;

}

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = +;
int a[maxn]; int main()
{
int l, n, m, i, f;
int low, mid, high, sum, cnt;
cin>>l>>n>>m;
a[n+] = l;
low = l;
high = l;
for(i = ; i <= n; i++)
{
cin>>a[i];
}
sort(a+, a+n+);
for(i = ; i <= n; i++)
if(a[i]-a[i-]<low)
low = a[i]-a[i-]; f = ;
while(high>low)
{
sum = ; cnt = ;
mid = (high+low)/;
for(i = ; i <= n; i++)
{
sum += a[i]-a[i-];
if(sum<mid)
cnt++;
else
sum = ;
}
if(cnt > m)
{
high = mid;
f = ;
}
else
low = mid+;
//cout<<low<<endl<<high<<endl;
}
if(f)
cout<<high-<<endl;
else
cout<<high<<endl;
return ;
}

还有http://blog.csdn.net/jackyguo1992/article/details/8665202

这篇博客以这两道题为例, 说明了二分时的易错的情况

poj 3258 River Hopscotch(二分+贪心)的更多相关文章

  1. POJ 3258 River Hopscotch(二分答案)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...

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

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

  3. POJ 3258 River Hopscotch 二分枚举

    题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...

  4. poj 3258 River Hopscotch 二分

    /** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...

  5. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

  6. POJ 3258 River Hopscotch

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11031   Accepted: 4737 ...

  7. poj 3258 River Hopscotch 题解

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

  8. POJ 3258 River Hopscotch (binarysearch)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...

  9. POJ 3258 River Hopscotch(二分答案)

    嗯... 题目链接:http://poj.org/problem?id=3258 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...

随机推荐

  1. hifi/ headphone test

    https://www.youtube.com/watch?v=-r0gRjqN0N8 https://www.youtube.com/watch?v=sMh_zvCw6us

  2. 微信消息处理JAXP-dom解析

    package cn.lihainan.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import ...

  3. js之正则表达式(上)

    1.正则表达式的创建方式 两种方式创建:通过new修饰符创建和字面量的方式创建 1>new修饰符方式创建 var b2=new RegExp('Box','ig'); //第二个参数是 模式字符 ...

  4. [SC] OpenSCManager FAILED 1722

    在服务器A(windows server 2008 r2)执行如下命令访问远端服务器B(windows server 2003)的服务运行状况: sc \\servername query " ...

  5. C#错误:The Controls collection cannot be modified

    用 <%# %>这种写法是写在数据绑定控件中的,之所以用 <%= %>会出现The Controls collection cannot be modified because ...

  6. description 数组的中文打印

    打印一个对象:NSLog(@"%@", stu); 默认情况下打印的时对象的名字和内存地址:这时需要重写description方法 // 重写description方法 - (NS ...

  7. 使用Div+CSS布局设计网站的优点

    网页设计业界越来越关注DIV+CSS的标准化设计,大到各大门户网站,小到不计其数的个人网站,在Div+CSS标准化的影响下,网页设计人员已经把这一要求作为行业标准.那么什么是Div+CSS标准?Div ...

  8. oracle——表修改语句集合

     alter table table_name modify column_name default 0; 

  9. Openstack Quantum project 更名为 Neuron

    因为与磁带备份厂商Quantum商标冲突: The OpenStack Foundation has changed the name of its networking project from Q ...

  10. 比較Backbone.js, Angular.js, Ember.js, Knockout.js 心得

    還記得第一次寫網站的時候,我無意間寫成了 SPA(single page application),當時還沒有SPA這個詞,後來因為廣告主需要不同 url location 頁面的廣告展示,只好把部分 ...