题目链接: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();
}

最新文章

  1. Node.js 安装配置
  2. L# ForUnity Helloworld的更新方法
  3. 【读书笔记】iOS-GCD-GCD与perfomSelector系方法比较
  4. AFX_MANAGE_STATE(AfxGetStaticModuleState())DLL导出函数包含MFC资源
  5. Altium Designer多图纸原理图设计方法探讨
  6. 使用ES6进行开发的思考
  7. WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
  8. cocos2d-x 3.0rc1 创建project
  9. 转:通过ant来批量执行jmeter脚本,并生成报告(附: 生成报告时报“Content is not allowed in prolog”这个错误的解决方案)
  10. 直接插入排序算法:ArrayList实现和数组实现
  11. iOS依赖库管理工具之Carthage
  12. [Swift]LeetCode217. 存在重复元素 | Contains Duplicate
  13. 服务器启动socket服务报错 java.net.BindException:Cannot assign requested address
  14. 转载:揪出MySQL磁盘消耗迅猛的真凶
  15. springboot security
  16. vux安装中遇到的坑(转)
  17. 几个OOD概念
  18. How to tell if a file is an EXE or a DLL?
  19. 机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)
  20. 原生js--类的扩充和类型检测

热门文章

  1. node.js中使用http-proxy-middleware请求转发给其它服务器
  2. 一文带你探究Sentinel的独特初始化
  3. 使用nodejs和express搭建http web服务
  4. selenium爬虫 | 爬取疫情实时动态
  5. (二)数据源处理5-excel数据转换实战(上)
  6. 【ORA】ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O
  7. 【Oracle】如果有一个Oracle中的用户,想知道他有什么权限,怎么查看?
  8. Redis 实战 —— 02. Redis 简单实践 - 文章投票
  9. [Usaco2007 Dec]Building Roads 修建道路
  10. Django-http协议