• Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence


    题目链接:https://codeforces.com/contest/1364/problem/B

    题意

    给出大小为 $n$ 的一个排列 $p$,找出子序列 $s$,使得 $|s_1-s_2|+|s_2-s_3|+ldots+|s_{k-1}-s_k|$ 最大的同时 $k$ 尽可能地小。

    题解

    忽略所有位于两个数中间的数。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    bool sorted(int a, int b, int c) {
        return (a < b and b < c) or (a > b and b > c);
    }
    
    void solve() {
        int n; cin >> n;
        int a[n] = {};
        for (int i = 0; i < n; i++)
            cin >> a[i];
        int len = n;
        bool skip[n] = {};
        for (int i = 1; i < n - 1; i++)
            if (sorted(a[i - 1], a[i], a[i + 1]))
                skip[i] = true, --len;
        cout << len << "
    ";
        for (int i = 0; i < n; i++) 
            if (!skip[i]) cout << a[i] << ' ';
        cout << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }
  • 相关阅读:
    A1035
    A1005
    A1073
    A1061
    A1058
    A1027
    A1019
    Java 操作临时文件创建与删除
    面试必会之HashMap源码分析
    springboot整合cxf框架启动报错
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13124731.html
Copyright © 2020-2023  润新知