Thứ Tư, 27 tháng 4, 2022

TWOVALS C++

#include <bits/stdc++.h>
using namespace std;

int n,x,y,maxx,j;
long long a[1000005];
void nhap()
{
    cin>>n;
    for (int i=1;i<=n;i++) 
        cin>>a[i];
}

void xuli1()
{

    x=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]!=a[x])
        {
            y=i;
            break;
        }
    }
    if (y==0)
    {
        cout<<"0";
        return;
    }
    //printf("%d %d \n",x,y);
    a[n+1]=-19121985;
    for(int i=y+1;i<=n+1;i++)
    {
        if((a[i]!=a[x])&&(a[i]!=a[y]))
        {

            if(i-1-x+1>maxx) maxx=i-1-x+1;
            y=i;j=i-1;
            while (a[j]==a[i-1]) j--;
            x=j+1;
        }
    }
    cout<<maxx;
}
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    freopen("twovals.inp","r",stdin);
    freopen("twovals.out","w",stdout);
    nhap();
    xuli1();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.