暑假集训日记--8.10--二分+单调队列+练习赛

来源:互联网 发布:数据库副本 编辑:程序博客网 时间:2024/05/21 21:43

今天开了新的专题,上午做了三道二分的题目。有两道数学题,还有一道精度问题。

两道数学题就不贴代码了。

贴一道题吧。在这道题里我学会了控制π的精度。用三角函数。

题目如下:

Problem 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 various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though. 

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.
 

Input
One line with a positive integer: the number of test cases. Then for each test case:<ul><li>One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.<li>One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.</ul>
 

Output
For 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<sup>3</sup>.
 

Sample Input
33 34 3 31 24510 51 4 2 3 4 5 6 5 4 2
 

Sample Output
25.13273.141650.2655

源代码如下:


//1Pie

/*

 Npie分给F+1个人;

 每个人分到的只能是一个pie的一部分或者是整个,不能是不同的pie拼起来的。

 求能分到的最大值。

 

 二分法,在0和最大的体积之间二分,直到找到可以分且最大的值结束。

 注意精度的控制……好像很严格……

 *//*

#include <iostream>

#include <algorithm>

#include <cstring>

#include <stdio.h>

#include <iomanip>

#include <cmath>

using namespace std;

int main()

{

    const double pi = atan(1.0)*4;

    int F,N,n,r,i;

    int sum;

    double V[10005];

    scanf("%d",&n);

    while (n--)

    {

        double ans = 0;

        double max1 = 0;

        memset(V,0,sizeof(V));

        scanf("%d %d",&N,&F);

        for (i = 1; i <= N; i++)

        {

            scanf("%d",&r);

            V[i] = pi * r * r;

            if (V[i] > max1)

                max1 = V[i];

        }

        double l = 0.0;

        double r = max1;

        double mid;

        while (r - l > 0.00001)

        {

            mid = (l + r) / 2;

            sum = 0;

            for (i = 1; i <= N; i++)

            {

                sum += (int)(V[i]/mid);

            }

            if (sum >= F+1)

            {

                l = mid;

                ans = mid;

            }

            else

                r = mid;

        }

        cout << setiosflags(ios::fixed) << setprecision(4) << ans << endl;

    }

    return 0;

}


这里pi表示π,用三角函数进行高精度计算。也可以是pi = acos(-1.0)。


这个题没什么说的,二分就出来了。也算是复习了一下二分。

下午做了一场练习赛。居然有三角形的dp。题目比较简单A了四道但是大佬们都五六道……差距还是很明显的……

这些题就不贴代码了、


然后晚上又看了看单调队列。感觉并没有之前想的那么简单……还是不是很理解。

好像明白了单调队列的思想,但是还是不太明白代码是怎么实现的。

把单调队列想的太简单了……

明天继续看单调队列。争取能够深入理解一下。

然后刷题……

原创粉丝点击