CF1500A(桶思想,鸽巢原理)

CF1500A(桶思想,鸽巢原理)

题意

对序列 \(a\) ,找到一对 \(a_i +a_j = a_k+a_w\) 如果有,输出下标

\(1 \le a_i \le 250000\)

思路

和的值域为 \([2,5000000]\),对值域开桶,当枚举对数超过值域后,一定会有两对放在同一个桶中。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<random>
#include<iomanip>
#define yes puts("yes");
#define inf 0x3f3f3f3f
#define ll long long
#define linf 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define endl '\n'
#define int long long
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x;}
typedef pair<int,int> PII;
const int MAXN =10 + 2e5 ,mod=1e9 + 7;

void solve()
{    
	int n; cin >> n;
	vector<int> a(n);
	rep(i,0,n - 1) cin >> a[i];
	vector<PII> s(5e6 + 1,make_pair(-1,-1));
	rep(i,0,n - 1) {
		rep(j,i + 1,n - 1) {
			if(s[a[i] + a[j]].first == -1) s[a[i] + a[j]] = {i,j};
			else {
				int k = a[i] + a[j];
				if(s[k].first != i && s[k].second != i
				&& s[k].first != j && s[k].second != j) {
					cout << "YES\n";
					cout << s[k].first + 1 << " " << s[k].second + 1 << " "
					<< i + 1 << " " << j + 1 << endl;
					return;
				}
			}
		}
	}
	cout << "NO\n";
}
signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

	//int T;cin>>T;
	//while(T--)
		solve();

	return 0;
}

和它一模一样的还有 B-Four Xor_第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(热身赛) (nowcoder.com)

posted @ 2022-07-05 23:33  Mxrurush  阅读(31)  评论(0编辑  收藏  举报