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

Posted kanoon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

 

以上是关于Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #649 (Div. 2)ABC

Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs

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

Codeforces Round #649 (Div. 2) D - Ehab's Last Corollary dfs

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)