• 【思维构造】Vampiric Powers, anyone?—CF1847C


    Vampiric Powers, anyone?—CF1847C
    参考文章

    这个思路完美利用了 a i a_i ai 很小的这个特点,通过异或前缀和完美地将循环数组的长度变为循环元素的范围,极快地提升了代码的效率。

    思路

    手推一遍“召唤”的过程可以发现,能召唤出的最大力量即 a a a 数组中连续子串中元素异或和的最大值。用代码表示就是:

    	int res = a[1];
    	for (int l = 1; l <= n; l ++) {
    		int now = 0;
    		for (int r = l; r <= n; r ++) {
    			now ^= a[r];
    			res = max(res, now);
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    当然这样是一定会超时的,我们需要考虑一个更高效的方法。
    观察题目可以发现, a i < 256 a_i < 256 ai<256 这个条件还没有用到,那么可以发现一个新的思路:
    a a a 的前缀异或和数组,数组中任意两个元素异或的最大值即为所求。虽然这样也是两层循环,但每层循环的循环次数最多不过 256 256 256 次,可以通过本题。

    C o d e Code Code

    #include 
    #define int long long
    #define sz(a) ((int)a.size())
    #define all(a) a.begin(), a.end()
    using namespace std;
    using PII = pair<int, int>;
    using i128 = __int128;
    const int N = 1e5 + 10;
    
    int n;
    int a[N];
    
    void solve() {
    	cin >> n;
    	map<int, int> mp;
    	mp[0] = 1;
    	for (int i = 1; i <= n; i ++) {
    		cin >> a[i];
    		a[i] ^= a[i - 1];
    		mp[a[i]] = 1;
    	}
    	
    	int res = -1;
    	for (auto i : mp) {
    		if (i.second) {
    			for (auto j : mp) {
    				if (j.second) {
    					res = max(res, i.first ^ j.first);
    				}
    			}
    		}
    	}
    	
    	cout << "         ";
    	cout << res << "\n";
    }
    
    signed main() {
    	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    	int T = 1;
    	cin >> T; cin.get();
    	while (T --) solve();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    基础算法训练(五)折半插入排序
    L2-007 家庭房产 - java
    产业园区实现产业集聚的三大举措
    Vue中的组件生命周期
    gitlab runner
    如何快速掌握DDT数据驱动测试?
    Flink 中的 Window (窗口)
    JavaScript 基础知识|值的比较
    11.Java面向对象进阶(3)
    java计算机毕业设计景区在线购票系统源码+系统+mysql数据库+lw文档+部署
  • 原文地址:https://blog.csdn.net/suoper2656/article/details/133840124