当前位置:网站首页>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;
}
边栏推荐
- Life perception 1
- Detailed explanation of common network attacks
- As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
- (9) Opencv Canny edge detection
- AcWing 4489. Longest subsequence
- [UE4] brush Arctic pack high quality Arctic terrain pack
- Hongmeng third training
- Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
- Select 3 fcpx plug-ins. Come and see if you like them
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
猜你喜欢
List的stream中Long对象与long判等问题记录
How to install PHP on Ubuntu 20.04
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
MySQL has been stopped in the configuration interface during installation
Golang单元测试、Mock测试以及基准测试
Hongmeng fourth training
Getting started with deops
1146_ SiCp learning notes_ exponentiation
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
随机推荐
When absolutely positioned, the element is horizontally and vertically centered
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
Applet setting multi account debugging
Gear2021 monthly update - December
IntelliJ 2021.3 short command line when running applications
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
Draw some simple graphics with MFC
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
AcWing 3438. 数制转换
c# .net 工具生态
OpenSSL的SSL/BIO_get_fd
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
The difference between get and post
自动渗透测试工具核心功能简述
TCP拥塞控制详解 | 3. 设计空间
VM11289 WAService. js:2 Do not have __ e handler in component:
Design limitations of structure type (struct)
Kotlin的协程:上下文
聊聊支付流程的設計與實現邏輯