算法 18. 看楼房
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
struct building
{
long long int color;
long long int height;
}house[1000005];
stack <struct building> s;
long long int colors[1000005];
long long int p;
int main()
{
int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
p=0;
memset(colors,0,sizeof(colors));
while(!s.empty())
{
s.pop();
}
int count=0;
long int n;
scanf("%ld",&n);
for(long int j=1;j<=n;j++)
{
scanf("%lld",&house[j].color);
}
for(long int j=1;j<=n;j++)
{
scanf("%lld",&house[j].height);
}
s.push(house[1]);
colors[house[1].color]++;
p++;
count++;
printf("%lld ",p);
for(long int j=2;j<=n;j++)
{
if(count==0)
{
s.push(house[j]);
colors[house[j].color]++;
count++;
p++;
}
else
{
while(house[j].height>=s.top().height)
{
colors[s.top().color]--;
if(colors[s.top().color]==0)
{
p--;
}
s.pop();
count--;
if(s.empty())
{
break;
}
}
if(count==0)
{
s.push(house[j]);
colors[house[j].color]++;
count++;
p++;
}
if(house[j].height<s.top().height)
{
if(colors[house[j].color]==0)
{
p++;
}
colors[house[j].color]++;
s.push(house[j]);
count++;
}
}
if(j<n)
{
printf("%lld ",p);
}
else
{
printf("%lld\n",p);
}
}
}
return 0;
}