一千组数据,枚举出所有任意两个数的组合,一共1000 * 1000组
记录组合来的下标 然后二分查相反数并查重。
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 1010;
int a[MAXN] ;
struct num //定义两数组合及位置结构体
{
ll sum;
ll x, y;
friend bool operator < (num a, num b) //重载符号以应用二分
{
return a.sum < b.sum;
}
}arr[MAXN * MAXN];
bool cmp(num a, num b) //sort用cmp
{
return a.sum < b.sum;
}
int main()
{
int n;
cin>>n;
for(int i = 0; i < n; i++)
{
cin>>a[i];
}
int flag = 0;
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
arr[flag].sum = a[i] + a[j]; //记录组合和
arr[flag].x = i; //记录组合两数的下标
arr[flag].y = j;
flag ++;
}
}
sort(arr, arr + flag, cmp); //排序 配合符号重载
/*for(int i = 0; i < flag; i++)
{
cout<<arr[i].sum<<' '<<arr[i].x<<' '<<arr[i].y<<endl;
}*/
for(int i = 0; i < flag; i++)
{
num tmp = arr[i];
tmp.sum = -arr[i].sum;
int fst = lower_bound(arr, arr + flag, tmp) - arr; //查上限
int lst = upper_bound(arr, arr + flag, tmp) - arr; //查下限
for(int j = fst; j <= lst; j++)
{
if(arr[j].sum == tmp.sum)
{
if(arr[j].x != tmp.x && arr[j].y != tmp.y && arr[j].x != tmp.y && arr[j].y != tmp.x) // 判重
{
cout<<"Yes"<<endl;
return 0;
}
}
}
}
cout<<"No"<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
};
struct cmp1
{
bool operator () (const node &a,const node &b) const {
return a.x < b.x;
}
};
int main()
{
vector<node>pq;
pq.push_back({1,3});
pq.push_back({2,8});
pq.push_back({4,3});
node a;
a.x = a.y = 3;
vector<node>::iterator it = lower_bound(begin(pq),end(pq),a,cmp1());
cout << (*it).x << endl;
}