题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5200
题意:
每次去掉高度小于q的树,每次输出剩下的块数。
分析:
我们对高度从高到低进行排序,对要去掉的高度从低到高进行排序。
因此前面去掉的在后面一定会去掉,因可以离线处理,节省时间,
然后需要开一个辅助空间,记录这棵树去没有去掉。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxn = 50010;
int vis[maxn],ans[maxn];
struct tree{
int h,id;
bool operator <(const struct tree &tmp) const{
return h > tmp.h;
}
}p[maxn];
struct query{
int h,id;
bool operator <(const struct query &tmp) const{
return h<tmp.h;
}
}q[maxn];
inline int S(){
int ret=0,ok=0;
char c;
while((c=getchar()))
{
if(c>='0'&&c<='9')
ret=ret*10+c-'0',ok=1;
else if(ok)
return ret;
}
return ret;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++){
p[i].h=S();
p[i].id=i+1;
}
sort(p,p+n);
for(int i=0;i<m;i++){
q[i].h=S();
q[i].id=i+1;
}
sort(q,q+m);
int tot = 0;
memset(vis,0,sizeof(vis));
for(int i = 0,j = m-1;j>=0;j--){
for(;i<n;i++){
if(p[i].h<=q[j].h)
break;
vis[p[i].id]=1;
if(!vis[p[i].id-1]&&!vis[p[i].id+1]) tot++;
else if(vis[p[i].id-1]&&vis[p[i].id+1]) tot--;
}
ans[q[j].id]=tot;
}
for(int i=0;i<m;i++)
printf("%d\n",ans[i+1]);
}
return 0;
}