# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
978007 | alexdd | popa (BOI18_popa) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "popa.h"
#include<bits/stdc++.h>
using namespace std;
int _s[1005],_n;
int query(int a, int b, int c, int d)
{
int gc1=_s[a],gc2=_s[c];
for(int i=a;i<=b;i++)
gc1 = __gcd(gc1,_s[i]);
for(int i=c;i<=d;i++)
gc2 = __gcd(gc2,_s[i]);
if(gc1==gc2)
return 1;
return 0;
}
int tole[1005],tori[1005];
int calc(int le, int ri)
{
if(le>ri)
return -1;
if(le==ri)
{
tole[le]=tori[le]=-1;
return le;
}
for(int root=le;root<=ri;root++)
{
if(query(root,root,le,ri))
{
tole[root] = calc(le,root-1);
tori[root] = calc(root+1,ri);
return root;
}
}
}
int solve(int N, int* Left, int* Right)
{
for(int i=0;i<N;i++)
tole[i]=tori[i]=-1;
int root = calc(0,N-1);
for(int i=0;i<N;i++)
{
Left[i]=tole[i];
Right[i]=tori[i];
}
return root;
}
int _left[1005],_right[1005];
signed main()
{
cin>>_n;
for(int i=0;i<_n;i++)
cin>>_s[i];
int root = solve(_n,_left,_right);
cout<<root<<" the root\n";
for(int i=0;i<_n;i++)
{
cout<<i<<" "<<_left[i]<<" "<<_right[i]<<" left and right\n";
}
return 0;
}