题目链接: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的更多相关文章

  1. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

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

    题目链接:https://codeforces.com/contest/1364/problem/C 题意 给出大小为 $n$ 的非递减数组 $a$,构造同样大小的数组 $b$,使得对于每个 $i$, ...

  3. Codeforces Round #649 (Div. 2) A. XXXXX

    题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...

  4. Codeforces Round #649 (Div. 2) E. X-OR 交互 二进制 随机 期望

    LINK:X-OR 本来是应该昨天晚上发的 可是昨天晚上 做这道题 写了一个分治做法 一直wa 然后查错 查不出来 心态崩了 想写对拍 发现交互库自己写不出来. 一系列sb操作 == 我都醉了. 今天 ...

  5. Codeforces Round #649 (Div. 2) C、Ehab and Prefix MEXs D、Ehab's Last Corollary 找环和点染色

    题目链接:C.Ehab and Prefix MEXs 题意; 有长度为n的数组a(下标从1开始),要求构造一个相同长度的数组b,使得b1,b2,....bi集合中没有出现过的最小的数是ai. mex ...

  6. Codeforces Round #649 (Div. 2) C. Ehab and Prefix MEXs (构造,贪心)

    题意:有长度为\(n\)的数组\(a\),要求构造一个相同长度的数组\(b\),使得\({b_{1},b_{2},....b_{i}}\)集合中没有出现过的最小的数是\(a_{i}\). 题解:完全可 ...

  7. Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence (数学,差分)

    题意:有一长度为\(n\)的数组,求一子序列,要求子序列中两两差的绝对值最大,并且子序列尽可能短. 题解:将数组看成坐标轴上的点,其实就是求每个单调区间的端点,用差分数组来判断单调性. 代码: #in ...

  8. Codeforces Round #649 (Div. 2) A. XXXXX (贪心)

    题意:有一个长度为\(n\)的数组,找一段最长子数组,使得其元素和为\(x\),如果存在,输出子数组的长度,否则输出\(-1\). 题解:这题我们要从元素和\(sum\)来考虑,首先,如果原数组的所有 ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. Spring Cloud Gateway 跨域 CORS 配置方式实现

    网上找了一堆文章全是说这样写无效 globalcors: cors-configurations: '[/**]': allowCredentials: true allowedOriginPatte ...

  2. 【Redis3.0.x】事务

    Redis3.0.x 事务 基本概念 multi,exec,discard,watch 是 Redis 事务的基础,它们允许一步执行一组命令,有两个重要保证: 事务中的所有命令都被序列化并顺序执行.在 ...

  3. 【Redis3.0.x】持久化

    Redis3.0.x 持久化 概述 Redis 提供了两种不同的持久化方式: RDB(Redis DataBase)持久化,可以在指定的时间间隔内生成数据集的时间点快照. AOF(Append Onl ...

  4. 【Flutter】容器类组件之剪裁

    前言 Flutter中提供了一些剪裁函数,用于对组件进行剪裁. 剪裁Widget 作用 ClipOval 子组件为正方形时剪裁为内贴圆形,为矩形时,剪裁为内贴椭圆 ClipRRect 将子组件剪裁为圆 ...

  5. 使用OpenCV进行简单的人像分割与合成

    图像合成 实现思路 通过背景建模的方法,对源图像中的动态人物前景进行分割,再将目标图像作为背景,进行合成操作,获得一个可用的合成影像. 实现步骤如下. 使用BackgroundSubtractorMO ...

  6. 最全的HashMap源码解析!

    HashMap源码解析 HashMap采用键值对形式的存储结构,每个key对应唯一的value,查询和修改的速度很快,能到到O(1)的平均复杂度.他是非线程安全的,且不能保证元素的存储顺序. 他的关系 ...

  7. 【Software Test】Introduction to Software Testing

    Introduction to Software Testing 文章目录 Going to Learn --. Evolution of The Software Industry Errors, ...

  8. LeetCode563. 二叉树的坡度

    题目 1 class Solution { 2 public: 3 int ans = 0; 4 int findTilt(TreeNode* root) { 5 postOrder(root); 6 ...

  9. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp

    [root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf root@192.168.30.23:/etc/sysctl.d/k8s.conf@@@@@@@@@@@ ...

  10. 1.2V升5V电源芯片,1.2V升3V的IC电路图方案

    镍氢电池就是典型的1.2V供电电源了,但是1.2V电压太低,需要电源芯片来1.2V升5V输出,或1.2V升3V输出稳压,1.2V单独难给其他芯片或者模块供电,即使串联1.2V*2=2.4V,也是因为电 ...