zoukankan      html  css  js  c++  java
  • Codeforces Round #656 (Div. 3) C. Make It Good

    题目链接:https://codeforces.com/contest/1385/problem/C

    题意

    去除一个数组的最短前缀使得余下的数组每次从首或尾部取元素可以排为非减序。

    题解一

    当两个大数夹着一个小数那么第一个大数及其之前的数必须要去掉,比如 $1,1,2,1,2$,要去除的前缀长为 $3$,但是考虑到也会有 $1,1,2,1,1,2$ 的情况,所以可以将原数组去重后判断每三个相邻的元素。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        int n; cin >> n;
        int a[n] = {}; for (int i = 0; i < n; ++i) cin >> a[i];
        vector<pair<int, int>> v;
        for (int i = 0; i < n; ) {
            int j = i + 1;
            while (j < n and a[j] == a[i]) ++j;
            v.emplace_back(a[i], j - 1);
            i = j;
        }
        for (int i = v.size() - 1; i - 2 >= 0; --i) {
            if (v[i - 2].first > v[i - 1].first and v[i - 1].first < v[i].first) {
                cout << v[i - 2].second + 1 << "
    ";
                return;
            }
        }
        cout << 0 << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }

    题解二

    换言之,即找右端山脚为数组末尾元素的最长的山峰。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        int n; cin >> n;
        int a[n] = {}; for (auto &i : a) cin >> i;
        int p = n - 1;
        while (p - 1 >= 0 and a[p - 1] >= a[p]) --p;
        while (p - 1 >= 0 and a[p - 1] <= a[p]) --p;
        cout << p << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }
  • 相关阅读:
    237. 删除链表中的节点
    牛客网-第一场-J-Fraction Comparision
    1. 两数之和
    CCF-201903-1大中小
    学习Python
    Convert Sorted Array to Binary Search Tree
    3-1
    Merge Sorted Array
    Climbing Stairs
    Add Binary
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13334257.html
Copyright © 2011-2022 走看看