题目

Given a permutation p of length n, find its subsequence s1, s2, …, sk of length at least 2 such that:

|s1−s2|+|s2−s3|+…+|sk−1−sk| is as big as possible over all subsequences of p with length at least 2.
Among all such subsequences, choose the one whose length, k, is as small as possible.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.

A sequence a is a subsequence of an array b if a can be obtained from b by deleting some (possibly, zero or all) elements.

A permutation of length n is an array of length n in which every element from 1 to n occurs exactly once.

Input
The first line contains an integer t ( 1 ≤ t ≤ 2 × 1 0 4 ) t (1≤t≤2\times10^4) t(1≤t≤2×104)— the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n ( 2 ≤ n ≤ 1 0 5 ) n (2≤n≤10^5) n(2≤n≤105) — the length of the permutation p.

The second line of each test case contains n integers p1, p2, …, pn (1≤pi≤n, pi are distinct) — the elements of the permutation p.

The sum of n across the test cases doesn’t exceed 105.

Output
For each test case, the first line should contain the length of the found subsequence, k. The second line should contain s1, s2, …, sk — its elements.

If multiple subsequences satisfy these conditions, you are allowed to find any of them.

Example
Input
2
3
3 2 1
4
1 3 4 2
Output
2
3 1
3
1 4 2

思路

把这个排列想象成一个个长条排放在坐标轴上,如1,3,2 ,4
如此看来,不难看出要让子数组相邻数字的差的绝对值的和最大要递增或递减地去寻找,从第一个开始,放入子数组的第一位,往后若递增(a[i]>a[i-1]),则一直往下寻找,直到出现转折(a[i]<a[i-1]),再把a[i-1]放到子数组的下一个。若往后递减也类似如此去寻找子数组里的各个数字。在这里插入代码片

代码

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;const int N=1e5+5;int a[N];
int sub[N];int main()
{int t;scanf("%d",&t);int n;while(t--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&a[i]);}int temp=0;sub[temp++]=a[0];int i=1;while(i<n){if(a[i]<a[i-1]){while(a[i]<a[i-1] && i<n){i++;}sub[temp++]=a[i-1];}else{while(a[i]>a[i-1] && i<n){i++;}sub[temp++]=a[i-1];}}printf("%d\n",temp);for(int j=0;j<temp;j++){printf("%d ",sub[j]);}printf("\n");}return 0;
}

Most socially-distanced subsequence(贪心)相关推荐

  1. 376 Wiggle Subsequence 贪心解法以及证明

    376. Wiggle Subsequence 题目理解 给定一个数组,相邻两个数计算差值.差值排成的序列是正负相间的,那这个数组就是一个wiggle 数组.例如数组[1,7,4,9,2,5],差值序 ...

  2. opencv图像深度-1_OpenCV空间AI竞赛之旅(第1部分-初始设置+深度)

    opencv图像深度-1 OpenCV空间AI竞赛 (OpenCV Spatial AI Competition) Recently, the people at OpenCV launched th ...

  3. 苹果iphone 12它只是一个电话

    Electric. That's the atmosphere at an iPhone launch. Seated in one of Apple's exquisite presentation ...

  4. Python干货项目:【新闻急先锋】新闻API获取谷歌头条新闻

    新闻API是用于搜索和检索来自整个Web的实时新闻文章,可以根据某些标准检索新闻. 使用它,可以获取任何新闻网站上运行的顶级新闻,也可以搜索特定主题(或关键字)的顶级新闻. 假设要搜索的主题(关键字) ...

  5. 实施一个光明与黑暗的主题,以持久的React

    介绍(Introduction) So, you've come to that point in the creation of your website where you want to do ...

  6. 两个词之间的依存语义关系_相互依存的网络之间的级联中断-2003年停电的5个教训

    两个词之间的依存语义关系 In 2003, what should have been a local power outage resulted in 50 million people losin ...

  7. 语料库建立_通过挖掘covid 19科学语料库建立对病毒的理解

    语料库建立 At the time I publish this, we are entering the 9th month since COVID-19 froze the world. Sinc ...

  8. 托管和非托管_技术和托管新闻综述

    托管和非托管 Welcome to our latest round-up of news from the technology and hosting world. Here's what we' ...

  9. 蜜蜂编程_没有蜜蜂就无法拼写“可预防”

    蜜蜂编程 The New York Times Spelling Bee is a daily online puzzle that presents a set of seven letters a ...

  10. E.03.31 Shadowed by Pandemic, Olympic Torch Relay Begins in Japan

    2021.03.31 文章目录 [课程导读] [英文原文] [外刊原文] [课程导读] 上周,东京奥运会火炬传递仪式在福岛县正式开启.日本政府将这届因新冠疫情推迟了一年的奥运会视为重建和复兴的标志,认 ...

最新文章

  1. 钱海丰:农药污染下的土壤微生态响应与风险预测​(今晚7点半)
  2. 央视曝光:刷单实为非法商业模式 步步设置全是套路
  3. ios开发 多人语音聊天_手游语音市场的现状、机遇与挑战
  4. 线程的3种实现方式并深入源码简单分析实现原理
  5. 【C++笔记】函数(笔记)
  6. RS报表从按月图表追溯到按日报表
  7. Android第三十八天
  8. 【Ubuntu】升到14,攻克了进入用户后没有菜单条导航栏的问题
  9. Fruits 360(水果数据集)
  10. linux unzip 包括目录,Linux 命令(目录管理 - zip/unzip)
  11. 百度人脸识别申请授权文件步骤要领
  12. ARC下循环引用的问题
  13. Java使用POI将doc文档转为Html
  14. 天津大学计算机课程设计挂,天津大学智能装置课程设计 电子时钟..doc
  15. python晋江爬虫_python爬虫之小说爬取
  16. 线程共享地址空间的问题
  17. 【学习笔记】Docker基础实战教程一:入门
  18. Keil5使用第一步
  19. 高数第一章(函数)练习题
  20. 基于FPGA的串口传图SDRAM缓存VGA显示

热门文章

  1. 改变应用程序配置文件的文件名
  2. myBase使用手册
  3. 用自定义view实现刮刮乐
  4. 个人独立博客的去与从
  5. libev源代码分析--事件监控器
  6. 了解公司企业Advance Metering
  7. 蚂蚁森林收集能量之AutoJs实现(精度优化版)
  8. 《云计算核心技术剖析》参考文献
  9. 给孩子的史上最全思维导图使用指南
  10. 读书笔记-《专业主义》的读后收获