uva 815 Flooded!

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.

中文:

給你一堆10×10的方塊組成的積木,給你一定量的水,水能從積木的最底層向上漫,問你最後水位高度是多少,有多少積方塊被淹沒。

代碼:

#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<utility>
#include <iomanip>
#include<climits>
using namespace std;

typedef long long ll;
const int maxn = 200001;
typedef pair<int, int> pii;


int a[31][31];
int mark[31][31];
int n, m;
double water;
map<int, int> mvp;
int main()
{
	ios::sync_with_stdio(false);

	
	int k = 0;
	while (cin >> n >> m, n + m)
	{
		mvp.clear();
		double level = INT_MAX;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> a[i][j];
				mark[i][j] = a[i][j];
				level = min(level, (double)a[i][j]);
				mvp[a[i][j]]++;
			}
		}
		cin >> water;
		water /= 100;
		auto cur = mvp.begin();
		while (true)
		{
			if (cur == mvp.end())//
				break;
			if (water == 0)
				break;
			auto next = cur;
			next++;
			if (next == mvp.end())//所有水位相同 water應該減去平均的往上加
			{
				int area = cur->second;//有多少個區域
				level += (water*1.0) / area;
				break;
			}
			else
			{
				int sub = next->first - cur->first;//水位差
				int cnt = cur->second;//當前待填補水位數量
				if (water >= cnt * sub)
				{
					water -= cnt * sub;//灌水到next的高度
					level = next->first;
					cur = mvp.erase(cur);//刪除掉灌完水的水位,此時應該cur == next
					cur->second += cnt;//新的水位數量更新

				}
				else
				{
					level += (water * 1.0) / cnt;
					break;
				}
			}
		}
		int pre = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (level > (double)mark[i][j])
					pre++;
			}
		}

		cout << "Region " << ++k << endl;
		cout << "Water level is ";
		cout << fixed << setprecision(2) << level << " meters." <<endl;
		//cout << fixed << setprecision(2) << ((double)(pre * 1.0) / (n*m) - 0.00005) * 100.0<< " percent of the region is under water." << endl;
		cout << fixed << setprecision(2) << ((double)(pre * 1.0) / (n*m)) * 100.0 << " percent of the region is under water." << endl<<endl;
	}
	
	//system("pause");
	return 0;
}

思路:

好久沒做uva上的題目了,比較簡單。

我的實現方法時,設置一個map,key值是水位高度,value值是當前水位高度的沒過的方塊數,初始化時以最最低的方塊開始計算。

由於map是按照key值排序的,所以正好可以按照水位高度從小到大計算,當前水位cur要如果要上漲,那就要找最少比cur高的最近方塊next作爲參考,更新水位高度,然後刪除當前水位高度cur即可。
使用map處理很方便。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章