Description
Input
Output
Sample Input
2002 4920
2003 5901
2004 2832
2005 3890
2007 5609
2008 3024
5
2002 2005
2003 2005
2002 2007
2003 2007
2005 2008
Sample Output
true
false
maybe
false
HINT
100%的数据满足:1<=n<=50000, 1<=m<=10000, -10^9<=yi<=10^9, 1<=ri<=10^9
Source
这题写了一天总算过了……不能说难,但是细节比较多……
对于一组询问Y和X(也即从Y年到X年),我们来分析什么时候答案为”true“:
1、从Y到X年的降雨量都已知
2、X年的降雨量不超过Y年的降雨量
3、从Y+1到X-1年的降雨量都小于X年的降雨量
只有上述3个条件都满足,答案才为"true”。
接下来考虑什么时候答案为"maybe“:
1-1、Y年和X年的降雨量已知
1-2、X年的降雨量不超过Y年的降雨量
1-3、从Y+1到X-1年中存在至少一年的降雨量未知
1-4、从Y+1到X-1年中已知的降雨量都小于X年的降雨量
2-1、Y年和X年中有且仅有一年的降雨量未知
2-2、从Y+1到X-1年中已知的降雨量都小于X年的降雨量
3、Y年和X年的降雨量都未知
以上三组条件中只要满足任意一组,答案即为"maybe"。
至于剩下的情况,自然就是"false"了。
实现方面,我们可以预处理一个ST表来做RMQ,同时二分找出年份对应数组中的位置(题目中年份的取值从-10^9~10^9……),再各种判断就行了
代码(这个代码是BZOJ上交的,注意POJ上有多测):
//BZOJ1067; WorstWeather Ever (SCOI2007); RMQ-ST
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <algorithm>
#define N 100000
#define LOGN 18
int n, st[LOGN + 1][N + 1], q, x, y, d[N + 1], a[N + 1], ans, logn, Log[N + 1];
int query(int l, int r)
{
if (l > r) return INT_MIN;
int len = Log[r - l + 1];
return std::max(st[len][l], st[len][r - (1 << len) + 1]);
}
void build()
{
logn = Log[n];
for (int i = 0; i <= n; ++i) st[0][i] = a[i];
for (int i = 1; i <= logn; ++i)
for (int j = 1; j <= n - (1 << i) + 1; ++j)
st[i][j] = std::max(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
}
//lower_bound returns the first elements not less than x (>=)
inline int getpos(int x)
{ return std::lower_bound(d + 1, d + n + 1, x) - d; }
int main()
{
for (int i = 1, j = 1, k = -1; i <= N; ++i)
if (i == j) Log[i] = ++k, j <<= 1;
else Log[i] = k;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", d + i, a + i);
scanf("%d", &q);
build();
while (q--)
{
scanf("%d%d", &x, &y);
int l = getpos(x), r = getpos(y), m;
bool lx = l <= n && d[l] == x, rx = r <= n && d[r] == y;
if (!rx) --r;
if (lx)
if (rx)
{
m = query(l + 1, r - 1);
if (a[l] < a[r]) ans = 0;
else
if (m < a[r])
if (r - l == y - x) ans = 1;
else ans = -1;
else ans = 0;
}
else
{
m = query(l + 1, r);
if (m < a[l]) ans = -1;
else ans = 0;
}
else
if (rx)
{
m = query(l, r - 1);
if (m < a[r]) ans = -1;
else ans = 0;
}
else ans = -1;
if (ans == 1) printf("true\n");
else if (!ans) printf("false\n");
else printf("maybe\n");
}
return 0;
}
本文介绍了一个用于查询指定年份间最大降雨量的算法,通过预处理ST表进行快速查询,并结合二分查找定位年份,实现了高效判断指定年份是否为某区间内降雨量最大的年份。
1540

被折叠的 条评论
为什么被折叠?



