POJ 3122 & 3258 & 3273 #二分
以下三道都是经典二分,道理都差不多,代码就贴在一起了。
POJ 3122:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define PI 3.14159265359 //ÓÃ3.1415926»áWA¡£¡£¡£
double pie[10005]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,f,r;
double maxp=0.0;
scanf("%d%d",&n,&f);
f++;
for(int i=0;i<n;i++)
{
scanf("%d",&r);
pie[i]=r*r*PI;
maxp=max(maxp,pie[i]);
}
double left=0.0,right=maxp;
while(left+1e-6<right)
{
double mid=(left+right)/2.0;
int sum=0;
for(int i=0;i<n;i++)
sum+=floor(pie[i]/mid);
if(sum>=f)
left=mid;
else right=mid;
}
printf("%.4lf\n",left);
}
return 0;
}
POJ 3258:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; int main()
{
int L,n,m,d[50005];
while(~scanf("%d%d%d",&L,&n,&m))
{
int l=L,r=L,mid;
d[0]=0; d[n+1]=L;
for(int i=1;i<=n;i++)
scanf("%d",&d[i]);
sort(d,d+n+2);
for(int i=1;i<=n+1;i++)
l=min(l,d[i]-d[i-1]); while(l<r)
{
mid=(l+r)>>1;
int cnt=0,sum=0;
for(int i=1;i<=n+1;i++)
{
if((sum+=(d[i]-d[i-1]))<=mid) //注意加括号
cnt++;
else sum=0;
}
if(cnt>m)
r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}
POJ 3273:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; int main()
{
int n,m,pay[100005];
while(~scanf("%d%d",&n,&m))
{
int l=0,r=0;
for(int i=0;i<n;i++)
{
scanf("%d",&pay[i]);
l=max(l,pay[i]);
r+=pay[i];
}
while(l<r)
{
int cnt=0,sum=0;
int mid=(l+r)/2;
for(int i=0;i<n;i++)
{
sum+=pay[i];
if(sum>mid)
{
cnt++;
sum=pay[i];
}
}
if(cnt<m)
r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}
POJ 3122 & 3258 & 3273 #二分的更多相关文章
- 【POJ 3122】 Pie (二分+贪心)
id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...
- POJ - 3122 Pie(二分)
http://poj.org/problem?id=3122 题意 主人过生日,m个人来庆生,有n块派,m+1个人(还有主人自己)分,问每个人分到的最大体积的派是多大,PS每 个人所分的派必须是在同一 ...
- POJ 2456 3258 3273 3104 3045(二分搜索-最大化最小值)
POJ 2456 题意 农夫约翰有N间牛舍排在一条直线上,第i号牛舍在xi的位置,其中有C头牛对牛舍不满意,因此经常相互攻击.需要将这C头牛放在离其他牛尽可能远的牛舍,也就是求最大化最近两头牛之间的距 ...
- POJ 3122 Pie【二分答案】
<题目链接> 题目大意: 将n个半径不一但是高度为1的蛋糕分给 F+1个人,每个人分得蛋糕的体积应当相同,并且需要注意的是,每个人分得的整块蛋糕都只能从一个蛋糕上切下来,而不是从几个蛋糕上 ...
- POJ 3122 Pie(二分+贪心)
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22684 Accepted: 7121 Special Ju ...
- poj 3122 (二分查找)
链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- poj 3258 3273
poj3258 题目 (最大化最小值)(最小值最大化) 题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,现在去掉M块石头,要求去掉M块石 ...
- E - River Hopscotch POJ - 3258(二分)
E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...
- POJ 3122 Pie 二分枚举
题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. #include <stdio.h> #include <ma ...
随机推荐
- DoTween基本使用
using UnityEngine; using System.Collections; using DG.Tweening; using UnityEngine.UI; public class D ...
- 【1】Hover 效果收集
各种 hover 效果 github repository>> git 仓库 1. 背景图的 hover 效果 原页面>> githubSite>>
- linux的基本命令
find: find [路径] [选项] [查找规则] [动作] -print 打印出找到的文件,以\n结束 -print0 打印出找到的文件,无换行 -exec command 对找到的文件执行命令 ...
- vultr新用户注册享受50美元优惠码,长期有效
vultr vps服务器,我用了三年多,购买了几十台vps,性价比非常高. 近期,vutlr推出了最新优惠码DOMORE长期有效,新用户注册账号时候,可在付款方式界面输入这个优惠码,享受50美元余额, ...
- android studio Activity标题栏研究
第一次研究时间:2016/7/30,以下研究主要存在于当前最新版本的android studio上.eclipse请参考 一.头部标题取消 当前版本新建工程在 application中默认主题为 an ...
- FTP上传下载工具(FlashFXP) v5.5.0 中文版
软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...
- cos距离与欧式距离
1. 欧氏距离(EuclideanDistance) 欧氏距离是最易于理解的一种距离计算方法,源自欧氏空间中两点间的距离公式. (1)二维平面上两点a(x1,y1)与b(x2,y2)间的欧氏距离: ( ...
- unity3D中重要函数
Update 当MonoBehaviour启用时,其Update在每一帧被调用. LateUpdate 当Behaviour启用时,其 LateUpdate在每一帧被调用. FixedUpdate 当 ...
- ios 显示其他app的购买页面
using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...
- some idea for my personal page
firstly, dump the old personal page source from Github to Dropbox.then the idea is: 1: make a fake s ...