Contrast Value(数学规律)

# Contrast Value

## 题面翻译

定义序列 a1,a2,,an 的权值是
|a1a2|+|a2a3|++|an1an|

T 次询问,每次给一个序列 a ,一个 a 的子序列 b 合法当且仅当 b 权值和 a 相等,求 b 的最小长度。

## 题目描述

For an array of integers [a1,a2,,an] , let's call the value |a1a2|+|a2a3|++|an1an| the contrast of the array. Note that the contrast of an array of size 1 is equal to 0 .

You are given an array of integers a . Your task is to build an array of b in such a way that all the following conditions are met:

- b is not empty, i.e there is at least one element;
- b is a subsequence of a , i.e b can be produced by deleting some elements from a (maybe zero);
- the contrast of b is equal to the contrast of a .

What is the minimum possible size of the array b ?

## 输入格式

The first line contains a single integer t ( 1t104 ) — the number of test cases.

The first line of each test case contains a single integer n ( 1n3105 ) — the size of the array a .

The second line contains n integers a1,a2,,an ( 0ai109 ) — elements of the array itself.

The sum of n over all test cases doesn't exceed 3105 .

## 输出格式

For each test case, print a single integer — the minimum possible size of the array b .

## 样例 #1

### 样例输入 #1

```
4
5
1 3 3 3 7
2
4 2
4
1 1 1 1
7
5 4 2 1 0 0 4
```

### 样例输出 #1

```
2
2
1
3
```

复制代码
//Contrast Value //对于任意的递减和递增的数组,有以下性质,利用前缀和维护和 //首尾元素之差等于数组中相邻元素只差的和 #include <bits/stdc++.h> #define int long long using namespace std; const int N=1e6+10,mod=1e9+7; string s; int n,t,a[N],f[N],res,num,ans,m; bool vis[N]; signed main() { std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n; res=0,num=0,ans=0,a[0]=0x3f3f3f; bool f1=true,f2=true; for(int i=0;i<n;i++){ cin>>m; if(m==a[num]) continue; a[++num]=m; } for(int i=2;i<=num;i++) { if(a[i]>a[i-1]&&f2) res++,f1=true,f2=false; if(a[i]<a[i-1]&&f1) res++,f2=true,f1=false; } cout<<res+1<<endl; } return 0; }
复制代码

 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17541948.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示