Codeforces Global Round 11 A. Avoiding Zero(前缀和)
题目链接:https://codeforces.com/contest/1427/problem/A
题意
将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和。
题解
计算正、负前缀和,如果二者和为 \(0\),则不存在满足题意的排列,否则将绝对值较大的一方排在前面即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
int pos = 0, neg = 0;
for (auto &x : a) {
cin >> x;
(x < 0 ? neg : pos) += x;
}
if (pos + neg == 0) {
cout << "NO" << "\n";
continue;
}
if (pos + neg > 0) {
sort(a.begin(), a.end(), greater<>());
} else {
sort(a.begin(), a.end(), less<>());
}
cout << "YES" << "\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " \n"[i == n - 1];
}
}
return 0;
}
Codeforces Global Round 11 A. Avoiding Zero(前缀和)的更多相关文章
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- Codeforces Global Round 11【ABCD】
比赛链接:https://codeforces.com/contest/1427 A. Avoiding Zero 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正 ...
- Codeforces Global Round 11 A~D题解
A.Avoiding Zero 题目链接:https://codeforces.ml/contest/1427 题目大意:给定一个数组a1,a2...,an,要求找出一个a重排后的数组b1,b2,.. ...
- Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)
题目链接:https://codeforces.com/contest/1427/problem/D 题意 给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)
题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...
- Codeforces Global Round 11 B. Chess Cheater(贪心)
题目链接:https://codeforces.com/contest/1427/problem/B 题意 给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi (DP)
题意:有\(r\)X\(r\)的网格图,有\(n\)位名人,会在\(t_i\)时出现在\((x_i,y_i)\),如果过了\(t_i\)名人就会消失,从某一点走到另外一点需要花费的时间是它们之间的曼哈 ...
- Codeforces Global Round 4 B. WOW Factor (前缀和,数学)
题意:找出序列中有多少子序列是\(wow\),但是\(w\)只能用\(vv\)来表示. 题解:我们分别记录连续的\(v\)和\(o\)的个数,用\(v1\)和\(v2\)存,这里要注意前导\(o\)不 ...
- Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)
题意:你和朋友进行了\(n\)个回合的棋艺切磋,没有平局,每次要么输要么赢,每次赢可以得一分,假如前一局也赢了,那么可以得两分,结果已成定局,但是你确可以作弊,最多修改\(k\)个回合的结果,问你作弊 ...
随机推荐
- [日常填坑系列]CAP食用指南-版本引用问题
一.前言 最近,由于好久没升级底层框架,一直用着netcore2.2版本,导致有些包没能更新到最新的版本,例如:CAP. 然而,在最近升级到CAP:3.1.2版本的时候,发现有点小坑,只能退回到CAP ...
- 直播预告 | 开源的云原生开发环境 —— Nocalhost
直播来啦!本次云原生学院邀请到腾讯云 CODING DevOps 后端工程师王炜为大家分享<开源的云原生开发环境 -- Nocalhost>. 直播信息 讲师:王炜 - 腾讯云 CODIN ...
- rm: cannot remove `/tmp/localhost-mysql_cacti_stats.txt': Operation not permitted
[root@DBslave tmp]# chown zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt
- 【Oracle】等待事件之 V$SESSION_WAIT
(1)-V$SESSION_WAIT 这是一个寻找性能瓶颈的关键视图.它提供了任何情况下session在数据库中当前正在等待什么(如果session当前什么也没在做,则显示它最后的等待事件).当系统存 ...
- 【ORA】ORA-01078和LRM-00109 解决方法
今天切换到asm实例的时候,发现是一个空实例,尝试启动实例,结果报错ORA-01078和LRM-00109 SQL> startupORA-01078: failure in processin ...
- os.walk() 遍历目录下的文件夹和文件
os.walk(top, topdown=True, onerror=None, followlinks=False) top:顶级目录 os.walk()返回一个三元tupple(dirpath, ...
- 关于阿里云服务器安装了Apache开放80端口访问不了网页
先用netstat -tlunp查看80端口是否打开,再关闭服务器的防火墙,可以用 systemctl status firewalld 查看防火墙状态 systemctl stop firewal ...
- 07--Docker安装Redis
1.拉取redis:3.2 docker pull redis:3.2 2.创建redis容器 docker run -p 6379:6379 -v /zhengcj/myredis/data:/da ...
- springAOP的概述及使用
Spring AOP SpringAOP是Spring中非常重要的功能模块之一,该模块提供了面向切面编程,在事务处理,日志记录,安全控制等操作中广泛使用. SpringAOP的基本概念 AOP的概念 ...
- Java 栈的使用
讲栈之前,要先讲一下Deque双端队列 既可以添加到队尾,也可以添加到队首 既可以从队首获取又可以从队尾获取 public interface Deque<E> extends Queue ...