首页 > 其他 > 详细

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

时间:2020-06-14 18:37:21      阅读:51      评论:0      收藏:0      [点我收藏+]

题目链接: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 << "\n";
    for (int i = 0; i < n; i++) 
        if (!skip[i]) cout << a[i] <<  ;
    cout << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

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

原文:https://www.cnblogs.com/Kanoon/p/13124731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!