water1
Time Limit: 1000MS
Memory Limit: 65536KB
Description
听说全球气候变暖,冰川融化,海水淹大地。着实好奇要融化多少冰川会淹没我的宿舍,哦不,淹没南京,反正NJUST应该总会是第一批被淹的。现将问题简化成一个二维坐标系的模型。有N个矩形块区域,每个区域有海拔(H)和宽度(W),区域按照顺序给出,比如有3个区域,第一个区域宽度为1,海拔为2,第二个区域宽度为5,海拔为6,第三个区域宽度为3,海拔为4,从图像上看就是像这样:(Y轴是海拔)8 7 6 +++++5 +++++4 ++++++++3 ++++++++2 +++++++++1 +++++++++ 123456789假设最左边和最右边都是无限高的墙。为了简化问题,假设水流下来的速度是每秒一个单位的水,并且总是往区域一降水(某沿海地区)。现在请问要淹没所有的区域至少要多少时间(秒)?淹没的定义是,所有的区域表面至少覆盖一层水。如果没有区域则至少要放一滴水。上图例子中,淹没完如下:87 wwwwwwwww6 w+++++www5 w+++++www4 w++++++++3 w++++++++2 +++++++++1 +++++++++ 123456789所以需要19秒。
Input
多CASE,测试数据以EOF结尾,对于每个CASE:第一行一个整数N(0 <= N <= 10^5)。接下去N行每行对应两个整数H(1 <= H <= 10^5),W(1 <= W <= 10^5)分别对应第N个区域的海拔和宽度。
分析:求出最大矩形面积再依次减去各个小区域面积。
以下是直接计算每个区域的覆盖雨量。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define N 100020
#define inf 0x3f3f3f3f
#define ll long long
ll w[N], h[N];
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
ll t, n, i, j, k, water, ww, max_h;
while (~scanf("%lld", &n)){
max_h = ww = 0;
k = water = 0;
for (i = 0; i < n; i++){
scanf("%lld%lld", &h[i], &w[i]);
max_h = max(h[i], max_h);
}
for (i = 0; i < n; i++){
water += (max_h+1 - h[i])*w[i];
}
if (n == 0) water = 1;
printf("%lld\n", water);
}
return 0;
}
求出最大矩形面积再减去各个小区域面积和。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define N 1020
#define inf 0x3f3f3f3f
#define ll long long
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
ll t, n, i, j, k, water, w, ww, h, max_h;
while (~scanf("%lld", &n)){
max_h = ww = 0;
k = 0;
for (i = 0; i < n; i++){
scanf("%lld%lld", &h, &w);
max_h = max(h, max_h);
ww += w;
k += h*w;
}
water = (max_h + 1)*ww - k;
if (n == 0) water = 1;
printf("%lld\n", water);
}
return 0;
}