UVALive - 3635 - Pie(二分)

题意:有F + 1(1 <= F <= 10000)个人分N(1 <= N <= 10000)个圆形派,每个人得到的派面积相同,且必须是一整块(不能够两个甚至多个派拼在一起),求每个人最多能得到多大面积的派。(误差最多到0.001)

因为答案是小数类型的,并且N高达10000,故不可暴力枚举。

可以二分枚举最大面积,然后检查是否切出来派的总个数大于等于F + 1。

(判相等时不可直接判相等,需要加精度控制)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
#define pr(x) cout << #x << " : " << x << "   "
#define prln(x) cout << #x << " : " << x << endl
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
const ll MOD = 1e9 + 7;
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;  

int T, n, f;
double a[MAXT];  

bool judge(double area){
    int sum = 0;
    for(int i = 0; i < n; ++i)  sum += int(a[i] / area);
    return sum >= f;
}  

int main(){
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &f);
        for(int i = 0; i < n; ++i){
            scanf("%lf", a + i);
            a[i] = a[i] * a[i] * pi;
        }
        ++f;
        double l = 0.0, r = *max_element(a, a + n);
        while(l + EPS < r){
            double mid = (l + r) / 2;
            if(judge(mid))  l = mid;
            else  r = mid;
        }
        printf("%.4lf\n", l);
    }
    return 0;
}  
时间: 2024-08-17 19:05:06

UVALive - 3635 - Pie(二分)的相关文章

uvalive 3635 Pie

https://vjudge.net/problem/UVALive-3635 题意: 有F+1个人要分n个蛋糕,他们得到的蛋糕的面积必须是一样的,但是每个蛋糕必须是整块的蛋糕,而不是有多块蛋糕拼成的,蛋糕的形状也可以不相同. 给出n块蛋糕各自的半径,求他们每个人能得到的蛋糕的最大面积. 思路: 使得最小值最大,那显然是二分. 二分半径,计算面积,然后枚举每个蛋糕,计算每个蛋糕可以分出来的当前面积的蛋糕会有多少个,总数是否大于等于F+1即可. 记住计算个数的时候要用floor向下取整函数,而且l

UVALive 3635 Pie(二分法)

简单的二分法应用,循环1000次精度就满足要求了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm>

LA 3635 - Pie 【二分】

Regionals 2006 >> Europe - Northwestern 3635 - Pie Time limit: 3.000 seconds My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are com

【hoj】2651 pie 二分查找

二分查找是一个很基本的算法,针对的是有序的数列,通过中间值的大小来判断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找到一个满足一些约束的值时就可以用二分查找,时间复杂度O(log n); 题目:http://acm.hit.edu.cn/hoj/problem/view?id=2651 因为题目有精度要求,对于浮点数小数点部分会有一定误差,所以可以选择将这些有小数部分的数值扩大e6倍,因为题目要求精确到e-3,之后

UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

先从中找出性能最好的那个数, 在用钱比较少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; map<string,int> vic;//以字符映射数字 int end,start; int num; int

POJ 3122 Pie 二分答案

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9653   Accepted: 3478   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of v

poj 3122 Pie (二分)

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18pt;">Description</span> My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of

POJ 3122 Pie (二分+精度)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11240   Accepted: 3919   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

LA 3635 Pie

二分答案 找到最大的圆的面积作为每个人可能分到的最大的面积. 对每个人可能分到的面积二分 验算时,求出每个pie可以切出的最大块数,然后总的块数和需要的块数比较 PS:(就是精度恶心) #include <map> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <algorith