# JXUFE紫书第四章习题4-10

xiaoxiao2021-02-28  6

To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients withthe elevation of each 10-meter by 10-meter square of land in regions where homes may be purchased.Water from rain, melting snow, and burst water mains will collect first in those squares with thelowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we alsoassume that storm sewers enable water from high-elevation squares in valleys (completely enclosed bystill higher elevation squares) to drain to lower elevation squares, and that water will not be absorbedby the land.

From weather data archives, we know the typical volume of water that collects in a region. Asprospective homebuyers, we wish to know the elevation of the water after it has collected in lowlyingsquares, and also the percentage of the region’s area that is completely submerged (that is, thepercentage of 10-meter squares whose elevation is strictly less than the water level). You are to writethe program that provides these results.

Input

The input consists of a sequence of region descriptions. Each begins with a pair of integers, m andn, each less than 30, giving the dimensions of the rectangular region in 10-meter units. Immediatelyfollowing are m lines of n integers giving the elevations of the squares in row-major order. Elevationsare given in meters, with positive and negative numbers representing elevations above and below sealevel, respectively. The final value in each region description is an integer that indicates the number ofcubic meters of water that will collect in the region. A pair of zeroes follows the description of the lastregion.

Output

For each region, display the 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 separate line. The water leveland percentage of the region’s area under water are to be displayed accurate to two fractional digits.Follow the output for each region with 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.67 meters.

66.67 percent of the region is under water.

#include <stdio.h>  #include <string.h>  #define maxn 900  int i,j,cases=0,n,m,tot,t;  int a[maxn];  double sum,high,per;  int main()  {      while(~scanf("%d%d",&n,&m)&&n)      {          for(i=1;i<=n*m;i++)              scanf("%d",&a[i]);          scanf("%lf",&sum);          sum/=100;          for(i=1;i<n*m;i++)//选择排序          {              t=i;              for(j=i+1;j<=n*m;j++)                  if(a[j]<a[t]) t=j;              if(t!=i)              {                  int tmp=a[i];                  a[i]=a[t];                  a[t]=tmp;              }          }          for(i=2;i<=n*m;i++)          {              tot=(a[i]-a[i-1])*(i-1);              if(sum>=tot)              {                  sum-=tot;              }              else              {                  high=a[i-1]+sum/(i-1);                  sum=0;                  per=(i-1)*1.0/n/m;                  break;              }          }          if(sum)        {              high=a[n*m]+sum/n/m;              per=1;          }          printf("Region %d\n",++cases);          printf("Water level is %.2lf meters.\n",high);          printf("%.2lf percent of the region is under water.\n\n",per*100);      }      return 0;  }