BNUOJ 1094 Skyscrapers

CEPC 2008

题意:有一堆并列着的摩天大楼,给出每栋楼的高度,从DAY1到DAYD海平面逐渐上涨,输出每天还在水面上的连续区域数。

题解:排序+模拟题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct T
{
    int h;
    int c;
};
bool cmp(T a,T b)
{
    return a.h<b.h;
}
int     day[1200000];
T       build[1200000];
int     v[1200000];
int     ans[1200000];
int main()
{
    int prob,i,j,t1,t2,t3,n,d,s,e,pr;
    scanf("%d",&prob);
    while (prob--)
    {
        scanf("%d%d",&n,&d);
        for (i=0; i<n; i++)
        {
            scanf("%d",&build[i].h);
            build[i].c=i;
            v[i]=0;
        }
        for (i=0; i<d; i++)
            scanf("%d",day+i);
        sort(build,build+n,cmp);
        e=n-1;
        pr=0;
        for (i=d-1; i>=0; i--)
        {
            s=e;
            while (build[s].h > day[i]) s--;
            s++;
            for (j=s; j<=e; j++)
            {
                if ((build[j].c == 0 || v[build[j].c-1] == 0)
                 && (build[j].c == n-1 || v[build[j].c+1] == 0))
                {
                    pr++;
                    v[build[j].c]=1;
                }
                else if ((build[j].c == 0 || v[build[j].c-1] == 1)
                      && (build[j].c == n-1 || v[build[j].c+1] == 0))
                {
                    v[build[j].c]=1;
                }
                else if ((build[j].c == 0 || v[build[j].c-1] == 0)
                      && (build[j].c == n-1 || v[build[j].c+1] == 1))
                {
                    v[build[j].c]=1;
                }
                else if ((build[j].c == 0 || v[build[j].c-1] == 1)
                      && (build[j].c == n-1 || v[build[j].c+1] == 1))
                {
                    pr--;
                    v[build[j].c]=1;
                }
            }
            e=s-1;
            ans[i]=pr;
        }
        for (i=0; i<d; i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
    }
}


 

 

你可能感兴趣的:(BNUOJ 1094 Skyscrapers)