Friday, 21 September 2012

SPOJ 11578. A Famous City Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long



int main()
{
    int n,t,kk=1,histogram[100000+10],i,left[100000+10],right[100000+10],mx;
    stack<int>stk;
    
    while(cin>>n)
    {
        
        map<pair<int,int>,int>mp;

        for(i=1;i<=n;i++)
        {
            scanf("%d",&histogram[i]);
            histogram[i]++;
        }


        while(!stk.empty())
            stk.pop();

        stk.push(0);
        histogram[0]=0;

        for(i=1;i<=n;i++)
        {
            while(histogram[stk.top()]>=histogram[i])
                stk.pop();

             left[i]= stk.top()+1;
             stk.push(i);
        }

        while(!stk.empty())
            stk.pop();

        stk.push(n+1);
        histogram[n+1]=0;


        for(i=n;i>0;i--)
        {
            while(histogram[stk.top()]>=histogram[i])
                stk.pop();

             right[i]=  stk.top()-1;
             stk.push(i);
        }
     
        int cnt=0;
        for(i=1;i<=n;i++)
        {
            if(histogram[i]!=1)
            {

            if(mp[MP(left[i],right[i])]!=1)
            {
                cnt++;
                mp[MP(left[i],right[i])]=1;
            }
            }
        }
        cout<<"Case "<<kk++<<": "<<cnt<<endl;
    }
return 0;
}

1 comment: