Loading [MathJax]/jax/output/HTML-CSS/jax.js

【SRM】649 t2

题意

一个数列A,数的范围均在[0,2N1]内,求一个B,使得新生成的数列C中逆序对最多(Ci=AixorB),输出最多的逆序对。(|A|<=105

分析

这种题当然要逐位考虑..考虑到二进制和xor,我们需要想到trie...

题解

将数列插入到一棵trie,我们在每一个层记录一个信息,表示B在这一层取0或取1新增的逆序对数,然后统计答案即可。
而由于是xor操作,所以很好统计,我们可以每插入一个数就统计一次。

// BEGIN CUT HERE

// END CUT HERE
#line 5 "XorSequence.cpp"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N=150005;
struct node {
	node *c[2];
	int s;
	void init() {
		c[0]=c[1]=0;
		s=0;
	}
}Po[N*31], *iT=Po, *root=0;
ll c[31][2];
node *newnode() {
	iT->init();
	return iT++;
}
void add(int w, int dep=29, node *&x=root) {
	if(!x) {
		x=newnode();
	}
	++x->s;
	if(dep<0) {
		return;
	}
	int f=(w>>dep)&1;
	if(f) {
		add(w, dep-1, x->c[1]);
		if(x->c[0]) {
			c[dep][0]+=x->c[0]->s;
		}
	}
	else {
		add(w, dep-1, x->c[0]);
		if(x->c[1]) {
			c[dep][1]+=x->c[1]->s;
		}
	}
}
class XorSequence {
public:
	long long getmax(int N, int sz, int A0, int A1, int P, int Q, int R) {
		iT=Po;
		root=0;
		memset(c, 0, sizeof c);
		add(A0);
		for(int i=1; i<sz; ++i) {
			add(A1);
			int t=A1;
			A1=(1ll*A0*P+1ll*A1*Q+R)%N;
			A0=t;
		}
		ll ans=0;
		for(int i=0; i<30; ++i) {
			ans+=max(c[i][0], c[i][1]);
		}
		return ans;
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arg0 = 4; int Arg1 = 6; int Arg2 = 3; int Arg3 = 2; int Arg4 = 0; int Arg5 = 1; int Arg6 = 3; long long Arg7 = 8LL; verify_case(0, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }
	void test_case_1() { int Arg0 = 8; int Arg1 = 8; int Arg2 = 2; int Arg3 = 5; int Arg4 = 3; int Arg5 = 1; int Arg6 = 4; long long Arg7 = 13LL; verify_case(1, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }
	void test_case_2() { int Arg0 = 8; int Arg1 = 7; int Arg2 = 3; int Arg3 = 0; int Arg4 = 1; int Arg5 = 2; int Arg6 = 4; long long Arg7 = 12LL; verify_case(2, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }
	void test_case_3() { int Arg0 = 32; int Arg1 = 15; int Arg2 = 7; int Arg3 = 9; int Arg4 = 11; int Arg5 = 2; int Arg6 = 1; long long Arg7 = 60LL; verify_case(3, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }
	void test_case_4() { int Arg0 = 131072; int Arg1 = 131072; int Arg2 = 7; int Arg3 = 7; int Arg4 = 1; int Arg5 = 0; int Arg6 = 0; long long Arg7 = 0LL; verify_case(4, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }
	void test_case_5() { int Arg0 = 131072; int Arg1 = 131070; int Arg2 = 411; int Arg3 = 415; int Arg4 = 398; int Arg5 = 463; int Arg6 = 9191; long long Arg7 = 4302679760LL; verify_case(5, Arg7, getmax(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main() {
	XorSequence ___test;
	___test.run_test(-1);
	return 0;
}
// END CUT HERE
posted @   iwtwiioi  阅读(542)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2014-11-22 高精度模板2(带符号压位加减乘除开方封包)
2014-11-22 【BZOJ】1004: [HNOI2008]Cards(置换群+polya+burnside)
2014-11-22 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)
点击右上角即可分享
微信分享提示