梯田AGAIN
Time Limit: 5000 MS | Memory Limit: 65536 KB |
Total Submissions: 95 | Accepted: 21 |
Description
奴隸們去年嘗試了一下,結果發現,由於土質太過鬆軟,水能夠透過土地滲入到相鄰的梯田,即對於海嘯高度h,梯田中所有小於等於h的土地都會由於土質鬆軟而被被淹沒。
現在給你一個n*m的矩陣,代表梯田中每塊田地的高度。然後給定q個詢問,每個詢問給定一個海嘯高度h,問在此高度下,不被淹沒的梯田數量是多少。
Input
對於每組測試數據:
第一行三個數字n,m,q,表示梯田的行數,列數和詢問數。
之後n行,每行m個數字,表示每塊田地的高度,梯田高度不大於1000000。
之後q行,每行給出一個海嘯高度h,問大於這個高度的梯田有多少塊。
0<T<20。
0<n,m<=200。
0<=q<1000。
0<=h<=1000000.
Output
Sample Input
2 2 2
1 2
3 4
2
3
2 3 3
1 2 3
3 4 5
0
4
5
Sample Output
1
6
1
0
Hint
另外,由於測評環境爲xp,取消對stdio同步對流式輸入輸出效率可能是不起作用的,請大家務必注意。若懷疑某題存在卡IO的可能,請使用C式io函數或者用io加速外掛進行Io操作。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[40010], s;
int search(int start, int end, int key)
{
int mid, pos = 0;
while (start < end)
{
mid = (start + end) / 2;
if (a[mid] > key) {
end = mid;
pos = end;
}else {
start = mid + 1;
pos = start;
}
}
return pos;
}
int main(int argc, char const *argv[])
{
int T, n, m, q, h, i, j;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &q);
s = n * m;
for (i=0; i<s; i++)
scanf("%d", &a[i]);
sort(a, a+s);
while (q--)
{
scanf("%d", &h);
if (h < a[0]) printf("%d\n", s);
else if (h > a[s-1]) printf("%d\n", 0);
else
printf("%d\n", s - search(0, s-1, h));
}
}
return 0;
}