1760D - Challenging Valleys - CodeForces Solution


implementation two pointers *1000

Please click on ads to support us..

Python Code:

t=int(input())
result=[]
for i in range(t):
    n=int(input())
    tab=list(map(int,input().split(" ")))
    valleys=0
    if(n==1):
        result.append('YES')
        continue
    l,r=0,0
    while(r<n):
        if((r==n-1)):
            if(l==0):
                valleys+=1
            elif(tab[l-1]>tab[l]):
                valleys+=1
            break
        if((l==0)&(tab[r+1]>tab[r])):
            valleys+=1
        elif(l!=0):
            if((tab[r+1]>tab[r])&((tab[l-1]>tab[l]))):
                valleys+=1
        if(tab[r+1]==tab[r]):
            r+=1
        elif(tab[r+1]!=tab[l]):
            r+=1
            l=r
    if(valleys==1):
        result.append('YES')
    else:
        result.append('NO')

for i in result:
    print(i)


C++ Code:

#pragma GCC optimize("O3")
#include <iostream>
// #include <vector>
// #include <cstring>
// #include <algorithm>
// #include <iomanip>
// #include <math.h>
// #include <iomanip>
// #include <map>
// #include <unordered_map>
// #include <set>
// #include <unordered_set>
// #include <stack>
// #include <queue>
// #include <deque>
using namespace std;

template < typename A, typename B > ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << '\n'; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define PI 3.14159265358979323846
#define ll long long
#define ld long double
#define fi first
#define se second
#define nl '\n'
#define all(x) x.begin(), x.end()
#define gg(t) cout << "Case #"<< t << ": ";
#define line cout << nl;
#define pline cout << nl << "______________________" << nl;
#define f(i, k, n) for(int i = k; i <= n; i++)
#define ms(x, a, n) f(i, 0, n-1) x[i] = a;

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vvl vector<vector<ll>>

#define cia(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i];
#define ciap(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i].fi >> a[i].se;
#define ciab(a, b, n, s) for (int i = s; i < n + s; ++i) cin >> a[i] >> b[i];
#define cim(a, m, n, s) for (int i = s; i < m + s; ++i) for (int j = s; j < n + s; ++j) cin >> a[i][j];
#define tr(it, a) for(auto &it : a)
#define sz(x) int((x).size())

const ll MOD = 1e9 + 7;
const int INF = 2e9;
const int LIM = 1e6 + 5;

int n, a[LIM], min_num, min_p, tmp, pre;

void run_case()
{
    cin >> n;
    cia(a, n, 0);
    min_num = INF;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] < min_num)
        {
            min_num = a[i];
            min_p = i;
        }
    }
    tmp = min_p;
    pre = min_num;
    while (min_p >= 0)
    {
        if (a[min_p] < pre)
        {
            cout << "NO" << nl;
            return;
        }
        else
        {
            pre = a[min_p];
        }
        min_p--;
    }
    min_p = tmp;
    pre = min_num;
    while (min_p < n)
    {
        if (a[min_p] < pre)
        {
            cout << "NO" << nl;
            return;
        }
        else
            pre = a[min_p];
        min_p++;
    }
    cout << "YES" << nl;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("A.INP", "r", stdin);
    // freopen("A.OUT", "w", stdout);
    int t = 1;
    cin >> t;
    f(i, 1, t)
    {
        // gg(i);
        run_case();
    }
}


Comments

Submit
0 Comments
More Questions

Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal