当前位置:网站首页>Codeforces Round #803 (Div. 2) C. 3SUM Closure
Codeforces Round #803 (Div. 2) C. 3SUM Closure
2022-07-03 17:53:00 【Codiplay】
C 3SUM Closure
The question
Give a sequence , Ask whether the sum of three arbitrary numbers is also in this sequence
Cause of error
-3 -1 1 1 Not discussed , Can't discuss , So it should be direct brute force, No classified discussion
analysis
This problem has many boundaries when the data is relatively small , However, when there is a small amount of data, we can directly triple cycle violence
So we only need to consider the case that the amount of data is large enough .
We putAll the data are calculated violently .
We record the number of all positive and negative numbers in the sequence .Positive or negative numbers exceed 2 It's definitely not possible
Positive and negative numbers each have an hour , They must be opposite to each other to satisfy the condition .
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
bool cabs(int x, int y){
return abs(x) > abs(y);
}
int a[2000009];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
cin >> t;
while(t -- ) {
int n, c = 0, c2 = 0;
cin >> n;
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
c += a[i] > 0;
c2 += a[i] < 0;
}
if(c > 2 || c2 > 2) {
cout << "no\n";
continue;
}
sort(a, a + n, cabs);// Quintessential operation , Show
n = min(n, 9);
sort(a, a + n);
bool tag = true;
for (int i = 0; i < n; i ++ ) {
for (int j = i + 1; j < n; j ++ ) {
for (int k = j + 1; k < n; k ++ ) {
int w = a[i] + a[j] + a[k];// This place has to think of using binary search , Or use map And other ways to reduce the search complexity
int nx = lower_bound(a, a + n, w) - a;
if(nx == n || a[nx] != w) {
tag = false;
}
}
}
}
if(tag)cout << "yes\n";
else cout << "no\n";
}
return 0;
}
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
cin >> t;
while(t -- ) {
int n, sum = 0;
int cnt[2] = {0, 0};
cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
cnt[0] += a[i] > 0;
cnt[1] += a[i] < 0;
sum += a[i];
}
if(n <= 10) {
set<int> s;
for (int i = 0; i < n; i ++ )s.insert(a[i]);
bool ok = true;
for (int i = 0; i < n; i ++ ) {
for (int j = i + 1; j < n; j ++) {
for (int k = j + 1; k < n; k ++ ) {
if(!s.count(a[i] + a[j] + a[k])) {
ok = false;
}
}
}
}
if(ok) cout << "yes\n";
else cout << "no\n";
}
else {
if(cnt[0] + cnt[1] == 1) cout << "yes\n";
else if(cnt[0] > 1 || cnt[1] > 1 || sum) cout << "no\n";
else cout << "yes\n";
}
}
return 0;
}
边栏推荐
- PHP MySQL reads data
- PHP MySQL preprocessing statement
- PHP MySQL create database
- OpenSSL的SSL/BIO_get_fd
- [mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
- 分布式的任务分发框架-Gearman
- Kotlin's collaboration: Context
- Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
- PHP MySQL where clause
- 聊聊支付流程的設計與實現邏輯
猜你喜欢
Codeforces Round #803 (Div. 2) C. 3SUM Closure
Records of long objects and long judgments in the stream of list
IntelliJ 2021.3 short command line when running applications
PHP MySQL preprocessing statement
PHP MySQL inserts data
1147_ Makefile learning_ Target files and dependent files in makefile
Draw some simple graphics with MFC
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
How to read the source code [debug and observe the source code]
随机推荐
Redis core technology and practice - learning notes (11): why not just string
Wechat applet for the first time
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
Create a new file from templates with bash script - create new file from templates with bash script
Talk about the design and implementation logic of payment process
The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
Micro service component sentinel console call
List of financial products in 2022
【统信UOS】扫描仪设备管理驱动安装
国内如何购买Google Colab会员
毕业总结
MySQL has been stopped in the configuration interface during installation
PHP MySQL order by keyword
Embedded-c language-7
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
Enterprise custom form engine solution (XI) -- form rule engine 1
QT adjust win screen brightness and sound size
AcWing 4489. 最长子序列
The difference between i++ and ++i: tell their differences easily
SQL injection database operation foundation