poj 3258 River Hopscotch(二分+贪心)
题目: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(二分+贪心)的更多相关文章
- POJ 3258 River Hopscotch(二分答案)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- POJ 3258 River Hopscotch 二分枚举
题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...
- poj 3258 River Hopscotch 二分
/** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- poj 3258 River Hopscotch 题解
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
- POJ 3258 River Hopscotch (binarysearch)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...
- POJ 3258 River Hopscotch(二分答案)
嗯... 题目链接:http://poj.org/problem?id=3258 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...
随机推荐
- python学习小结8:I/O
文件I/O是Python中最重要的技术之一,在Python中对文件进行I/O操作是非常简单的. 打印到屏幕上 >>> print "python is really a g ...
- 华为章宇:如何学习开源项目及Ceph的浅析
转自http://www.csdn.net/article/2014-04-10/2819247-how-to-learn-opensouce-project-&-ceph 摘要:开源技术的学 ...
- Jquery ajax请求导出Excel表格
直接贴代码吧 $("#btn-export").click(function(){ var exportExcel = "export_excel"; data ...
- w3c_html_study_note_5.26
xhtml+css 正确的说法 “DIV+CSS”叫法将网页制作者引入两大误区 [误区一]网页中用了Table,页面就不标准,甚至觉着用Table丢人,Table成为了判定页面是否标准的关键点. [误 ...
- win下Maven安装和基本设置
注:本文介绍 Windows 平台上 Maven 的安装.Maven 3 需要运行在 JDK1.4 以上的版本上. 非原创:原创地址 http://www.ibm.com/developerworks ...
- 【BZOJ】【1412】【ZJOI2009】狼和羊的故事
网络流/最小割 一开始我是将羊的区域看作连通块,狼的区域看作另一种连通块,S向每个羊连通块连一条无穷边,每个狼连通块向T连一条无穷边,连通块内部互相都是无穷边.其余是四连通的流量为1的边……然后WA了 ...
- 编译为 Release 与 Debug 的区别
class Program { static void Main(string[] args) { DoWork(); } static void DoWork() { new Person().Ru ...
- epoll 知识总结
poll/select/epoll 对比 http://www.cnblogs.com/apprentice89/p/3234677.html ---有待继续学习 http://blog.chi ...
- PE文件结构详解(五)延迟导入表
PE文件结构详解(四)PE导入表讲 了一般的PE导入表,这次我们来看一下另外一种导入表:延迟导入(Delay Import).看名字就知道,这种导入机制导入其他DLL的时机比较“迟”,为什么要迟呢?因 ...
- 【C++基础】构造函数
说说你对构造函数的理解? 构造函数:对象创建时,利用特定的值构造对象(不是构造类),将对象初始化(保证数据成员有初始值),是类的一个public 函数 ① 与类同名 ② 无返回值 ③ 声明 ...