Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
程序员文章站
2022-10-25 15:29:46
题目链接思路:连续的值之间的差值的绝对值就是两端的差值,所以我们只需要找转折点并删去中间所有的值即可。代码:#includeusing namespace std;#define int long long#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);const int N=2e5+7;const int mod=1e9+7;const int inf=0x7ffff...
题目链接
思路:
连续的值之间的差值的绝对值就是两端的差值,所以我们只需要找转折点并删去中间所有的值即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=2e5+7;
const int mod=1e9+7;
const int inf=0x7fffffff;
const double pi=3.1415926535;
using namespace std;
int t,n,a[N];
vector<int>vt;
signed main()
{
IOS;
cin>>t;
while(t--)
{
cin>>n;
vt.clear();
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
if(n==2)
{
if(a[1]==a[2])
{
cout<<1<<endl<<a[1]<<endl;
}
else
{
cout<<2<<endl<<a[1]<<" "<<a[2]<<endl;
}
continue;
}
vt.push_back(a[1]);
vt.push_back(a[2]);
int i=1,j=2;
for(int k=3;k<=n;k++)
{
if(abs(a[i]-a[j])+abs(a[j]-a[k])<=abs(a[i]-a[k]))
{
vt.pop_back();
}
vt.push_back(a[k]);
i=j;j=k;
}
cout<<vt.size()<< endl;
for(auto it:vt)
{
cout<<it<<" ";
}
cout<<endl;
}
return 0;
}
本文地址:https://blog.csdn.net/ACkingdom/article/details/107394049
下一篇: vue以及vue.html组件的代码片段
推荐阅读
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(思维,数学)
-
Codeforces Round #673 (Div. 2) B. Two Arrays(思维,构造)
-
B. Two Arrays(模拟+思维)Codeforces Round #673 (Div. 2)
-
B. Jzzhu and Sequences(思维)Codeforces Round #257 (Div. 2)
-
Codeforces Round #621 (Div. 1 + Div. 2) B. Cow and Friend (思维)
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(思维,数学)
-
Educational Codeforces Round 52 (Rated for Div. 2)B. Vasya and Isolated Vertices·「模拟,思维」