当前位置:网站首页>Permutation (greedy strategy)
Permutation (greedy strategy)
2022-06-12 09:04:00 【Little wish】
link :https://ac.nowcoder.com/acm/contest/10746/J
source : Cattle from
Title Description
give n Numbers , Is there a calculation order for these numbers , So that the number will not exceed 3 Not less than 0?
Input description
The first line gives a positive integer t , ( 1 ≤ t ≤ 1000 ) t,(1 \le t \le 1000) t,(1≤t≤1000) Number of representative test data groups
The first row of each set of test data is a positive integer n , ( 1 ≤ n ≤ 500 ) n,(1 \le n \le 500) n,(1≤n≤500)
The second line contains n n n A number separated by a space
Input to ensure that each number is − 3 , − 2 , − 1 , + 0 , + 1 , + 2 , + 3 −3, −2, −1, +0, +1, +2, +3 −3,−2,−1,+0,+1,+2,+3 One of them .
Output description
Each group of test data output one line ,“Yes” or “No”
The sample input
2
4
+3 +2 -1 -2
5
+3 +2 +1 +0 +2
Sample output
Yes
No
Their thinking
greedy , Think about it first − 3 -3 −3, Judge whether the + 3 +3 +3 or + 2 − 1 + 2 +2 -1 +2 +2−1+2 or + 2 + 1 +2 +1 +2+1 or + 1 + 1 + 1 +1 +1 +1 +1+1+1 Neutralize it , Until there is no − 3 -3 −3 until ; If nothing can neutralize in the middle − 3 -3 −3, be “NO”;
Then consider + 3 +3 +3, When + 3 +3 +3 The number of is greater than 1 when , Judge whether the − 3 -3 −3 or − 2 + 1 − 2 -2 +1 -2 −2+1−2 or − 2 − 1 -2 -1 −2−1 or − 1 − 1 − 1 -1 -1 -1 −1−1−1 Neutralize it , until + 3 +3 +3 The number is less than or equal to 1 until ; If nothing can neutralize in the middle + 3 +3 +3, be “NO”;
If none of them “NO”, be “YES”.
AC Code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int T, n, a[7], *cnt;
bool check() {
int t = 0;
for (int i = -3; i <= 3; i++) {
t += i * cnt[i];
}
if (t > 3 || t < 0) return false;
//for -3, +3 || +2 +2 -1 || +2 +1 || +1 +1 +1
t = min(cnt[-3], cnt[3]);
cnt[-3] -= t;
cnt[3] -= t;
t = min(cnt[2], cnt[-1]);
cnt[2] -= t;
cnt[-1] -= t;
cnt[1] += t;
t = min(cnt[-3], cnt[1]);
cnt[-3] -= t;
cnt[1] -= t;
cnt[-2] += t;
if (cnt[-3] > 0) return false;
// for 3, -3 || -2 -2 +1 || -2 -1 || -1 -1 -1
t = min(cnt[-2], cnt[1]);
cnt[-2] -= t;
cnt[1] -= t;
cnt[-1] += t;
t = min(cnt[3], cnt[-1]);
cnt[3] -= t;
cnt[-1] -= t;
cnt[2] += t;
if (cnt[3] > 1) return false;
return true;
}
int main() {
//freopen(" Permutation formula .in", "r", stdin);
scanf("%d", &T);
cnt = &a[3];
while (T--) {
memset(a, 0, sizeof(a));
scanf("%d", &n);
int t;
for (int i = 1; i <= n; i++) {
scanf("%d", &t);
cnt[t]++;
}
if (check()) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
边栏推荐
- 【字符集九】gbk拷贝到Unicode会乱码?
- Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps
- mySql学习记录——二、mySql建表命令
- Handling abnormal data
- MySQL learning record - II. MySQL create table command
- ip、DNS、域名、URL、hosts
- Source code and scheme for target recognition, detection and 6D attitude estimation (the most advanced method and data set)
- 目标识别、检测和 6D 姿态估算源码与方案(最先进的方法和数据集)
- 解决當打開Unity時 提示項目已經打開,而自己之前並沒有打開過(可能之前异常關閉)的問題
- Several ways to restart kubernetes pod
猜你喜欢

Redis installation test

第八章-数据处理的两个基本问题

xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”
![Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps](/img/0a/65bc44850e52204af278e50a8f86eb.jpg)
Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps

Filters and listeners
![[untitled] task3 multiple recall](/img/84/81ada5dd7afac62c31a52e743547e9.png)
[untitled] task3 multiple recall

Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low

第四章-第一个程序

Summary of common character sets
![[character set 9] will GBK be garbled when copied to unicode?](/img/dc/c9ec4a90355d30479f23fdead4b349.png)
[character set 9] will GBK be garbled when copied to unicode?
随机推荐
(十四)InputField逻辑分析
IP, DNS, domain name, URL, hosts
Node sample background setup
Source code and scheme for target recognition, detection and 6D attitude estimation (the most advanced method and data set)
Filters and listeners
Union selector
Flink传入自定义的参数或配置文件
Background position - mixed units
(14) Inputfield logic analysis
43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
ISCSI详解(五)——ISCSI客户端配置实战
JS to refresh the page after loading
Difference between binary GB and gib
利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
2022 simulated examination platform operation of high voltage electrician work license question bank
Construction of memcached cache service under Linux:
解压缩zip文件的工具类
Judge whether the object is empty
Chapter 8 - two basic problems of data processing
第三章 寄存器 (内存访问)