CodeForces - 1501C Going Home(暴力)

Posted Frozen_Guardian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1501C Going Home(暴力)相关的知识,希望对你有一定的参考价值。

题目链接:点击查看

题目大意:给出 n n n 个数,问是否存在四个数满足: a + b = c + d a+b=c+d a+b=c+d

题目分析:官方题解是直接 O ( n 2 ) O(n^2) O(n2) 暴力,因为每个数的范围是 [ 1 , 2.5 e 6 ] [1,2.5e6] [1,2.5e6] 的,每次匹配即使会产生一个新的 s u m sum sum 和,那么因为鸽巢原理,在匹配 5 e 6 5e6 5e6 次后,必定会产生一对 s u m sum sum 相同的二元对 ( i , j ) (i,j) (i,j)。同理在匹配第 4 ∗ 5 e 6 4*5e6 45e6 次后,一定会产生四个 s u m sum sum 相同的二元对 ( i , j ) (i,j) (i,j)

再结合官方的证明,如果有四对 s u m sum sum 相同的二元对,一定是可以组合出本题答案的

但是经过和冰哥的讨论,分析出了一个更优解

首先通过移项,将原式子转换为减法的形式: a − c = d − b a-c=d-b ac=db

将求和转换为求差,为了尽可能的不满足题目的要求,我们需要构造类似于斐波那契数列那样的数列,形如:

1 , 2 , 3 , 5 , 7 , 10 , 13 , 17 , 21... 1,2,3,5,7,10,13,17,21... 1,2,3,5,7,10,13,17,21...

就是初始时的 n u m = 1 , c n t = 1 num=1,cnt=1 num=1,cnt=1 ,每次执行两次 n u m + = c n t num+=cnt num+=cnt 后, c n t + = 1 cnt+=1 cnt+=1

这样构造出的数列,数学好的同学可以推公式记录其复杂度。一般的同学看到之后也可以大胆猜测这是 n 2 n^2 n2 级别递增的,打个表观察一下确实如此,当 n = 5000 n=5000 n=5000 的时候, n u m num num 已经到达 6 e 6 6e6 6e6

所以就可以套用经典的 “小范围暴力、大范围猜结论” 的解题方法了,经过上述的论证+猜想,当 n > 5000 n>5000 n>5000 的时候一定有解,且将原数列排列后,每次维护相邻两项的差值就可以快速获得答案了

时间复杂度相对于官方题解来说,更趋近于线性: O ( m i n ( n 2 , n l o g n ) ) O(min(n^2,nlogn)) O(min(n2,nlogn))

当然如果套用桶排序或基数排序的话可以将复杂度优化成线性的

代码:

// Problem: C. Going Home
// Contest: Codeforces - Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1501/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
#define X first
#define Y second
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
	T f=1;x=0;
	char ch=getchar();
	while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	x*=f;
}
template<typename T>
inline void write(T x)
{
	if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=5e6+100;
int a[N];
pair<int,int>node[N];
pair<int,int>b[N];
int main()
{
#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int n;
	read(n);
	for(int i=1;i<=n;i++) {
		read(a[i]);
	}
	if(n<=5000) {
		for(int i=1;i<=n;i++) {
			for(int j=i+1;j<=n;j++) {
				int s=a[i]+a[j];
				if(!node[s].X) {
					node[s]={i,j};
				} else if(i!=node[s].X&&i!=node[s].Y&&j!=node[s].X&&j!=node[s].Y) {
					puts("YES");
					cout<<i<<' '<<j<<' '<<node[s].X<<' '<<node[s].Y<<endl;
					return 0;
				}
			}
		}
		puts("NO");
	} else {
		puts("YES");
		for(int i=1;i<=n;i++) {
			b[i]={a[i],i};
		}
		sort(b+1,b+1+n);
		for(int i=2;i<=n;i++) {
			int d=b[i].X-b[i-1].X;
			if(!node[d].X) {
				node[d]={b[i-1].Y,b[i].Y};
			} else if(b[i].Y!=node[d].X&&b[i].Y!=node[d].Y&&b[i-1].Y!=node[d].X&&b[i-1].Y!=node[d].Y) {
				cout<<b[i].Y<<' '<<node[d].X<<' '<<b[i-1].Y<<' '<<node[d].Y<<endl;
				return 0;
			}
		}
	}
	return 0;
}

以上是关于CodeForces - 1501C Going Home(暴力)的主要内容,如果未能解决你的问题,请参考以下文章

错误形式(在 com.going.books.MainActivity.onCreate(MainActivity.java:19))

php Event Tickets Plus:禁止与会者修改其与会者信息,他们的RSVP“Going / Not Going”状态和/或他们的“Do”的能力

keep going

POJ 2195 Going Home

Going Bundleless: ES Modules

POJ2195 Going Home