首页 > 代码库 > 【UVA】815 - Flooded!

【UVA】815 - Flooded!

水题,排序之后依次累加,模拟。

14069284815Flooded!AcceptedC++0.0322014-08-20 10:42:17

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define esp 1e-9
#define INF 1 << 30
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===============KinderRiven===================
===========================================*/
#define MAXD 1000 + 10
int n,m;
int main(){
    int Case = 1;
    while(scanf("%d%d",&n,&m)){
        if(!n && !m)
            break;
        double array[MAXD],V;
        n = n * m;
        for(int i = 0 ; i < n ; i ++)
            scanf("%lf",&array[i]);
        scanf("%lf",&V);
        V /= 100;
        sort(array, array + n);
        int area = 0;
        while(V){
            int i,j;
            for(i = 0 ; i < n - 1; i ++){
                 if(array[i] < array[i + 1])
                    break;
            }
            area = i + 1;
            if(i == n - 1){
                double aver = V / n;
                V = 0;
                for(j = 0 ; j < n ; j++)
                    array[j] += aver;
            }
            else{
                double _V = (array[i + 1] - array[i]) * (i + 1);
                if(_V > V)
                    _V = V;
                V -= _V;
                double aver = _V / (i + 1);
                for(j = 0 ; j <= i ; j++)
                    array[j] += aver;
            }
        }
        printf("Region %d\n",Case++);
        printf("Water level is %.2f meters.\n",array[0]);
        printf("%.2f percent of the region is under water.\n",1.0 * area / n * 100);
        printf("\n");
    }
    return 0;
}