首页 > 其他分享 >Ian and Array Sorting

Ian and Array Sorting

时间:2023-04-15 11:56:54浏览次数:46  
标签:Ian 奇数 array Sorting test Array YES make

题目链接

题目描述:

To thank Ian, Mary gifted an array \(a\) of length \(n\) to Ian. To make himself look smart, he wants to make the array in non-decreasing order by doing the following finitely many times: he chooses two adjacent elements \(a_i\) and \(a_{i+1}\) \((1≤i≤n−1 )\), and increases both of them by \(1\) or decreases both of them by \(1\). Note that, the elements of the array can become negative.

As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order.

输入描述:

The first line contains a single integer \(t(1≤t≤10^4)\) — the number of test cases. The description of test cases follows.

The first line of each test case consists of a single integer \(n(2≤n≤3⋅10^5)\) — the number of elements in the array.

The second line of each test case contains \(n\) integers \(a_1,a_2,…,a_n(1≤ai≤10^9)\) — the elements of the array \(a\).

It is guaranteed that the sum of \(n\)over all test cases does not exceed \(3⋅10^5\).

输出描述:

For each test case, output "\(YES\)" if there exists a sequence of operations which make the array non-decreasing, else output "\(NO\)".

You may print each letter in any case (for example, "\(YES\)", "\(Yes\)", "\(yes\)", "\(yEs\)" will all be recognized as positive answer).

样例:

input:

5
3
1 3 2
2
2 1
4
1 3 5 7
4
2 1 4 3
5
5 4 3 2 1

output:

YES
NO
YES
NO
YES

AC代码:

思路:

这个题直接写没有思路,但是可以转换成\(a_2-a-1,a_3-a_2...a_n-a_{n-1}\)

设这个数组为\(b_1,b_2,...b_{n-1}\)

\(a_1,a_2...a_n\)是一个非递减序列当且仅当\(b\)数组为非负数

最后可以看到有两种改变的情况

  • \(i\)在\(1\)~\(n-3\)时,\(b_i\) 加或减 \(1\),则 \(b_i + 2\) 减或加\(1\)

  • 单独改变\(b_2\)和\(b_{n-2}\)且不影响其他数

所以在\(n\)为奇数时,\(n-2\)为奇数。所以对于\(b_{n-2}\)我们可以给它加上足够大的数

所以\(i\)为奇数时,我们可以任意改变其他\(i\)为奇数的\(b\)的值使得它们为非负数

而\(i\)为偶数时同理,给\(b_2\)加上足够大的数,使得其他\(i\)为偶数的\(b\)的值为非负数

当\(n\)为偶数时,\(n-2\)为偶数,我们给\(b_2\)和\(b_{n-2}\)加上足够大的数,此时只能保证\(i\)为偶数时\(b\)的值都为非负数

\(i\)为奇数的\(b\)的值,我们可以随意分配,但是\(b\)的值的总和都是不变的

所以此时只要将\(i\)为奇数时的b的总和加起来,其值大于等于\(0\)则代表可以保证\(b\)的值为非负数

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

void solve()
{
	int n;
	cin >> n;

	vector<int> a(n + 1);

	for(int i = 1; i <= n; i ++)
		cin >> a[i];

	if(n % 2 == 1)
	{
		cout << "YES\n";

		return;
	}

	LL sum = 0;

	for(int i = 2; i <= n; i += 2)
		sum += a[i] - a[i - 1];

	if(sum >= 0)
	{
		cout << "YES\n";

		return;
	}

	cout << "NO\n";
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	int T;
	cin >> T;

	while(T --)
		solve();

	return 0;
}

标签:Ian,奇数,array,Sorting,test,Array,YES,make
From: https://www.cnblogs.com/KSzsh/p/17320754.html

相关文章

  • Debian安装数据库
    Debian安装数据库本来用的MySQL,但是安装MySQL很麻烦,MariaDB作为MySQL的替代品可以直接使用以前用MySQL的方式使用参考链接:如何在Debian10上安装MariaDB|linux资讯(linux265.com)[笔记]Mariadb安装并配置远程访问-知乎(zhihu.com)Host'xxx'isnotallowedtoc......
  • ArrayList类
    ArrayList类ArrayList简单介绍**感觉很像c++里面的vector**1.储存的类型都是相同的2.打印的时候直接打印内容而不是地址ArrayLsit的简单使用代码示例importjava.util.ArrayList;publicclassArray{publicstaticvoidmain(String[]args){ArrayLi......
  • 13-ArrayList&学生管理系统
    1.ArrayList集合和数组的优势对比:长度可变(自动扩容)添加数据的时候不需要考虑索引,默认将数据添加到末尾1.1ArrayList类概述什么是集合提供一种存储空间可变的存储模型,存储的数据容量可以发生改变ArrayList集合的特点长度可以变化,只能存储引用数据类型。泛......
  • 如何在Debian 9上安装Python 3.7
    转自https://help.aliyun.com/document_detail/146390.html 执行以下命令安装构建Python源所需的包。 sudoaptupdatesudoaptinstallbuild-essentialzlib1g-devlibncurses5-devlibgdbm-devlibnss3-devlibssl-devlibreadline-devlibffi-devwget执行以下命......
  • Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
    题目地址:http://codeforces.com/contest/551/problem/E将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个整体偏移量。修改操作:l~r区间内,对两端的块进行暴力处理,对中间的整体的块用整体偏移量标记增加了多少。时间复杂度:O(2*sqrt(n)+n/sqrt(n)).查询操作:对每一块二分,查找......
  • 05 Rasterization (Triangles)
    1.ScreenPixel(RGB0-255)ScreenSpaceViewportTransform将屏幕进行缩放,然后将重心平移到原点,得到视口变换矩阵:2.Triangles最基础的多边形,任意多边形可以拆成三角形,三角形一定是平面图形,三角形内外定义清晰并可用叉积辨别(像素中心点),三角形内部属性可用三个点的属性由......
  • Codeforces Round #284 (Div. 1) C. Array and Operations (最大流)
    题目地址:http://codeforces.com/contest/498/problem/C分别分解出每个数字的质因子,然后第奇数个数字的质因子在左边集合,偶数个数字的质因子在右边集合,建立源点和汇点,然后根据每个数字含有的质因子的个数建边,跑一遍最大流即可。代码如下:#include<iostream>#include<string.h>......
  • POJ 1094Sorting It All Out(拓扑排序)
    题目地址:http://poj.org/problem?id=1094这个题改了一下午。。代码越改越挫。。凑活着看吧。。#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#include<map>......
  • Array和String
    Array对象newArray()vararr=newArray(2);//创建长度为2的空数组检测参数是否是数组创建数组vararr=newArray(5);检测参数是否是数组instanceof/Array.isArray(params)/***@param{[]]}params@returns*/functionreverse(params){if(paramsinstanceof......
  • Debian下解决vim不能安装问题
    新安装的一个Debian系统,无法安装vim编辑器找到问题是系统自带了一个依赖包vim-common。。。。。。 卸载这个依赖包apt-getremovevim-common 重新安装vimapt-getinstallvim 成功解决 ......