BZOJ 2460 Element Linear Basis + Greed

https://www.lydsy.com/JudgeOnline/problem.php?id=2460

Legend has it that in ancient times, people had mastered the use of magic on Magic Land in the western continent.
The technology of making crutches by ore refining. At that time, people realized that the magic power of a magic wand depends on the ore used.
Generally speaking, the more ore, the stronger the law, but things will go against each other: sometimes, people in order to obtain stronger power and.
Many ores were used, but magic ores were found to have disappeared during the refining process, making it impossible to refine them.
Out of the magic wand, this phenomenon is known as "magic offset". In particular, if used in the refining process more than
A piece of the same mineral, then there must be "magic offset".
Later, with the improvement of people's cognitive level, this phenomenon has been well explained. After a lot of work
After the experiment, Dmitri, a famous mage, found that if every mineral found now was reasonably braided.
Number (positive integer number, known as the element number of the ore), then, a combination of ores will produce "magic"
If and only if there exists a non-empty subset, the element numbers of those ores are dissimilar or in place.
Zero. (If you don't know what XOR is, see the noun explanation on the next page.) For example, use two
"Magic offset" must occur for the same ore, because the element numbers of the two ores are the same, different or different.
Come to zero.  
And people have an effective way to measure magic, and they already know: the magic of synthetic wands.
It is equal to the sum of the mana of each mineral. Mandatory values of all ores found today have been determined.
The element number of each ore is calculated by experiment.
Now, given the above ore information, please calculate the maximum number of sticks that could be made at that time.
How magical.

Input

The first line contains a positive integer N, representing the number of ore types.
Next N lines, two positive integers Numberi and Magichi in each line, denote the element number of the ore.
And magic value.

Output

Pack only one line, an integer: the maximum magic value

Sample Input

3 1 10 2 20 3 30

Sample Output

50

Hint

Because of the fact that there is "magic offset", each mineral is used at most one piece.

If all three kinds of ores are used, because their element numbers are different or different: 1 xor 2 xor 3 = 0,

There will be magic offset and no magic wand.

It can be found that the best option is to select the latter two minerals, mana 20 + 30 = 50.

For all data: N < 1000, Numberi < 10 ^ 18

,Magici ≤ 10^4

Thought: Greed, sorting the ore according to magic from big to small, traversing from front to back, adding the contribution of the ore if its number can be successfully inserted into the linear basis.

#include<bits/stdc++.h>
#define ll long long
#define MAXN 10005
using namespace std;
int t,n,q;
ll k,tmp;

struct L_B
{
	ll b[65],p[65],base[65];
	int cnt,flag;
	L_B()
	{
		memset(p,0,sizeof(p));
		memset(b,0,sizeof(b));
		cnt=flag=0;
        for(int i=0;i<=62;i++)
            base[i]=1ll<<i;
        }
	inline bool insert(ll x)//Inserting elements into a linear basis
	{
		for(int i=62;i>=0;--i)
			if(x&base[i])
			{
				if(b[i])
					x^=b[i];
				else
				{
					b[i]=x;
					return true;
				}
			}
		flag=1;//Differentiable or get 0
		return false;
	}
	ll get_max()//Finding the Maximum XOR Value
	{
		ll ret=0;
		for(int i=62;i>=0;--i)
			if((ret^b[i])>ret)
				ret^=b[i];
		return ret;
	}
	ll get_min()//Finding the Minimum XOR Value
	{
		if(flag)
			return 0;
		for(int i=0;i<=62;++i)
			if(b[i])
				return b[i];
		return 0;
	}
	inline void rebuild()//Reconstructing the Pre-step of Finding Small k
	{
		for(int i=62;i>=1;--i)
			if(b[i])
				for(int j=i-1;j>=0;--j)
					if(b[i]&base[j])
						b[i]^=b[j];
		for(int i=0;i<=62;++i)
			if(b[i])
				p[cnt++]=b[i];
	}
	ll kth(ll k)//Find the k smallest
	{
		if(flag)
			--k;
		if(k==0)
			return 0;
		ll ret=0;
		if(k>=base[cnt])
			return -1;
		for(int i=0;i<=cnt-1;++i)
			if(k&base[i])
				ret^=p[i];
		return ret;
	}
	void merge(L_B &n2)//The elements in the linear basis n2 are inserted into the linear basis n1 one by one.
    {
        flag|=n2.flag;
        for(int i=0;i<=62;++i)
            if(n2.b[i])
                insert(n2.b[i]);
    }
};

struct node
{
    ll id;
    int magic;
    bool operator <(const node& a)const
    {
        return magic>a.magic;
    }
}a[1005];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lld%d",&a[i].id,&a[i].magic);
    sort(a,a+n);
    int ans=0;
    L_B lis;
    for(int i=0;i<n;i++)
    {
        if(lis.insert(a[i].id))
            ans+=a[i].magic;
    }
    printf("%d\n",ans);
    return 0;
}

 

Keywords: PHP

Added by komquat on Wed, 07 Aug 2019 06:22:57 +0300