Codeforces 1501C (数学+暴力)
题意描述
给定\(n\)个数(\(n≤200000\)),求是否存在一对(\(x,y\))和(\(z,w\)),使得\(a_{x}+a_{y}=a_{z}+a_{w}\)。
思路
由于题目中每个数字的范围位于[\(1,2.5*10^6\)],所以任意两个数的和都在[\(2,5*10^6\)]区间内。
由鸽巢原理得,枚举\(5*10^6\)对\((i,j)\)后,一定存在一对\((x,y)\)和\((z,w)\)具有相同的\(sum\)。
所以暴力枚举即可,复杂度\(O(min(5*10^6,n^2))\)。
AC代码
#include "iostream"
#include "cstring"
#include "string"
#include "vector"
#include "cmath"
#include "algorithm"
#include "map"
#include "set"
#include "queue"
#include "stack"
#include "cassert"
#include "unordered_map"
#include "sstream"
#include "cstdio"
using namespace std;
#define fi first
#define se second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) a.begin(),a.end()
#define rep(x,l,u) for(ll x=l;x=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);
#define seteps(N) setprecision(N)
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define lson (ind<<1)
#define rson (ind<<1|1)
#define endl '\n'
#define dbg(x) cerr << #x " = " << (x) << endl
#define mp make_pair
//#define LOCAL
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef pair PII;
typedef pair PCC;
typedef pair PDD;
typedef pair PLL;
typedef pair PIII;
struct Scanner {
bool hasNext = 1;
bool hasRead = 1;
int nextInt() {
hasRead = 0;
int res = 0;
char flag = 1, ch = getchar();
while(ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while(ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return res * flag;
}
ll nextLL() {
hasRead = 0;
ll res = 0;
char flag = 1, ch = getchar();
while(ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while(ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return res * flag;
}
char nextChar() {
hasRead = 0;
char ch = getchar();
while(ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return ch;
}
int nextString(char *str) {
hasRead = 0;
int len = 0;
char ch = getchar();
while(ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
while(ch != EOF && !isspace(ch)) {
hasRead = 1;
str[++len] = ch;
ch = getchar();
}
str[len + 1] = 0;
if(ch == EOF)
hasNext = 0;
return len;
}
} sc;
ll rd() {
ll x = sc.nextLL();
return x;
}
void rd(int &x) {
x = sc.nextInt();
}
void rd(ll &x) {
x = sc.nextLL();
}
void rd(char &x) {
x = sc.nextChar();
}
void rd(char* x) {
sc.nextString(x);
}
template
void rd(pair &x) {
rd(x.first);
rd(x.second);
}
template
void rd(T *x, int n) {
for(int i = 1; i <= n; ++i)
rd(x[i]);
}
template
void rd(vector &x,int n){
for(int i = 1; i <= n; ++i)
rd(x[i]);
}
void printInt(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x >= 10)
printInt(x / 10);
putchar('0' + x % 10);
}
void printLL(ll x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x >= 10)
printLL(x / 10);
putchar('0' + x % 10);
}
void pr(int x, char ch = '\n') {
printInt(x);
putchar(ch);
}
void pr(ll x, char ch = '\n') {
printLL(x);
putchar(ch);
}
template
void pr(pair x, char ch = '\n') {
#ifdef LOCAL
putchar('<');
pr(x.first, ' ');
pr(x.second, '>');
putchar(ch);
return;
#endif //LOCAL
pr(x.first, ' ');
pr(x.second, ch);
}
template
void pr(T *x, int n) {
for(int i = 1; i <= n; ++i)
pr(x[i], " \n"[i == n]);
}
template
void pr(vector &x) {
int n = x.size();
for(int i = 1; i <= n - 1; ++i)
pr(x[i], " \n"[i == n - 1]);
}
const int N=5*1e6+10;
const int M=1<<12;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const lll oone=1;
const double eps=1e-6;
const double pi=acos(-1);
int n,a[N];
PII b[N];
struct Solver
{
void InitOnce(){
}
void Read(){
rd(n);
rd(a,n);
}
void Solve(){
rep(i,1,n+1){
rep(j,i+1,n+1){
int sum=a[i]+a[j];
if(b[sum].fi){
if(i!=b[sum].fi && j!=b[sum].se && i!=b[sum].se && j!=b[sum].fi){
puts("YES");
pr(i,' ');pr(j,' ');pr(b[sum].fi,' ');pr(b[sum].se);
return;
}
}else{
b[sum]={i,j};
}
}
}
puts("NO");
}
}solver;
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif //LOCAL
solver.InitOnce();
int t=1;
//t=sc.nextInt();
//t=INF;
while(t--){
solver.Read();
if(!sc.hasRead) break;
solver.Solve();
if(t>=1) puts("");
if(!sc.hasNext) break;
}
}