676A - Nicholas and Permutation - CodeForces Solution


constructive algorithms implementation *800

Please click on ads to support us..

Python Code:

n = int(input())
lst = list(map(int, input().split()))

ma = max(lst)
mi = min(lst)

m = max([lst.index(ma), lst.index(mi), n -lst.index(ma)-1, n-lst.index(mi)-1])
print(m)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
const long long MOD=1000000007;
#define yes cout<<"YES\n";
#define no cout<<"NO\n"
#define pi acos(-1)
typedef long long ll;
typedef vector<ll> vec;
typedef vector<string> vecs;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vp;
typedef set<ll> st;
typedef set<pair<ll, ll>> sp;
typedef multiset<ll> mst;
typedef multiset<pair<ll, ll>> mstp;
typedef map<ll, ll> mp;
typedef map<char, ll> mpc;
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()
#define sqr(a) ((a) * (a))
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
template < typename T, typename ... hello>void faltu( T arg, const hello &... rest)
{
    cerr << arg << ' ';
    faltu(rest...);
}
//inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
//inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
//inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
//inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
//inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
//inline ll modInverse(ll a) { return modPow(a, MOD-2); }
//inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
// arr n-1 last and 1 start compare previous arr[i+1]>arr[i] min num of moves to make a to b b-(a%b)

ll getmodex(ll a, ll  b)
{
    if(b==0) return 1;
    if(b%2==0)
    {
        auto t=getmodex(a,b/2)%MOD;
        return (t*t)%MOD;
    }
    return(a * getmodex(a,b-1)%MOD)%MOD;
}
//--------------------------------- START ------------------------------------------//
void solve()
{

    ll n;
    cin>>n;
    ll arr[n];
    ll idx=0,idx1=0;
    for(ll i=1; i<=n; i++)
    {
        cin>>arr[i];
        if(arr[i]==1)
        {
            idx1=i;
        }
        if(arr[i]==n)
        {
            idx=i;
        }
    }
    ll ans=max(abs(idx1-n),abs(idx1-1));
    ll ans1=max(abs(idx-n),abs(idx-1));
    cout<<max(ans1,ans)<<endl;

}


int main()
{
    //optimize();
    int t;
//    cin>>t;
//    while(t--)
//    {
//        solve();
//
//    }
    solve();
    // main();
    //for(int i=1;i<=t;i++) cout<<"Case "<<i<<": ",solve();ll n;

    //ll t;scanf("%lld",&t);for(int i=1;i<=t;i++)printf("Case %lld: ",i),solve();
    return 0;

}
//#include<bits/stdc++.h>
//using namespace std;
//#define ll long long
//void rangemin(ll *arr, ll l,ll r)
//{
//    vector<pair<ll,ll>>vec;
//    ll mino=LONG_MAX;
//
//    for(ll i=l; i<=r; i++)
//    {
//        mino=min(mino,arr[i]);
//        vec.push_back({mino,i});
//
//    }
//    ll maxno=LONG_MIN;
//    for(auto x:vec)
//    {
//        maxno=max(maxno,x.second);
//    }
//    cout<<mino<<endl;
//    cout<<maxno<<endl;
//
//}
//int main()
//{
//
//    ll n,q;
//    cin>>n>>q;
//    ll arr[n];
//    ll mino=LONG_MAX;
//    for(ll i=1; i<=n; i++)
//    {
//        cin>>arr[i];
//    }
//    while(q--)
//    {
//
//        ll l,r;
//        cin>>l>>r;
//        rangemin(arr,l,r);
//
//    }
//
//}


Comments

Submit
0 Comments
More Questions

1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party