Codeforces Round 276 (Div 2) D is difficult then the inverse enumeration multiple of two points O (NLOGN) Maximum Value n Number of large number of mod decimal to find the maximum remainder __ Question Bank-CF

Source: Internet
Author: User

D. Maximum Value time limit per test 1 second memory limit/test 256 megabytes input standard input output standard out Put

You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder Ai divided by AJ), where 1≤i, J≤n and Ai≥aj. Input

The contains integer n-the length of the sequence (1≤n≤2 105).

The second line contains n space-separated integers ai (1≤ai≤106). Output

Print the answer to the problem. Sample Test (s) input

3
3 4 5
Output
2



"Codeforces Round 276 (Div 2) D" "The number of inverse enumerated multiples of second-fractional group optimization O (nlogn)" Maximum Value n numbers mod decimal find maximum remainder

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout); #define MS (X,y) memset (x,y,sizeof (x)) #define MC (x,y) memcpy (x,y,sizeof (x)) #define MP (x,y) Make_pair (x,y) #define LS o<<1 #define
RS o<<1|1 typedef long LL;
typedef unsigned long UL;
typedef unsigned int UI; Template <class t1,class t2>inline void Gmax (T1 &a,t2 b) {if (b>a) a=b;} template <class T1,class T2>inli
ne void Gmin (T1 &a,t2 b) {if (b<a) a=b;} const int n=2e5+10,m=2e6+10,z=1e9+7,ms63=1061109567;
int n;
int a[n],b[m];
		int main () {while (~SCANF ("%d", &n)) {for (int i=1;i<=n;++i) scanf ("%d", &a[i]); Sort (a+1,a+n+1);a[n+1]=a[n]<<1;
		for (int i=1;i<=n;++i) {for (int j=a[i];j<a[i+1];++j) b[j]=a[i];
		int ans=0;
			for (int i=n-1;i>=1;--i) {if (A[i]+1<=ans) break;
			if (a[i]==a[i+1]) continue;
			for (int x=a[i]+a[i]-1;x<a[n+1];x+=a[i]) {gmax (ans,b[x]%a[i]);
	} printf ("%d\n", ans);
return 0; }/* "trick&& spit" wow.

Try to optimize, the violence pruning unexpectedly AC ~ ~ with the wrong way ac a problem is really the heart of the most happy 2333 "Ask" Give you n (2e5) number, each number of values in [1,1e6] between, let you find Max (A[i]%a[j]), A[i]>=a[j].
"Type" number theory "analysis" we assume that these n numbers have been sorted from the previous to the following in order from small to large. So-if there is no a[i]>=a[j limit, the answer must be Max (A[x]), A[x]!=a[n] However, there are now a[i]>=a[j requirements.

How do we do it.
The first thing I did was to enumerate the large number and then find the small number to update the answer, and the time complexity of the algorithm is O (nsqrt (n) log (n)) tle.
However, the time to do the problem did not think of the reverse.

When we look for primes, the time complexity of the enumerator to find the factor is O (nsqrt (n)) but if you convert the order, the time complexity of the enumerator screen is only O (Nlog (n)).
This is the same problem.
1, the enumeration factor to find a multiple, for a multiple of a factor, we find the largest number smaller than this (via Lower_bound ()-1) 2, and pruning from large to small enumerations is done in O (Nlogn (n) log (n)) time.

In addition, the first can be greedy x (x/2+1) for ans, can play a role in optimization.
PS: Because the value of a[] is not large. Therefore, we can use a different approach, directly record the number of <=x the largest number of b[x].
Then we can query O (1). can achieve the overall O (NLOGN) time complexity. La La la ~ "Time complexImpurity && optimization "O (Nlogn Logn)->o (NLOGN) * * 

"Codeforces Round 276 (Div 2) D" "Number of positive numbers of inverse enumeration multiples" Maximum Value n numbers a mod decimal to find the maximum remainder

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout); #define MS (X,y) memset (x,y,sizeof (x)) #define MC (x,y) memcpy (x,y,sizeof (x)) #define MP (x,y) Make_pair (x,y) #define LS o<<1 #define
RS o<<1|1 typedef long LL;
typedef unsigned long UL;
typedef unsigned int UI; Template <class t1,class t2>inline void Gmax (T1 &a,t2 b) {if (b>a) a=b;} template <class T1,class T2>inli
ne void Gmin (T1 &a,t2 b) {if (b<a) a=b;} const int n=2e5+10,m=1e6+10,z=1e9+7,ms63=1061109567;
int n;
int a[n];
BOOL E[m];
		int main () {while (~SCANF ("%d", &n)) {MS (e,0); for (int i=1;i<=n;++i) {scanf ("%d", &a[i]);E[a[i]]=1;}
		Sort (a+1,a+n+1); a[n+1]=1e9;
		int ans=0;
		for (int i=n;i>1;--i) if (e[a[i]]&&e[a[i]/2+1]) gmax (ans,a[i]% (a[i]/2+1));
			for (int i=n-1;i>=1;--i) {if (A[i]+1<=ans) break;
			if (a[i]==a[i+1]) continue;
			int p=i;
				while (1) {int x= (a[p]/a[i]+1) *a[i];
				P=lower_bound (a+p,a+n+1,x)-a-1;
				Gmax (Ans,a[p]%a[i]);
			if (++p>n) break;
	} printf ("%d\n", ans);
return 0; }/* "trick&& spit" wow.

Try to optimize, the violence pruning unexpectedly AC ~ ~ with the wrong way ac a problem is really the heart of the most happy 2333 "Ask" Give you n (2e5) number, each number of values in [1,1e6] between, let you find Max (A[i]%a[j]), A[i]>=a[j].
"Type" number theory "analysis" we assume that these n numbers have been sorted from the previous to the following in order from small to large. So-if there is no a[i]>=a[j limit, the answer must be Max (A[x]), A[x]!=a[n] However, there are now a[i]>=a[j requirements.

How do we do it.
The first thing I did was to enumerate the large number and then find the small number to update the answer, and the time complexity of the algorithm is O (nsqrt (n) log (n)) tle.
However, the time to do the problem did not think of the reverse.

When we look for primes, the time complexity of the enumerator to find the factor is O (nsqrt (n)) but if you convert the order, the time complexity of the enumerator screen is only O (Nlog (n)).
This is the same problem.
1, the enumeration factor to find a multiple, for a multiple of a factor, we find the largest number smaller than this (via Lower_bound ()-1) 2, and pruning from large to small enumerations is done in O (Nlogn (n) log (n)) time.

In addition, the first can be greedy x (x/2+1) for ans, can play a role in optimization. PS: Because the value of a[] is not large。 Therefore, we can use a different approach, directly record the number of <=x the largest number of v[x].
Then we can query O (1). can achieve the overall O (NLOGN) time complexity. La La la ~ "Time complexity && Optimization" O (Nlogn Logn)->o (NLOGN) * *


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: