B - Pie (二分)
My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.
What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.
InputOne line with a positive integer: the number of test cases. Then for each test case:
---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.
OutputFor each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).Sample Input
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
Sample Output
25.1327
3.1416
50.2655
题解:
每个人能分到的最大的面积max=总的面积/(朋友分数目+1)
begin:
left = 0; right = max;
mid = (left + right) / 2;
judge:
for(int i = 0; i < n; i++)
num += 每块饼的面积 / mid;
finally:
while(right - left > 1e-6)
虽然明白题意,但是自己写的代码,还是找不到bug在哪?
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define PI acos(-0.1)
using namespace std; int n, f, a[];
bool solve(double m)
{
int num=;
for(int i = ; i < n; i++)
num += int(a[i]*a[i]*PI/m);
if(num >= f+)
return true;
else
return false;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
double mid, left, right, sum=0.0;
scanf("%d %d", &n, &f);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
for(int i = ; i < n; i++)
sum += a[i]*a[i]*PI;
right = sum/f;
left = 0.0;
while((right - left) > 0.000001)
{
mid = (right+left)/;
if(solve(mid))
left = mid;
else
right = mid;
//printf("%d\n", mid);
}
printf("%.4f\n", mid);
} return ;
}
AC代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
double pi = acos(-1.0);
int F,N;
double V[];
bool test(double x)
{
int num=;
for(int i = ; i < N;i++)
{
num += int(V[i]/x);
}
if(num>=F)
return true;
else return false;
}
int main()
{
int t,r;
double v,max,left,right,mid;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&F);
F = F+;
for(int i = ; i < N; i++)
{
scanf("%d",&r);
V[i] = pi*r*r;
v += V[i];
}
max = v/F;
left = 0.0;
right = max;
while((right-left)>1e-)//注意这里的精度问题。
{
mid = (left+right)/;
if(test(mid))
left = mid;
else right = mid;
}
printf("%.4f\n",mid);
}
return ;
}
B - Pie (二分)的更多相关文章
- HDU 1969 Pie(二分查找)
Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...
- HDU 1969 Pie(二分,注意精度)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 【POJ 3122】 Pie (二分+贪心)
id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...
- Pie(二分)
http://poj.org/problem?id=3122 题意:将n个圆柱体的不同口味的pie分给m个人,要求每个人分得的pie必须体积相同,且来自于一块pie(即:只分得一种口味的pie),求最 ...
- Pie(二分POJ3122)
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12985 Accepted: 4490 Special Ju ...
- PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...
- 【hoj】2651 pie 二分查找
二分查找是一个非常主要的算法,针对的是有序的数列,通过中间值的大小来推断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找 ...
- HDU1969:Pie(二分)
Pie Time Limit : 5000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissio ...
- UVaLive 3635 Pie (二分)
题意:有f+1个人来分n个圆形派,每个人得到的必须是一个整块,并且是面积一样,问你面积是多少. 析:二分这个面积即可,小了就多余了,多了就不够分,很简单就能判断. 代码如下: #pragma comm ...
随机推荐
- GPIO编程2:使用GPIO监听中断完整程序
一个完整的使用GPIO捕捉中断的程序: #include<stdlib.h> #include<stdio.h> #include<string.h> #inclu ...
- java代码从键盘输入执行次数,数,然后排序
总结:实现从键盘控制执行次数,困惑我很久,直到昨日在提问时,网友说通过循环是肯定可以的所以顿悟了 package com.c2; import java.util.Arrays; import jav ...
- websocket之一:websocket简介
Websocket websocket为一次HTTP握手后,后续通讯为tcp协议的通讯方式. WebSocket 使用一种被称作“Upgrade handshake(升级握手)”的机制将标准的 HTT ...
- AngularJS:模块
ylbtech-AngularJS:模块 1.返回顶部 1. AngularJS 模块 模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. ...
- c语言-顺序表
在数据结构中包含两种,一种线性结构(包括顺序表,链表,栈,队列),一种非线性结构(树,图), 顺序表,其实就是在内存动态数组,Java中的ArrayList就是一个典型的顺序表,它在顺序表的基础上增加 ...
- qq图片选择效果的处理
QQ中图片鼠标一选择,整个图片就像加了个阴影一样,这个效果一般人都不会注意,突然没事测试了一下,原来qq是把原来每个像素的颜色变成了相反的颜色. 电脑中的三原色为0-255,中间值为128,以中间值为 ...
- 2015.12.14 MDI(多文档窗口结构)设置基本解决,折腾一天,部分解决存在已久的问题。但效果仍不如临时航线的MDI窗体结构。
创建从一个窗口弹出多个子窗口的结构叫MDI窗体结构 如果不按MDI结构管理,最简单的做法是: 在窗体A上添加菜单或按钮,在菜单或按钮事件中添加弹出B窗体代码: B b = new B(); b.sho ...
- WCF上传大文件处理方法
<system.serviceModel> <bindings> <basicHttpBind> <Binding name=" maxReceiv ...
- ABP模块配置
介绍 我们知道ABP中模块的配置都是通过模块的Configuration属性来设置的.例如在模块的生命周期方法中可以进行一系列的配置 审计 MQ Redis....也可以替换一些ABP默认配置 通常我 ...
- select 动态添加option函数
转自:https://lym6520.iteye.com/blog/309937 经常会用到select动态添加元素,写了个方法,方便调用! ... /** * 功能:select对象动态添加Opt ...