首页 > 其他分享 >Contrast Value(数学规律)

Contrast Value(数学规律)

时间:2023-07-10 18:36:42浏览次数:29  
标签:10 le ## Value int 数学 array Contrast

# Contrast Value

## 题面翻译

定义序列 $a_1,a_2,\dots,a_n$ 的权值是
$|a_1-a_2|+|a_2-a_3|+\dots+|a_{n-1}-a_n|$。

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

## 题目描述

For an array of integers $ [a_1, a_2, \dots, a_n] $ , let's call the value $ |a_1-a_2|+|a_2-a_3|+\cdots+|a_{n-1}-a_n| $ 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 $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 3 \cdot 10^5 $ ) — the size of the array $ a $ .

The second line contains $ n $ integers $ a_1, a_2, \cdot, a_n $ ( $ 0 \le a_i \le 10^9 $ ) — elements of the array itself.

The sum of $ n $ over all test cases doesn't exceed $ 3 \cdot 10^5 $ .

## 输出格式

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;
}

 

标签:10,le,##,Value,int,数学,array,Contrast
From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17541948.html

相关文章

  • 高等数学——无穷大与无穷小
    无穷大和无穷小无穷小无穷小指趋于\(0\),而不是\(-\infty\)。可以从正从负趋于无穷小。定义1如果函数\(f(x)\)当\(x\tox_{0}\)(或\(x\to\infty\))时的极限为\(0\),那么称函数\(f(x)\)为当\(x\tox_{0}\)(或\(x\to\infty\))时的无穷小。\(0\)可以作为无穷小的唯一的......
  • 高等数学——函数的极限
    函数的极限定义\(x\)趋于有限数\(a\)的极限。\[x\toa,f(x)\tob\]\(f(x)\)在\(x_{0}\)的去心领域内有定义(在\(x_{0}\)处可以没有定义)。若\(\existsA,\forall\delta>0,0<|x-x_{0}|<\delta\)时,$|f(x)-A|<\varepsilon$,则:\[\lim_{x\tox_{0}}f(x)=A\t......
  • 数学建模赛题类型
    评价类指标定权:主观经验,客观公式评价方法数据量小,评价指标少——————层次分析法数据量较小,样本数据具有时间序列特性——————灰色关联分析法时间序列时间序列是将某个统计量按照时间发生的先后顺序,按照其统计的值排列成的数列。时间序列分析通过已经发生的序列数......
  • 高等数学——数列的极限
    数列的极限定义数列:\(x_{1},x_{2},\dots,x_{n},\dots\)是一个从小到大的序列,称为数列,记为\(\{x_{n}\}\)其中\(x_{1}\)叫做项,\(x_{n}\)称为通项(一般项)。数列极限:设\(\{x_{n}\}\)是一个数列,\(\forall\varepsilon>0,\existsN\),使当\(n>N\)时,$|x_{n}-a|<\varepsilon$......
  • 编译运行Secure Value Recovery Service v2
    下载项目gitclonehttps://github.com/signalapp/SecureValueRecovery2.git 编译makedockersh报错 修改DockerfileARGPROTOC_GEN_GO_GITREV=6875c3d7242d1a3db910ce8a504f124cb840c23aRUNgoenv-wGOPROXY=https://goproxy.cn,directRUNgoinstallgoogle.......
  • 「高等数学」1.1.2 函数
    函数的概念定义:设数集\(D\subset\mathbf{R}\),则称映射\(f:D\rightarrow\mathbf{R}\)为定义在\(D\)上的函数,通常简记为\[y=f(x),x\inD,\]其中\(x\)称为自变量,\(y\)成为因变量,\(D\)称为定义域,记作\(D_f\),即\(D_f=D\).函数的定义中,对于每......
  • 高等数学——函数
    函数定义设数集\(D\subset\text{R}\),则称映射\(f:D\to\text{R}\)为定义在\(D\)上的函数,通常简记为:\[y=f(x),x\inD\]其中\(x\)称为自变量,\(y\)称为因变量,\(D\)称为定义域,记作\(D_{f}\),即\(D_{f}=D\),值域\(R_{f}=f(D)\)。每个\(x\inD\),都有唯一......
  • 高等数学笔记
    第一章函数与极限第一节映射与函数映射函数......
  • django values 与values_list的区别
    values values()方法返回包含字典的QuerySet<QuerySet[{'comment_id':1},{'comment_id':2}]>values_listvalues_list()方法返回一个包含元组的QuerySet<QuerySet[(1,),(2,)]>如果您使用values_list()单个字段,则可以使用flat=True返回单个值的QuerySet而不是1个元......
  • 数学英语
    单词中文说明linearalgebra线性代数parentheses括号即()associativelaw结合律线性代数中E32(E21A)=(E32E21)A,通过这个规律求单位矩阵Ielementarymatrix初等矩阵permutationmatrix置换矩阵不是矩阵的转置,是两个行(列)的交换,Exchagnerow1an......