返回顶部

Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence (数学,差分)

  • 题意:有一长度为\(n\)的数组,求一子序列,要求子序列中两两差的绝对值最大,并且子序列尽可能短.

  • 题解:将数组看成坐标轴上的点,其实就是求每个单调区间的端点,用差分数组来判断单调性.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<ll,ll> PLL;
    
    int t;
    int n,a[N];
    int b[N];
    vector<int> v;
    
    int main() {
        ios::sync_with_stdio(false);cin.tie(0);
    	cin>>t;
    	 while(t--){
    	 	cin>>n; 
    	 	v.clear();
    	 	for(int i=1;i<=n;++i){
    	 		cin>>a[i];
    	 		b[i]=a[i]-a[i-1];
    	 	}
    	 	v.pb(a[1]);
    	 	for(int i=3;i<=n;++i){
    	 		if((b[i]>0&&b[i-1]<0)||(b[i]<0&&b[i-1]>0)){
    	 			v.pb(a[i-1]);
    	 		}
    	 	}
    	 	v.pb(a[n]);
    	 	cout<<v.size()<<endl;
    	 	for(auto w:v){
    	 		cout<<w<<" ";
    	 	}
    	 	cout<<"\n";
    	 }
    
        return 0;
    }
    
posted @ 2020-06-15 01:07  Rayotaku  阅读(87)  评论(0编辑  收藏  举报