Advertisement
Guest User

Ajay Rajnikanth - Matched Brackets 2

a guest
Nov 17th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. typedef long long ll;
  6.  
  7. int main()
  8. {
  9.     ll n,i;
  10.     int c1=0,c2=0,count=0,ad=-999999999,switc=-1,a1=-999999999,a2=-999999999;
  11.     cin>>n;
  12.     vector<ll>a(n);
  13.     for(i=0;i<n;i++)
  14.     {
  15.         cin>>a[i];
  16.     }
  17.     for(i=0;i<n;)
  18.     {
  19.         do
  20.         {
  21.             if(a[i]==1)
  22.             {
  23.                 c1++;
  24.                 if((switc==1)||(switc==-1))
  25.                 {
  26.                     count++;
  27.                     switc=0;
  28.                 }
  29.             }
  30.             else if(a[i]==2)
  31.             {
  32.                 c1--;
  33.             }
  34.             else if(a[i]==3)
  35.             {
  36.                 c2++;
  37.                 if((switc==0)||(switc==-1))
  38.                 {
  39.                     count++;
  40.                     switc=1;
  41.                 }
  42.             }
  43.             else
  44.             {
  45.                 c2--;
  46.             }
  47.             i++;
  48.         } while ((c1!=0)||(c2!=0));
  49.         //cout<<count<<endl;
  50.         ad=max(ad,count);
  51.         count=0;
  52.         switc=-1;
  53.     }
  54.     count=0;
  55.     for(i=0;i<n;)
  56.     {
  57.         count=0;
  58.         do
  59.         {
  60.             if(a[i]==1)
  61.             {
  62.                 c1++;
  63.             }
  64.             else if(a[i]==2)
  65.             {
  66.                 c1--;
  67.             }
  68.             i++;
  69.             count++;
  70.         } while (c1!=0);
  71.         a1=max(a1,count);
  72.     }
  73.     count=0;
  74.     for(i=0;i<n;)
  75.     {
  76.         count=0;
  77.         do
  78.         {
  79.             if(a[i]==3)
  80.             {
  81.                 c2++;
  82.             }
  83.             else if(a[i]==4)
  84.             {
  85.                 c2--;
  86.             }
  87.             i++;
  88.             count++;
  89.         } while (c2!=0);
  90.         a2=max(a2,count);
  91.     }
  92.     cout<<ad<<" "<<a1<<" "<<a2;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement