poj3258 题目  (最大化最小值)(最小值最大化)

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

此题要求最短距离最大,对于最短距离我们知道其范围是1~L,那么可以在该单调区间内进行二分查找不断缩小范围。

那么还需要一个判断函数judge来判断当前距离作为最短距离是否是可行解。如果是可行解,但有可能它不是最优解,那么因为求最大值我们还需要继续向其右部区间查找是否有更优解;如果不是可行解,那么可行解只可能在其左部区间,二分向左部查找。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = 5e4+5;
int L,N,M;
int dis[Max];
/*
要求去掉M块石头后,剩下的石头之
间以及石头与河岸的最小距离的最大值。
*/
int cmp(int a,int b)
{
return a<b;
}
int Bsearch(int l,int r,int k)
{
int mid,last,cnt;
while(l<=r)
{
mid=(l+r)>>1;
last = cnt = 0;
for(int i=1;i<=N+1;i++)
if(mid>=dis[i]-dis[last]) cnt++;
else last = i;
if(cnt>k) r = mid-1;
else l = mid+1;
}
return l;
}
int main()
{
cin>>L>>N>>M;
dis[0]=0;
dis[N+1]=L;
for(int i=1;i<=N;i++)
cin>>dis[i];
sort(dis+1,dis+N+1,cmp);
int ans = Bsearch(0,L,M);
cout<<ans<<endl;
return 0;
}

poj 3273 题目(最小化最大值)(最大值最小化)

#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<math.h>
using namespace std;
const int Max_N=1e5+5;
const int INF = 0x3f3f3f3f;
int N,M;
int day[Max_N]; bool C(int mon)
{
int sum=0,cnt=0;
for(int i=0;i<N;i++)
{
if(day[i]>mon) return false;
if(sum+day[i]<=mon) sum+=day[i];
else {
sum=day[i];
cnt++;
}
}
cnt++;
return cnt<=M;
} void solve()
{
int l=0,r=INF;
while(r-l>1)
{
int mid = (r+l) >> 1;
if(C(mid)) r= mid;
else l = mid;
}
printf("%d\n",r);
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++)
scanf("%d",&day[i]);
solve(); return 0;
}

poj 3258 3273的更多相关文章

  1. POJ 3122 & 3258 & 3273 #二分

    以下三道都是经典二分,道理都差不多,代码就贴在一起了. POJ 3122    POJ 3258    POJ 3273 POJ 3122: #include<iostream> #inc ...

  2. POJ 2456 3258 3273 3104 3045(二分搜索-最大化最小值)

    POJ 2456 题意 农夫约翰有N间牛舍排在一条直线上,第i号牛舍在xi的位置,其中有C头牛对牛舍不满意,因此经常相互攻击.需要将这C头牛放在离其他牛尽可能远的牛舍,也就是求最大化最近两头牛之间的距 ...

  3. poj 3258 River Hopscotch 题解

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

  4. 二分搜索 POJ 3258 River Hopscotch

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

  5. E - River Hopscotch POJ - 3258(二分)

    E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...

  6. poj 3258 River Hopscotch(二分+贪心)

    题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...

  7. POJ 3258 River Hopscotch 二分枚举

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

  8. poj 3258 River Hopscotch 【二分】

    题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...

  9. POJ 3258(二分求最大化最小值)

    题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...

随机推荐

  1. 关于java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.的问题

    报错如下: 300 [main] DEBUG org.apache.hadoop.util.Shell - Failed to detect a valid hadoop home directory ...

  2. dpdk优化相关 转

    注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...

  3. linux内核中hlist_head和hlist_node结构解析

    hlist_head和hlist_node用于散列表,分表表示列表头(数组中的一项)和列表头所在双向链表中的某项,两者结构如下: 1 2 3 struct hlist_head { struct hl ...

  4. nodejs buffer 内存泄漏问题

    摘自<Node.js 高级编程> var buffer = new Buffer("this is the content of my buffer"); var sm ...

  5. Windows下的PHP 5.3.x安装 Zend Guard Loader

    PHP5.3之后不再使用Zend Optimizer而是由Zend Guard Loader替换,而Zend Guard Loader安装比前者方便了很多,只有一个dll: 址:http://down ...

  6. Vsphere初试——使用Vsphere client

    好不容易安装好ESXi之后,就要安装一个Vsphere Client,为什么要安装这个东东.使用过vmware workstation的人都知道,安装完就可以添加虚拟机,但是ESXi要通过Vspher ...

  7. Java学习笔记:多线程(一)

    Java中线程的五种状态: 新建状态(New) 就绪状态(Runnable) 运行状态(Running) 阻塞状态(Blocked) 凋亡状态(Dead) 其中阻塞状态(Blocked)又分为三种: ...

  8. Windows 8 Stroe Apps 控件

    重新想象 Windows 8 Store Apps (3) - 控件之内容控件: ToolTip, Frame, AppBar, ContentControl, ContentPresenter; 容 ...

  9. SLI的相关学习

    今天帮人安装前年的机皇-微星GT80S的操作系统,安装好后用鲁大师测试下跑分,发现双显卡和单显卡鲁大师的跑分竟然一样,就像副显卡根本没有工作,听主人所这台机器能跑到36万以上. 然后就苦逼的尝试,把B ...

  10. syslog系统日志、Windows事件日志监控