815 - Flooded!

来源:互联网 发布:怎么跟网络管理员联系 编辑:程序博客网 时间:2024/06/06 20:25

Toenable homebuyers to estimate the cost of flood insurance, a real-estate firmprovides clients with the elevation of each 10-meter by 10-meter square of landin regions where homes may be purchased. Water from rain, melting snow, andburst water mains will collect first in those squares with the lowestelevations, since water from squares of higher elevation will run downhill. Forsimplicity, we also assume that storm sewers enable water from high-elevationsquares in valleys (completely enclosed by still higher elevation squares) todrain to lower elevation squares, and that water will not be absorbed by theland.


From weather data archives, we know the typical volume of water that collectsin a region. As prospective homebuyers, we wish to know the elevation of thewater after it has collected in low-lying squares, and also the percentage ofthe region's area that is completely submerged (that is, the percentage of10-meter squares whose elevation is strictly less than the water level). Youare to write the program that provides these results.

Input 

The input consists of asequence of region descriptions. Each begins with a pair of integers,m and n,each less than 30, giving the dimensions of the rectangular region in 10-meterunits. Immediately following are m lines of n integersgiving the elevations of the squares in row-major order. Elevations are givenin meters, with positive and negative numbers representing elevations above andbelow sea level, respectively. The final value in each region description is aninteger that indicates the number of cubic meters of water that will collect inthe region. A pair of zeroes follows the description of the last region.

Output 

For each region, displaythe region number (1, 2, ...), the water level (in meters above or below sealevel) and the percentage of the region's area under water, each on a separateline. The water level and percentage of the region's area under water are to bedisplayed accurate to two fractional digits. Follow the output for each regionwith a blank line.

Sample Input 

3 3

25 37 45

51 12 34

94 83 27

10000

0 0

Sample Output 

Region 1

Water level is 46.67meters.

66.67 percent of the region isunder water.

 

 

真正核心的计算海拔的算法只想出两种:

第一个解法:把所有格子按海拔顺序排序,把每一档海拔与下一档之间能够容纳的水依次累加直到大于水的总体积。

第二个解法:把所有格子按海拔顺序排序,逐个计算含水海拔,第n个格子的含水海拔=(总水量+n个格子相对于0海拔的体积)/n个格子的面积和,顺序读入后面格子海拔,如果此格高于已算出的含水海拔则终止。

 

题解代码:

方法一:

#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

    cout.setf(ios::fixed,ios::floatfield);

    cout.precision(2);

    int r,c;

    int casenumber=1;

   while(cin>>r>>c&&r!=0&&c!=0)

    {

        double number=0;

        double height=0;

        double a[r*c];

        double suma=0;

        for(int i=0;i<r*c;i++)

        {

            cin>>a[i];

            suma+=a[i];

        }

        double volume;

        cin>>volume;

        double sumh=volume/100;

        sort(a,a+r*c);

        double b[r*c-1];

        for(int i=0;i<r*c-1;i++)

        {

            b[i]=a[i+1]-a[i];

        }

        double sumb=0,sum=0;

        for(int i=0;i<r*c-1;i++)

        {

            sumb=sumb+(i+1)*b[i];

            sum=sum+a[i];

            if(sumb==sumh)

            {

                height=a[i+1];

                number=i+1;

                break;

            }

            else if(sumb>sumh)

            {

                height=(sumh+sum)/(i+1);

                number=i+1;

                break;

            }

        }

        if(sumb<sumh)

        {

            height=(sumh+suma)/(r*c);

            number=r*c;

        }

        double rate=number/(r*c)*100;

        cout<<"Region"<<casenumber<<endl;

        cout<<"Water level is"<<height<<" meters.\n";

        cout<<rate<<" percentof the region is under water.\n\n";

        casenumber++;

    }

}

方法二:

#include<iostream>

#include<iomanip>

#include<vector>

#include<algorithm>

using namespacestd;

 

int main()

{

    int m,n;

    int kase=1;

   while(cin>>m>>n&&m&&n)

    {

        vector<double> high;

        for(int i=0;i<m*n;i++)

        {

            int j;

            cin>>j;

            high.push_back(j);

        }

        double vol;

        cin>>vol;

        vol=vol/100;

 

        sort(high.begin(),high.end());

        double sum=0;

        double num;

        bool flag=false;

        double average;

        for(int i=0;i<m*n-1;i++)

        {

            sum=sum+high[i];

            if((sum+vol)/(i+1)<=high[i+1])

            {

                num=i+1;

                average=(sum+vol)/(i+1);

                flag=true;

                break;

            }

        }

        if(!flag)

        {

            sum=sum+high[m*n-1];

            num=m*n;

            average=(sum+vol)/(m*n);

        }

        cout<<"Region"<<kase<<endl;

        kase++;

       cout<<fixed<<setprecision(2)<<"Water level is"<<average<<" meters.\n";

        cout<<(num*100/(m*n))<<"percent of the region is under water.\n\n";

    }

    return 0;

}

0 0
原创粉丝点击