zoukankan      html  css  js  c++  java
  • uvA Flooded!

    Description

    To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients with the 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 the lowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we also assume that storm sewers enable water from high-elevation squares in valleys (completely enclosed by still higher elevation squares) to drain to lower elevation squares, and that water will not be absorbed by the land.  From weather data archives, we know the typical volume of water that collects in a region. As prospective homebuyers, we wish to know the elevation of the water after it has collected in low-lying squares, and also the percentage of the region's area that is completely submerged (that is, the percentage of 10-meter squares whose elevation is strictly less than the water level). You are to write the program that provides these results. 

    Input

    The input consists of a sequence 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-meter units. Immediately following are m lines of n integers giving the elevations of the squares in row-major order. Elevations are given in meters, with positive and negative numbers representing elevations above and below sea level, respectively. The final value in each region description is an integer that indicates the number of cubic meters of water that will collect in the region. A pair of zeroes follows the description of the last region.
     

    Output

    For each region, display the region number (1, 2, ...), the water level (in meters above or below sea level) and the percentage of the region's area under water, each on a separate line. The water level and 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.
    


    先对所有海拔排序,从小到大
    然后,选取中间的海拔作为最终填满T1体积水后的海拔高度,判断其与T的关系,若T1>T,选取海拔较小的海拔,填满T2体积水。
    直到得到两相邻海拔高度的T1,T2,使得(T1-T)*(T2-T)<0,则说明最后的水的高度在T1和T2之间。
    考虑到数据个能比较多,采用二分法做该题。
    但是,最后一直 结果错误,以下为采用二分法的错误代码
    情况很多,细节也很多。
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    const int maxn = 30*30+5;
    int s[maxn], sum[maxn],m,n;    //s为各个网格的海拔高度,有正有负
    
    void dataout(double a1,double a2,double ss,double numm,double T)
    {
        //cout<<a1<<" "<<a2<< " "<<ss<< " "<<numm<<endl;
        if(numm == 0)numm=1;
        double height = ss + (a1 - a2) / numm , per;
        if(T == 0)per=0;
        else per = 100 * numm / (m*n) ;
        cout<<setiosflags(ios::fixed)<<setprecision(2);
        cout << "Water level is " << height << " meters."<<endl;
        cout << per << " percent of the region is under water."<<endl;
    }
    
    void deal2(double T)
    {
        sort(s, s + m * n);
        int a = 0, b = n * m - 1,mid;     //用二分法从中间向两边计算
        while(1){
                mid = (a + b) / 2;
                if(sum[mid]!=0)sum[mid]=0;
                for(int i = 0;i < mid; i++){
                    if(s[i] < s [mid]) {
                        sum[mid] += (s[mid] - s[i]);
                    }
                }
                if (sum[mid] < T) a = mid;
                else b = mid;
                if(abs(a-b)<=1)break;
        }
        if((sum[a-1]-T)*(sum[a]-T) < 0&&a!=0){
            if(sum[a-1] > T) dataout(T,sum[a],s[a],a-1,T);
            else dataout(T,sum[a-1],s[a-1],a,T);
        }
        else{
            if(sum[a+1] > T) dataout(T,sum[a],s[a],a+1,T);
            else dataout(T,sum[a+1],s[a+1],a,T);
        }
    }
    
    void deal1(double T)
    {
        sort(s, s + m * n);
        int i;
        bool flag = false;
        sum[0] = 0;
        for(i=1;i<n*m;i++){
            for(int j=0;j<i;j++){
                sum[i]+=(s[i] - s[j]);
                /*cout<<"i "<<i<<" j "<<j<<endl;
                cout<<sum[i]<<" = "<<s[i]<<" - "<<s[j]<<endl;*/
            }
            if(sum[i] >= T){
                flag = true;
                break;
            }
        }
        /*cout<<i<<endl;
        cout<<"sum[i-1] "<<sum[0]<<"  sum[i] "<<sum[1]<<endl;*/
        if(flag){
            double height ,per;
            if(T == 0){
                per=0;
                height = s[0];
            }
            else {
                per = 100 * i / m / n;
                height = s[i-1] + (sum[i] - sum[i-1]) / i;
            }
            cout<<setiosflags(ios::fixed)<<setprecision(2);
            cout << "Water level is " <<height << " meters."<<endl;
            cout << per<<" percent of the region is under water."<<endl;
        }
        else{
            i--;
            /*cout<<"false"<<endl;
            cout<<"i "<<i<<endl;
            cout<<" s[i-1] "<<s[i-1]<<" sum[i] "<<sum[i]<<endl;*/
            double height ,per;
            if(T == 0){
                per = 0;
                height = s[0];
            }
            else {
                per = 100;
                height = s[i] + (T - sum[i]) / (i + 1) ;
            }
            cout<<setiosflags(ios::fixed)<<setprecision(2);
            cout << "Water level is " <<height << " meters."<<endl;
            cout << per<<" percent of the region is under water."<<endl;
        }
    }
    
    int main()
    {
        int times=0;
        double T;
        while( cin >> n >> m ){
            if(n == 0)break;
            for(int i = 0;i < n*m;i++)cin >> s[i];
            cin >> T;
            cout<< "Region " << ++times << endl;
            memset(sum,0,sizeof(sum));
            if(m == 1&&n == 1){
                    double height = T/100 + s[0],per;
                    if(T == 0) per = 0;
                    else per = 100;
                    cout << setiosflags(ios::fixed) << setprecision(2);
                    cout << "Water level is " <<height << " meters."<<endl;
                    cout << per << " percent of the region is under water."<<endl;
            }
            else if(m*n<4)deal1(T/100);
            else deal2(T / 100);
        }
        return 0;
    }
    实在不知错在哪,就不用二分法试试。
    一次AC
    果然还是我想太多了,不用二分法也并没有超时。
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    const int maxn = 30*30+5;
    int s[maxn],m,n;    //s为各个网格的海拔高度,有正有负
    
    void dataout(double a1,double a2,double ss,double numm,double T)
    {
        if(numm == 0)numm=1;
        double height = ss + (a1 - a2) / numm , per = 100 * numm / (m*n);
        cout<<setiosflags(ios::fixed)<<setprecision(2);
        cout << "Water level is " << height << " meters."<<endl;
        cout << per << " percent of the region is under water."<<endl;
    }
    
    void deal(double T)
    {
    
        int a = 0,t1 = 0,t2 = 0,i;    
        for(i=1;i<n*m;i++){
            t2 = t1;
            t1 = 0;
            for(int j=0;j<i;j++)t1+=(s[i] - s[j]);
            if(t2< T&& (t1 > T||t1 == T))break;
            
        }    
        if(i == n*m) dataout(T,t1,s[i-1],i,T);
        else dataout(T,t2,s[i-1],i,T);
    }
    
    int main()
    {
        int times=0;
        double T;
        while( cin >> n >> m ){
            if(n == 0)break;
            for(int i = 0;i < n*m;i++)cin >> s[i];
            cin >> T;
            cout<< "Region " << ++times << endl;
            sort(s, s + m * n);
            if(T == 0){
                    double height = s[0], per = 0;
                    cout << setiosflags(ios::fixed) << setprecision(2);
                    cout << "Water level is " <<height << " meters."<<endl;
                    cout << per << " percent of the region is under water."<<endl;
            }
            else deal( T / 100 );
        }
        return 0;
    }
    
    
    
    
    
    


  • 相关阅读:
    redis持久化,主从及数据备份
    验证redis的快照和AOF
    Erlang中日志管理
    erlang tcp发包速度测试
    树(234树插入查找显示)
    JDBC(初步试用)
    树(二叉树的插入删除查找遍历)
    哈希表(链地址法插入删除)
    哈希表-再哈希法插入删除
    哈希表-线性探测插入删除
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5405739.html
Copyright © 2011-2022 走看看