Ian and Array Sorting
题目描述:
To thank Ian, Mary gifted an array
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
The first line of each test case consists of a single integer
The second line of each test case contains
It is guaranteed that the sum of
输出描述:
For each test case, output "
You may print each letter in any case (for example, "
样例:
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代码:
思路:
这个题直接写没有思路,但是可以转换成
设这个数组为
最后可以看到有两种改变的情况
-
在 ~ 时, 加或减 ,则 减或加 -
单独改变
和 且不影响其他数
当在
那么
而
当
所以此时只要将
#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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话