当前位置:网站首页>Codeforces Round #803 (Div. 2) C. 3SUM Closure
Codeforces Round #803 (Div. 2) C. 3SUM Closure
2022-07-03 17:48:00 【Codiplay】
C 3SUM Closure
题意
给出一个序列,问从中任意选三个数的和是否也在这个序列中
错因
-3 -1 1 1 没有讨论,也无法讨论,所以应该直接brute force,不进行分类讨论
分析
这题在数据比较小的时候边界很多,但是我们在数据量很少的时候直接三重循环暴力即可
所以我们只需要考虑数据量足够大的情况.
我们把的数据都暴力计算.
我们记录下数列中所有正数和负数的数量.正数或者负数超过2个肯定是不可以的
正数和负数各有一个时,它们必须互为相反数才满足条件.
#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);//精髓的操作,秀
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];//这个地方得想到用二分查找的方式,或者用map等方式减少查找复杂度
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;
}
边栏推荐
- 1146_ SiCp learning notes_ exponentiation
- 问题随记 —— 在 edge 上看视频会绿屏
- 鸿蒙第三次培训
- A day's work list of an ordinary programmer
- ES6类的继承
- i++与++i的区别:通俗易懂的讲述他们的区别
- c# . Net tool ecosystem
- PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
- 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
- [combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
猜你喜欢
Deops入门
Tensorboard quick start (pytoch uses tensorboard)
Records of long objects and long judgments in the stream of list
POM in idea XML graying solution
Test your trained model
Embedded-c language-7
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
[UE4] brush Arctic pack high quality Arctic terrain pack
[RT thread] NXP rt10xx device driver framework -- pin construction and use
随机推荐
Talk about the design and implementation logic of payment process
How to deploy applications on kubernetes cluster
SQL injection database operation foundation
ArrayList分析3 : 删除元素
Applet setting multi account debugging
Web-ui automated testing - the most complete element positioning method
[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
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
Qt调节Win屏幕亮度和声音大小
link preload prefetch
Design limitations of structure type (struct)
1147_ Makefile learning_ Target files and dependent files in makefile
MySQL grouping query
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
What is the difference between cloud server and cloud virtual machine
Embedded-c language-7
Interviewer: why is the value nil not equal to nil?
Fedora 21 安装 LAMP 主机服务器
[mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization