日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Most socially-distanced subsequence CodeForces - 1364B(贪心)

發(fā)布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Most socially-distanced subsequence CodeForces - 1364B(贪心) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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?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≤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
Note
In the first test case, there are 4 subsequences of length at least 2:

[3,2] which gives us |3?2|=1.
[3,1] which gives us |3?1|=2.
[2,1] which gives us |2?1|=1.
[3,2,1] which gives us |3?2|+|2?1|=2.
So the answer is either [3,1] or [3,2,1]. Since we want the subsequence to be as short as possible, the answer is [3,1].
思路:根據(jù)數(shù)組,可以發(fā)現(xiàn),選取峰值就是最優(yōu)的。

代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; int a[maxx]; int vis[maxx]; int n;int main() {int t;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),vis[i]=0;vis[1]=1;int cnt=1;for(int i=2;i<=n;){int j=i;if(a[i]>a[i-1]) while(j<=n&&a[j]>a[j-1]) j++;else while(j<=n&&a[j]<a[j-1]) j++;vis[j-1]=1;cnt++;i=j;}cout<<cnt<<endl;for(int i=1;i<=n;i++) if(vis[i]) cout<<a[i]<<" ";cout<<endl;}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Most socially-distanced subsequence CodeForces - 1364B(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲91av| 日韩欧美网址 | 国产一区二区在线播放视频 | 国产免费福利视频 | 深夜福利麻豆 | 色干干 | 黄色一级大片免费版 | 阿v视频免费在线观看 | av青草| 91微拍| 国产美女永久无遮挡 | 香蕉色网 | 四川丰满妇女毛片四川话 | 国产一二三在线 | 日韩国产电影 | 金瓶风月在线 | 亚洲一区二区三区乱码 | 国产一级高清 | 亚洲成人黄 | 日批av | 精品亚洲成人 | 在线观看免费毛片 | 国产suv精品一区二区33 | 国产经典一区二区 | 四虎成人影视 | 国产91精品高潮白浆喷水 | 亚洲最大中文字幕 | 久久一卡二卡 | 欧美中文日韩 | 青青草视频成人 | 午夜av电影在线观看 | 美女张开腿让男人操 | 亚洲成人av一区 | 爱爱视频在线播放 | 冲田杏梨在线 | 午夜在线视频免费 | 夜夜骚av一区二区三区 | 一级裸体片 | 就去吻综合 | 亚洲久久一区 | 色撸撸在线视频 | 在线免费黄网 | 999成人网| 成年人视频免费在线观看 | 日日夜夜综合网 | 99久久99 | 国产毛片久久久久久久 | 欧美精品在线第一页 | 中文字幕一区二区在线播放 | 六月丁香激情综合 | 午夜一区二区三区在线观看 | 疯狂撞击丝袜人妻 | 伊人网综合视频 | 国内福利视频 | 久久手机看片 | 成全影视在线观看第8季 | 国产综合视频一区二区 | 国产熟女一区二区 | 东方成人av | 欧美黄色免费大片 | 中文字幕在线视频一区二区三区 | 我们俩电影网mp4动漫官网 | 丁香伊人网| 男人的天堂色 | 福利网址在线观看 | 亚洲特黄一级片 | 最新福利在线 | 亚洲综合专区 | 午夜不卡久久精品无码免费 | 国产精品第1页 | 免费黄色一级大片 | 欧美成人片在线 | 男人的av | 调教丰满的已婚少妇在线观看 | 69国产精品视频 | 激情在线网站 | 性爱免费在线视频 | 91成人免费视频 | 日本黄色三级视频 | 国产精品高潮呻吟久久av黑人 | 干b视频在线观看 | 国产精品福利小视频 | 98国产精品| 欧美久久久久久又粗又大 | 亚洲精品码 | 在线观看成人黄色 | 伊人色综合久久久 | 91精品国产电影 | 日韩大片免费在线观看 | 日本亚洲色图 | 日韩成人综合 | 狠狠操一区二区 | 日大逼| 国产九色av| 影音先锋在线看 | 一区二区三区国产精品视频 | 九一网站在线观看 | 欧美丝袜一区二区 | 五月天国产精品 |