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 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...
随机推荐
- 11.1Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 主网页的框架搭建,任务编号760 研究代码,学习相应语言,讨论设计思路 申开亮 学习数据库的操作,任务编号761 研究代码,学习相应语言,讨论设计思路 王 ...
- Eclipse配置默认的编码集为utf-8
既然开了博,那就来点有用的. 可以使用下面的方法,让Eclipse对所有的项目里所有文件都按照指定的编码解析. Eclipse安装目录下有一个eclipse.ini文件, 用记事本打开即可,在最后一行 ...
- 《Dive into Python》Chapter 2 and Chapter 3 笔记
Example 2.1. odbchelper.py def buildConnectionString(params): """Build a connection s ...
- 阿里云之OSS 开放存储服务开发笔记
在使用云服务以后,你不用考虑他是否能承受压力,而是费用.不要考虑是否被攻击,而是他的API实现.本人开发阿里云服务也走了些崎岖之路,写下以备忘之. 阿里云的开放存储服务可以提供文件的存储服务,开放了上 ...
- centos nginx,php添加到Service
SHELL脚本: nginx vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx da ...
- DevExpress控件使用系列--ASPxGridView+Popup+Tab
1.控件功能 列表控件展示数据.弹框控件执行编辑操作.Tab控件实现多标签编辑操官方说明 2.官方示例 2.1 ASPxGridView http ...
- 【EntityFramwork--处理数据并发问题】
EntityFramwork--处理数据并发问题时支持乐观并发,即假定最佳场景(这里是指数据在更新过程中没有发生变化) 具体看<Beginning ASP.NET 4.5 Databases&g ...
- html添加keyword,description帮助百度收录处理方法,jsp去除空白行方法
1.将网页的title,keyword,description写成include包含文件,例如: top.jsp <%@ page language="java" conte ...
- 为什么很多应用都安装在/usr/local目录下
为什么很多应用都安装在/usr/local目录下 很多应用都安装在/usr/local下面,那么,这些应用为什么选择这个目录呢?理解了最根源的原因后,也许对你理解linux组织文件的方式有更直观的 ...
- POJ1840Eps
http://poj.org/problem?id=1840 题意 : 有这样一个式子a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,给你五个系数的值,让你找出x1,x2,x3 ...