Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence
2024-08-29 01:00:06
题目链接: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();
}
最新文章
- Node.js 安装配置
- L# ForUnity Helloworld的更新方法
- 【读书笔记】iOS-GCD-GCD与perfomSelector系方法比较
- AFX_MANAGE_STATE(AfxGetStaticModuleState())DLL导出函数包含MFC资源
- Altium Designer多图纸原理图设计方法探讨
- 使用ES6进行开发的思考
- WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
- cocos2d-x 3.0rc1 创建project
- 转:通过ant来批量执行jmeter脚本,并生成报告(附: 生成报告时报“Content is not allowed in prolog”这个错误的解决方案)
- 直接插入排序算法:ArrayList实现和数组实现
- iOS依赖库管理工具之Carthage
- [Swift]LeetCode217. 存在重复元素 | Contains Duplicate
- 服务器启动socket服务报错 java.net.BindException:Cannot assign requested address
- 转载:揪出MySQL磁盘消耗迅猛的真凶
- springboot security
- vux安装中遇到的坑(转)
- 几个OOD概念
- How to tell if a file is an EXE or a DLL?
- 机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)
- 原生js--类的扩充和类型检测
热门文章
- node.js中使用http-proxy-middleware请求转发给其它服务器
- 一文带你探究Sentinel的独特初始化
- 使用nodejs和express搭建http web服务
- selenium爬虫 | 爬取疫情实时动态
- (二)数据源处理5-excel数据转换实战(上)
- 【ORA】ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O
- 【Oracle】如果有一个Oracle中的用户,想知道他有什么权限,怎么查看?
- Redis 实战 —— 02. Redis 简单实践 - 文章投票
- [Usaco2007 Dec]Building Roads 修建道路
- Django-http协议