Pond Cascade Gym - 101670B 解题报告

版权声明:大鹏专属 https://blog.csdn.net/NCC__dapeng/article/details/88030398

题目:

The cascade of water slides has been installed in the park recently and it has to be tested. The cascade consists of some number of reservoirs, or “ponds” for short, which are linked into a single sequence connected by water slides. Visitors are expected to start the journey in the topmost pond, then to be washed into subsequent lower ponds, and finally end the journey in the last pond of the cascade, which is also the lowest pond of the cascade. The journey spans all ponds in the cascade.

To test the cascade, each pond has to be completely filled with water. Each pond is attached to a pipe with a valve which, when opened, pours water into the pond. The sizes and capacities of different ponds are different. Fortunately, at least the pipes and the valves are standardized. Therefore, the rate at which water is poured through the valve into a pond is the same for all ponds.

When a pond is filled, water overflows and continues to flow into the next pond. If that pond is also filled, water continues to the next subsequent pond, and so on, until it either reaches some pond which has not been completely filled yet, or it overflows the lowest pond and sinks in the drain at the bottom of the cascade. The time in which the overflowing water reaches the next pond is considered to be negligible. The test starts at time 0 with all ponds being empty. Then, all valves are opened simultaneously. The test stops and the valves are closed when all ponds are filled by water.

The pond capacities and valves flow rate are known, you have to determine the moment when the lowest pond starts to overflow and the first moment when all ponds are filled.

Input Specification:

There are more test cases. Each test case consists of two lines. The first line contains two integers N (1 ≤ N ≤ 105 , 1 ≤ F ≤ 109 ) separated by space. N is the number of ponds, F is the rate of water flow through each valve. We consider the flow to be expressed in litres per second. The second line contains a sequence of N integers Ci (1 ≤ Ci ≤ 109 ) separated by spaces and representing the pond capacities expressed in litres. The sequence reflects the order of ponds from the topmost one to the lowest one.

Output Specification:

For each test case, print a single line with two numbers separated by space. The first number represents the time in which the lowest pond in the cascade starts to overflow. The second number represents the duration of the test. Both times should be printed in seconds and should be accurate within an absolute or relative error of 10-6.

题目大意:

从上到下给出N个池塘,每个池塘都有水管与它相连,每个水管的流量都是flow,当上一层的池塘的水满了之后,该池塘剩余流入的水就会依次流入下边的池塘中,问最后一个池塘灌满的时间和全部池塘被灌满的时间。

思路:

这道题一开始拿过来首先想到的是贪心,但是错了,赛后看了看问题才知道,在统计所有池塘都被水填满的时候,是直接把所有池塘的容量加起来然后除以流速乘以N,想了想当时可能是傻了,因为下面的水塘先满对上面没有被填满的水塘是没有影响的,可能我就是个弟弟,至于最后的那个鱼塘只需要逆序一遍即可。之后看了看别人的代码,有很多人使用了二分枚举,个人对这一部分一直比较生疏,所以在这里也对二分求解进行了整理,大体的步骤是枚举很可能完成的时间段,然后进行多次二分,注意这里要进行次数的规范否则会出现死循环,以二分的搜索效率100-50次足以对很大的数据进行很多次的搜索了。

下面给出两种思想的AC代码:

贪心:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e6+100;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
ll n,flow,c[maxn];

double judgegre_all()
{
    double time=-1e9;
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=c[i];
        time=max(time,sum*1.0/(flow*i));
    }
    return time;
}

double judgegre_final()
{
    double time=1e9;
    ll sum=0;
    for(int i=n;i>=1;i--)
    {
        sum+=c[i];
        time=min(time,sum*1.0/(flow*(n-i+1)));
    }
    return time;
}


int main()
{
    //ios::sync_with_stdio(false);
    while(scanf("%lld %lld",&n,&flow)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%lld",&c[i]);

        double time_all=judgegre_all();

        double time_final=judgegre_final();

        printf("%.6f %.6f\n",time_final,time_all);

    }

    return 0;

}

二分枚举:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e6+100;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
ll n,flow,c[maxn];

bool judgegre_all(double t)
{
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=c[i];
        if(sum>flow*t*i) return false;
    }
    return true;
}

double judgegre_final(double t)
{
    if(judgegre_all(t)) return true;
    ll sum=0;
    for(int i=n;i>=1;i--)
    {
        sum+=c[i];
        if(t*flow*(n-i+1)>sum) return true;//注意这里的判断条件只要有一次满足就符合
    }
    return false;
}


int main()
{
    //ios::sync_with_stdio(false);
    while(scanf("%lld %lld",&n,&flow)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%lld",&c[i]);

        double l=0,r=INF,mid,time_all,time_final;

        for(int i=1;i<=100;i++)
        {
            mid=(l+r)/2;
            if(judgegre_all(mid)) r=mid;
            else l=mid;
        }

        time_all=mid;

        l=0,r=INF;

        for(int i=1;i<=100;i++)
        {
            mid=(l+r)/2;
            if(judgegre_final(mid)) r=mid;
            else l=mid;
        }

        time_final=mid;

        printf("%.6f %.6f\n",time_final,time_all);

    }

    return 0;

}

猜你喜欢

转载自blog.csdn.net/NCC__dapeng/article/details/88030398