2016多校训练Contest9: 1002 Best Division hdu5845
来源:互联网 发布:sip电话软件 编辑:程序博客网 时间:2024/05/16 07:52
Problem Description
You are given an array A, consisting of N integers.
You are also given 2 integers K and L.
You must divide the whole array A into exactly K nonempty intervals, such that the length of each interval is not greater than L.
The cost of an interval [S, E] is the bitwise XOR sum of all elements of A whose indices are in [S, E].
The score of a division is simply the maximum cost of K intervals in the division. You are interested in the best division, which minimizes the score of the division. Since this is too simple for you, the problem is reversed.
You know the minimum score: the answer for the original problem is not greater than X. Now you want to know, the maximum value of K.
You are also given 2 integers K and L.
You must divide the whole array A into exactly K nonempty intervals, such that the length of each interval is not greater than L.
The cost of an interval [S, E] is the bitwise XOR sum of all elements of A whose indices are in [S, E].
The score of a division is simply the maximum cost of K intervals in the division. You are interested in the best division, which minimizes the score of the division. Since this is too simple for you, the problem is reversed.
You know the minimum score: the answer for the original problem is not greater than X. Now you want to know, the maximum value of K.
Input
There are several test cases.
The first line of the input contains an integer T (1<=T<=20), the number of test cases. Then T test cases follow.
Each test case starts with 3 integers N, X, L (1<= L<=N<=100000, 0<=X<268435456), which are described above.
The next line contains 3 integers A[1], P, Q. All other integers of the array A are generated from these 3 integers in the following rule:
For every integer 1<k<=N, A[k] = (A[k-1]*P+Q) mod 268435456.
(0 <= A[1], P, Q < 268435456)
The first line of the input contains an integer T (1<=T<=20), the number of test cases. Then T test cases follow.
Each test case starts with 3 integers N, X, L (1<= L<=N<=100000, 0<=X<268435456), which are described above.
The next line contains 3 integers A[1], P, Q. All other integers of the array A are generated from these 3 integers in the following rule:
For every integer 1<k<=N, A[k] = (A[k-1]*P+Q) mod 268435456.
(0 <= A[1], P, Q < 268435456)
Output
For each test case, you should print a single line containing the answer.
If the answer does not exist, just print 0.
If the answer does not exist, just print 0.
Sample Input
23 1 21 1 13 0 31 1 1
Sample Output
21
首先朴素转移,f[i]表示前i个数可以分成的满足条件的最大段数
f[i]=max(f[j]+1),a[j]^a[j+1]^……^a[i]<=x,i-l+1>=j
然后观察满足转移的条件,令s[i]为i的前缀异或和
那么上式即为s[i]^s[j]<=x,i-l+1>=j
考虑用trie维护所有的s[j]
那么转移可以直接从trie上查找最大的f[i]
每次加入s[i]后删除s[i-l来]维护trie的值即可
因为如果s[i]相同,后面的f[i]一定不小于前一次出现的f[i]
因此维护某个s[i]出现次数,每次用后面的f[i]覆盖前面的,减成0的时候清空trie的值即可
#include<map>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;long long mod=268435456;struct tree{int ll,rr;int s,x;}tr[3000001];int tot;int a[40],b[40];int f[100001],px[100001];inline void cale(int xx){memset(a,0,sizeof(a));int d=0;while(xx>0){d++;a[d]=xx%2;xx=xx/2;}}inline void up(int p){tr[p].x=max(tr[tr[p].ll].x,tr[tr[p].rr].x);}inline void add(int p,int d,int xx){if(d==0)return ;tr[p].s++;if(a[d]==0){if(tr[p].ll!=0)add(tr[p].ll,d-1,xx);else{tot++;tr[p].ll=tot;add(tr[p].ll,d-1,xx);}}else{if(tr[p].rr!=0)add(tr[p].rr,d-1,xx);else{tot++;tr[p].rr=tot;add(tr[p].rr,d-1,xx);}}if(d==1)tr[p].x=xx;elseup(p);}inline void dec(int p,int d){if(d==1)return ;tr[p].s--;if(tr[p].s==0)tr[p].x=0;if(a[d]==0)dec(tr[p].ll,d-1);elsedec(tr[p].rr,d-1);up(p);}inline int ask(int p,int d){if(d==1||p==0)return tr[p].x;if(a[d]==0){if(b[d]==0)return ask(tr[p].ll,d-1);elsereturn max(ask(tr[p].rr,d-1),tr[tr[p].ll].x);}else{if(b[d]==0)return ask(tr[p].rr,d-1);elsereturn max(ask(tr[p].ll,d-1),tr[tr[p].rr].x);}}int main(){//freopen("1002.in","r",stdin);//freopen("1002.ans","w",stdout);int T;scanf("%d",&T);while(T>0){T--;tot=1;memset(tr,0,sizeof(tr));int n,x,l;scanf("%d%d%d",&n,&x,&l);long long p,q;scanf("%d%I64d%I64d",&px[1],&p,&q);int i;for(i=2;i<=n;i++)px[i]=((long long)px[i-1]*p+q)%mod;for(i=2;i<=n;i++)px[i]=(px[i]^px[i-1]);memset(f,0,sizeof(f));f[0]=0;cale(x);memset(b,0,sizeof(b));for(i=1;i<=35;i++)b[i]=a[i];for(i=1;i<=n;i++){cale(px[i]);int xt=ask(1,30);if(xt!=0)f[i]=xt+1;else if(px[i]<=x)f[i]=1;if(f[i]!=0)add(1,30,f[i]);if(i>l&&f[i-l]!=0){cale(px[i-l]);dec(1,30);}}printf("%d\n",f[n]);}return 0;}
0 0
- 2016多校训练Contest9: 1002 Best Division hdu5845
- 2016多校训练Contest9: 1012 Less Time, More profit hdu5855
- hdu5845 Best Division (xor-trie+dp)
- 多校联合训练hdu5845---Best Divison
- 2016多校训练总结
- 2016多校训练Contest6: 1002 A Simple Chess hdu5794
- 2016多校训练#1 1002 组合博弈
- 2016多校训练Contest7: 1002 Hearthstone hdu5810
- 2016多校训练Contest10: 1002 Hard problem hdu5858
- #2016多校训练Contest10: 1002 Hard problem hdu5858
- 2016湖南多校训练第一场
- 2016多校训练1 hdu5733 tetrahedron
- 多校训练
- Contest - 多校训练(三
- Contest - 多校训练(三
- ZZULIOJ 多校训练三
- 多校训练(三)
- 2016多校训练Contest3:1001 1002 1003 1007 1010 1011
- 2016多校训练Contest9: 1012 Less Time, More profit hdu5855
- Fragment嵌套首页初次加载不出界面问题解决
- 58. magento quote lifetime
- VS2013打开改变路径之后的解决方案(含opencv以及属性表)
- Tomcat源码阅读系列
- 2016多校训练Contest9: 1002 Best Division hdu5845
- Android逆向分析【未完】
- 59. magento quote
- EasyPusher手机直播编码推送之图像旋转90度后画面重复的问题
- java线程状态切换
- 《剑指offer》编程题Java源码解析(2)
- Python 文件操作
- C语言学习总结(1)-递归函数的理解
- 判断当前启动模式是EFI还是BIOS