Hdu-5845 Best Division (trie+DP)

解决一个算法问题,通过给定的整数序列,确定能够将其分割成的最大区间数量,每个区间长度不超过限定值且异或和不超过指定阈值。利用前缀异或和与字典树实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

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)
 

Output
For each test case, you should print a single line containing the answer.

If the answer does not exist, just print 0.
 

Sample Input
2 3 1 2 1 1 1 3 0 3 1 1 1
 

Sample Output
2 1
 

题意:给你n个数字的序列,问你最多能把序列分成多少份,每份长度不能超过l,且异或和不能超过x。

分析:维护前缀异或和,构造trie,节点保留这个子树下的最大值,每次插入和删除都要更新,然后每次去字典树里查最大值更新即可。

PS:比赛时出现了逻辑错误,最后也没改出来,”i-l-1 >= 0"时要删除一个前缀,但是没有判断之前这个前缀是否在trie树中,WA了6次。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>  
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 268435456
#define MAX 32*100002  
using namespace std;
typedef long long ll;
int n,x,l,T,a[100002],f[100002];
ll b[100002],p,q;
struct trie  
{  
    int ch[MAX][2],tot;  
    int val[MAX],dp[MAX],fa[MAX];  
    void build()  
    {  
        memset(ch,0,sizeof(ch));
    	memset(val,0,sizeof(val));
    	memset(dp,-1,sizeof(dp));
    	memset(fa,0,sizeof(fa));
        tot = 1;  
    }  
    void insert(int s,int Val,int dpval)  
    {  
        int u = 0;  
        for(int i = 30;i >= 0;i--)  
        {  
            int v = ((1<<i) & s) ? 1 : 0;  
            if(!ch[u][v]) 
			{        
				fa[tot] = u;
				ch[u][v] = tot++;
			}
            u = ch[u][v];  
            val[u] += Val;  
            dp[u] = max(dp[u],dpval);
        }
		if(!val[u])
		{
			dp[u] = -1;
			while(u)
			{
				u = fa[u];
				dp[u] = -1;
				if(ch[u][0] && val[ch[u][0]]) dp[u] = dp[ch[u][0]];
				if(ch[u][1] && val[ch[u][1]]) dp[u] = max(dp[u],dp[ch[u][1]]);	
			} 			
		}  
    }  
    int find(int s)  
    {  
		int u = 0,ans = -1;
		bool flag = false;
		for(int i = 30;i >= 0;i--)
		{
			int v = ((1<<i) & s) ? 1 : 0;  
			int V = ((1<<i) & x) ? 1 : 0;
			if(V && ch[u][v] && val[ch[u][v]]) ans = max(ans,dp[ch[u][v]]);
			u = ch[u][V ^ v];
			if(!u || !val[u]) break;
			if(i == 0) flag = true;
		}
		if(flag) ans = max(ans,dp[u]);
		return ans;
    }  
} tree;  
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		tree.build(); 
		scanf("%d%d%d",&n,&x,&l);
		scanf("%I64d%I64d%I64d",&b[1],&p,&q);
		a[1] = b[1];
		for(int i = 2;i <= n;i++) 
		{
			b[i] = (b[i-1]*p + q) % MOD;
			a[i] = a[i-1] ^ b[i];
		}
		tree.insert(0,1,0);
		for(int i = 1;i <= n;i++)
		{
			if(i-l-1 >= 0 && f[i-l-1]) tree.insert(a[i-l-1],-1,-1);
			int now = tree.find(a[i]) + 1;
			if(now) tree.insert(a[i],1,now);
			f[i] = now;
			if(i == n) printf("%d\n",now);
		}
	}
 } 


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值