当前位置:网站首页>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;
}
边栏推荐
- (8) HS corner detection
- Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
- Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
- Leetcode540: a single element in an ordered array
- POM in idea XML graying solution
- Applet setting multi account debugging
- What is the difference between cloud server and cloud virtual machine
- (9) Opencv Canny edge detection
- Kubernetes resource object introduction and common commands (III)
- BFS - topology sort
猜你喜欢

Golang单元测试、Mock测试以及基准测试

TCP拥塞控制详解 | 3. 设计空间

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

Select 3 fcpx plug-ins. Come and see if you like them

聊聊支付流程的设计与实现逻辑

Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use

QT学习日记9——对话框

Type conversion, variable

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
随机推荐
Create a new file from templates with bash script - create new file from templates with bash script
c# .net 工具生态
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
PHP processing - watermark images (text, etc.)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
Applet setting multi account debugging
数学公式(测试)
STM32 realizes 74HC595 control
What is the difference between cloud server and cloud virtual machine
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
MySQL has been stopped in the configuration interface during installation
[combinatorics] generating function (shift property)
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
MySQL grouping query
STM32实现74HC595控制
自动渗透测试工具核心功能简述
Applet with multiple tabs and Swipers + paging of each tab
(8) HS corner detection
