1538A - Stone Game - CodeForces Solution


brute force dp greedy *800

Please click on ads to support us..

Python Code:

t = int(input())
for tc in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    a, b = arr.index(max(arr)), arr.index(min(arr))
    if b < a:
        a, b = b, a
    print(n-max(b-a-1, a, n-1-b))

C++ Code:

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
typedef long long ll;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e17;

vector<ll > f[N];
ll a[N];


void solve() {
    int n;
    cin >> n;
    int mi = 110, mn = 0;
    int mip = 110, mnp = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (mi > a[i]) {
            mi = a[i];
            mip = i;
        }
        if (a[i] > mn) {
            mn = a[i];
            mnp = i;
        }
    }
    int  ans = min({
            max(mip,mnp),
            n-min(mip,mnp)+1,
            mip+n-mnp+1,
            mnp+n-mip+1
        });
    cout << ans << endl;
}
int main() {
    //ios::sync_with_stdio(0);
    //    cin.tie(0);             
    //    cout.tie(0);
    ll t;
    cin >> t;
    //t = 1;
    
    while (t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold