HDU5536 2015 Changchun Division J Dictionary Tree or violence Chip Factory Dictionary order procedure + violence practices __ACM

Source: Internet
Author: User

First of all, the violent approach can be AC.

#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #include < iostream> #include <string> #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=1010,m=0,z=1e9+7,ms63=1061109567;
int Casenum,casei;
int n;
int a[n];
	int main () {scanf ("%d", &casenum); for (casei=1;casei<=casenum;casei++) {scanf ("%d", &n);
		for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
		int ans=0; for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {for (int k=j+1;k<=n;k++) {gmax (ans, a[i]+a [j])
					^a[k]);
					Gmax (ans, (a[i]+a[k]) ^a[j]);
				Gmax (ans, (a[j]+a[k]) ^a[i]);
	} printf ("%d\n", ans);
return 0;

}/* "trick&& spit" 1, the dictionary tree is to support the delete operation of Oh 2, vigorously out of miracles.
"The" T (1000) Group of data, up to 10 groups of large data.
For each set of data, give you the number of n ([3,1000]) a[], (0&LT;=A[I]&LT;=1E9).

Let you figure out three numbers a[i],a[j],a[k],i,j,k each other to make (S[i]+s[j]) ^s[k] The right value as large as possible, and output this weight. The "type" Trie "analysis" time limit given to 9s, we consider O (n^3) blunt violence.
And then it just went off. Although the number of operations up to T*1E9/6, but the bit is very fast. This method is very enlightening. 233 *

But I'm sure it's the best way to get a dictionary.

#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #include < iostream> #include <string> #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=1010,z=1e9+7,ms63=1061109567;
int Casenum,casei;
int n;
int a[n];
int b[32];
int c[n*32][2],num[n*32],id;
	void ins (int x) {int k=0; for (int i=30;i>=0;i--) {int u= (X>>i) &1;
		if (c[k][u]==0) C[k][u]=++id;
		K=c[k][u];
	++NUM[K];
	} void Add (int x,int v) {int k=0;
		for (int i=30;i>=0;i--) {int u= (x>>i) &1;
		K=c[k][u];
	Num[k]+=v;
	int go (int x) {x=~x;
	int k=0;
	int tmp=0;
		for (int i=30;i>=0;i--) {int u= (x>>i) &1;
			if (Num[c[k][u]]) {tmp+=b[i];
		K=c[k][u];
	else k=c[k][u^1];
return TMP;
	int main () {for (int i=30;i>=0;i--) b[i]=1<<i;
	scanf ("%d", &casenum);
		for (casei=1;casei<=casenum;casei++) {memset (c,0, (id+1) *8);
		Memset (num,0, (id+1) *4);
		Id=0;
		scanf ("%d", &n);
		for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
		int ans=0;
		for (int i=1;i<=n;i++) ins (a[i]);
				for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {Add (a[i],-1);
				Add (a[j],-1);
				Gmax (Ans,go (a[i]+a[j));
				Add (a[i],1);
			Add (a[j],1);
	} printf ("%d\n", ans);
return 0;
}/* "trick&& spit" 1, vigorously a miracle.

2, the dictionary tree is to support the delete operation of Oh 3, the size of the dictionary tree is the total number of characters, do not open a small oh. "I" T (1000) Group data, with a maximum of 10 sets of large data.
For each set of data, give you the number of n ([3,1000]) a[], (0&LT;=A[I]&LT;=1E9).

Let you figure out three numbers a[i],a[j],a[k],i,j,k each other to make (S[i]+s[j]) ^s[k] The right value as large as possible, and output this weight. The "type" Trie "analysis" time limit given to 9s, we consider O (n^3) blunt violence.
And then it just went off.
But a lot of the reason for the violence is that data water. Let's consider some less complex algorithms.

For example, a typical dictionary tree solution.
We can consider enumerating pair (I,J), then enumerating K, and climbing along the dictionary tree.
This time complexity is however O (n^3).

Trying to optimize, we soon discovered that in fact the total number of dictionary trees is only O (c[n,2]) size, we have to enumerate (I,J) each time, because this question is required k!=i and k!=j.
So there's an idea that we start by building a dictionary tree that holds all n numbers, that is, inserting a[into the dictionary tree.
Then enumerate all the pair (I,J), then remove A[i] and a[j from the dictionary tree, which avoids duplication.

Then climb to update the maximum value, then restore A[i] and a[j]. "Time Complexity" O (n^2*32) * *

Besides, this problem can be divided into two parts, you can think for yourself.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.