zoukankan      html  css  js  c++  java
  • [BJWC2011]元素

    嘟嘟嘟


    题中说选的数的编号亦或和不能为0,也就是在这个集合中,不能用不同的选取方案亦或出相同的值。由此联想到线性基的一个性质是,每一个数都能由线性基中特定的一些数亦或得到。
    所以我们就是要求出这些数的线性基,并且满足所选的数的魔力值的和最大。
    本来以为是dp,结果按魔力值排个序贪心就过了。


    证明在网上找了半天,终于找到了一片很不错的题解:传送门.。
    利用的性质就是如果原数组固定,线性基的大小就是固定的。所以我们贪心的插入魔力值大的数。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int maxN = 63;
    inline ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), last = ' ';
    	while(!isdigit(ch)) {last = ch; ch = getchar();}
    	while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
    	if(last == '-') ans = -ans;
    	return ans;
    }
    inline void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    
    int n;
    struct Node
    {
    	ll id; int num;
    	bool operator < (const Node& oth)const
    	{
    		return num > oth.num;
    	}
    }t[maxn];
    
    ll p[maxN];
    In bool insert(ll x)
    {
    	for(int i = maxN; i >= 0; --i) if((x >> i) & 1)
    	{
    		if(p[i]) x ^= p[i];
    		else return p[i] = x;
    	}
    	return 0;
    }
    
    int main()
    {
    	n = read();
    	for(int i = 1; i <= n; ++i) t[i].id = read(), t[i].num = read();
    	sort(t + 1, t + n + 1);
    	int ans = 0;
    	for(int i = 1; i <= n; ++i) if(insert(t[i].id)) ans += t[i].num;
    	write(ans), enter;
    	return 0;
    }
    
  • 相关阅读:
    一个JAVA题引发的思考
    eclipse好玩的插件集(一) CKEditor插件
    Log4J使用实例---日志进行邮件发送或是存入数据库
    log4j输出到数据库(输出自定义参数、分级保存)
    String和StringBuffer的一点研究
    String、StringBuffer、StringBuilder区分和性能比较
    最新eclipse安装SVN插件
    jsoup select 选择器
    网页导出excel文件
    Dom4j完整教程
  • 原文地址:https://www.cnblogs.com/mrclr/p/10255659.html
Copyright © 2011-2022 走看看