题目链接:
https://codeforces.com/contest/1832/problem/C
题目概述:
给出一个数组,得到一个有着相同对比度非空子序列数组。求子序列数组的最短长度。
数组的对比度为每相邻两项的绝对值之和。
做题思路:
根据题目样例可以看出,连续两个相同的元素对最终的对比度没有影响,所以需要对其给出的数组进行去重操作。
其次可以发现在严格递增数列和严格递减数列的情况下,这一递增递减数列的绝对值之和为首项和末项的绝对值,所以可以对每一个递增递减区间进行寻找,最后根据数组长度累减即可达到最终所求的拥有相同对比度的最短数列。
输入分析:
T次询问。
每次询问,数组的长度和数组的各项元素。
输出分析:
输出最短数列的长度。
解题代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
//快速读入输出
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++)
cin >> a[i];
//去重处理
int l = unique(a.begin(), a.end()) - a.begin();
//去重后的初始数组长度
int ans = l;
for (int i = 0; i < l - 2; i++){
//递增数列查询
if(a[i+1] > a[i] && a[i+2] > a[i+1])ans--;
//递减数列查询
if(a[i+1] < a[i] && a[i+2] < a[i+1])ans--;
}
cout << ans << endl;
}
return 0;
}