首页 > ACM题库 > HDU-杭电 > HDU 2969-Skyscrapers[解题报告]HOJ
2014
02-24

HDU 2969-Skyscrapers[解题报告]HOJ

Skyscrapers

问题描述 :

In a seaside village, there is an avenue of skyscrapers. Each skyscrapers is 100m wide and has certain height. Due to very high price of parcels, any two consecutive skyscrapers are adjacent.

The avenue lies close to the beach so the street is exactly at the sea level.

Unfortunately, this year, due to the global warming, the sea level started to increase by one meter each day. If the skyscraper height is no greater than the current sea level, it is considered flooded.

A region is a maximal set of non-flooded, adjacent skyscrapers. This term is of particular importance, as it is suficient to deliver goods (like current, carrots or cabbages) to any single skyscraper in each region.

Hence, the city major wants to know how many regions there will be in the hard days that come. An example of an avenue with 5 skyscrapers after 2 days is given below.

输入:

The input contains several test cases. The first line contains an integer t (t <= 15) denoting the number of test cases. Then t test cases follow. Each of them begins with a line containing two numbers n and d (1 <= n, d <=10^6), n is the number of skyscrapers and d is the number of days which the major wants
to query.

Skyscrapers are numbered from left to right. The next line contains n integers h1,h2,…, hn where 1 <= hi <= 10^9 is the height of skyscraper i. The third line of a single test case contains d numbers tj such that 0 <= t1 < t2 < … < td-1 < td <=10^9.

输出:

The input contains several test cases. The first line contains an integer t (t <= 15) denoting the number of test cases. Then t test cases follow. Each of them begins with a line containing two numbers n and d (1 <= n, d <=10^6), n is the number of skyscrapers and d is the number of days which the major wants
to query.

Skyscrapers are numbered from left to right. The next line contains n integers h1,h2,…, hn where 1 <= hi <= 10^9 is the height of skyscraper i. The third line of a single test case contains d numbers tj such that 0 <= t1 < t2 < … < td-1 < td <=10^9.

样例输入:

2
3 3
1 2 3
1 2 3
5 3
1 3 5 1 3
0 2 4

样例输出:

1 1 0
1 2 1

#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
struct P{
	int l;
	int i;
	P(){}
}p[1000000],q[1000000];
bool cmp(P a,P b){
	return a.l<b.l;
}
int in(){
	int r=0;
	char c=getchar();
	while(c>'9'||c<'0')c=getchar();
	do{
		r=r*10+c-'0';
		c=getchar();
	}while(c>='0'&&c<='9');
	return r;
}
int res[1000000];
bool fl[1000000];
int main(){
	int ca;
	scanf("%d",&ca);
	while(ca-->0){
		int n,d;
		scanf("%d%d",&n,&d);
		for(int i=0;i<n;i++){
			p[i].l=in();
			p[i].i=i;
		}
		for(int i=0;i<d;i++){
			q[i].l=in();
			q[i].i=i;
		}
		sort(p,p+n,cmp);
		sort(q,q+d,cmp);
		memset(fl,false,sizeof(fl));
		int cnt=0;
		for(int i=d-1,j=n-1;i>=0;i--){
			while(j>=0&&p[j].l>q[i].l){
				int fr=p[j].i;
				fl[fr]=true;
				if(fr>0&&fr<n-1&&fl[fr-1]&&fl[fr+1])cnt--;
				else if(fr>0&&fl[fr-1]);
				else if(fr<n-1&&fl[fr+1]);
				else cnt++;
				j--;
			}
			res[q[i].i]=cnt;
		}
		for(int i=0;i<d;i++){
			printf("%d ",res[i]);
		}
		printf("\n");
	}
}