poj 3258 3273
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的更多相关文章
- POJ 3122 & 3258 & 3273 #二分
以下三道都是经典二分,道理都差不多,代码就贴在一起了. POJ 3122 POJ 3258 POJ 3273 POJ 3122: #include<iostream> #inc ...
- POJ 2456 3258 3273 3104 3045(二分搜索-最大化最小值)
POJ 2456 题意 农夫约翰有N间牛舍排在一条直线上,第i号牛舍在xi的位置,其中有C头牛对牛舍不满意,因此经常相互攻击.需要将这C头牛放在离其他牛尽可能远的牛舍,也就是求最大化最近两头牛之间的距 ...
- poj 3258 River Hopscotch 题解
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- E - River Hopscotch POJ - 3258(二分)
E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...
- poj 3258 River Hopscotch(二分+贪心)
题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...
- POJ 3258 River Hopscotch 二分枚举
题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...
- poj 3258 River Hopscotch 【二分】
题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...
- POJ 3258(二分求最大化最小值)
题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...
随机推荐
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- 1F - A+B for Input-Output Practice (III)
Your task is to Calculate a + b. Input Input contains multiple test cases. Each test case contains a ...
- python 计时程序运行时间
import time time_start=time.time() time_end=time.time() print('totally cost',time_end-time_start)
- Lazarus下面的javascript绑定另外一个版本bug修正
Lazarus下面的javascript绑定另外一个版本bug修正 从svn 检出的代码有几个问题 1.fpcjs.pas 单元开始有 {$IFDEF FPC} {$MODE delphi} {$EN ...
- abp项目中无法使用HttpContext.Current.Session[""]的问题
web项目Global.asax.cs中加入如下代码 public override void Init() { this.PostAuthenticateRequest += (sender, e) ...
- mongoDB(Linux)
启动 service mongod start 安装好后,输入mongo进入控制台 创建数据库 use baseName db.createCollection("game_record& ...
- JDK8集合类源码解析 - HashSet
HashSet 特点:不允许放入重复元素 查看源码,发现HashSet是基于HashMap来实现的,对HashMap做了一次“封装”. private transient HashMap<E,O ...
- Ubuntu下Tomcat绑定80端口(zz)
Ubuntu下Tomcat绑定80端口 来源:本站转载 作者:佚名 时间:2011-02-22 TAG: 工作环境迁移到了Ubuntu,很多东西发生了变化,比如原先配置tomcat端口.只需要配置se ...
- mybatis不报错,但是查询结果为0
[转载]https://blog.csdn.net/shenzhenNBA/article/details/46673327 在用MyBatis操作数据库的时候相信很多人都用到,当在判断null, 大 ...
- 【Linux】CentOS 7.2 安装 MySQL 5.7.21 解压版
安装环境/工具 1.Linux(CentOS 7.2版) 2.mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 安装步骤 1.下载mysql解压版(mysql-5. ...