CodeForces - 983B XOR-pyramid(区间dp,异或)
2 seconds
512 megabytes
standard input
standard output
For an array bb of length mm we define the function ff as
f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,
where ⊕⊕ is bitwise exclusive OR.
For example, f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15
You are given an array aa and a few queries. Each query is represented as two integers ll and rr. The answer is the maximum value of ff on all continuous subsegments of the array al,al+1,…,aral,al+1,…,ar.
The first line contains a single integer nn (1≤n≤50001≤n≤5000) — the length of aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1) — the elements of the array.
The third line contains a single integer qq (1≤q≤1000001≤q≤100000) — the number of queries.
Each of the next qq lines contains a query represented as two integers ll, rr (1≤l≤r≤n1≤l≤r≤n).
Print qq lines — the answers for the queries.
3
8 4 1
2
2 3
1 2
5
12
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
60
30
12
3
In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.
In second sample, optimal segment for first query are [3,6][3,6], for second query — [2,5][2,5], for third — [3,4][3,4], for fourth — [1,2][1,2].
给n个数,询问q次,每次询问给出l,r. [l,r]区间求异或最大值为多少。一开始没看清是最大值,还以为题目错了。
区间【1,6】和区间【2,5】比较一下就知道很多会重复,所以把它们记下来节省时间。
此题需要记忆化两次。区间动态规划。
我用b数组来存储所以异或的值,dp数来存储最大值。
拿第二个样例:
b数组这样得来:
b[1]这一排还是a数组
b[i][j]=b[i-1][j]^b[i-1][j+1];
dp数组这样的来:
dp[0]这一排还是a数组
dp[i][j]=max( dp[i-1][j], dp[i-1][j+1],b[i][j] );
这是dp数组
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define maxn 110
#define maxm 10010
#define inf 0x3f3f3f
using namespace std;
int b[][];
int dp[][];
int a[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dp[][i]=a[i];
}
for(int i=;i<=n-;i++)
{
b[][i]=a[i]^a[i+];
dp[][i]=max(a[i],a[i+]);//第一排也是要比较得到dp[1][i],否则第三个样例wa
dp[][i]=max(b[][i],dp[][i]);
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
b[i][j]=b[i-][j]^b[i-][j+];
}
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
dp[i][j]=max(dp[i-][j],dp[i-][j+]);
dp[i][j]=max(dp[i][j],b[i][j]);
}
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
printf("%4d",dp[i][j]);
}
cout<<endl;
}
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[r-l][l]);
}
return ;
}
CodeForces - 983B XOR-pyramid(区间dp,异或)的更多相关文章
- Codeforces 983B. XOR-pyramid【区间DP】
LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...
- CF 983B XOR-pyramid(区间dp,异或)
CF 983B XOR-pyramid(区间dp,异或) 若有一个长度为m的数组b,定义函数f为: \(f(b) = \begin{cases} b[1] & \quad \text{if } ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
- Codeforces.392E.Deleting Substrings(区间DP)
题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...
- CodeForces - 1025D: Recovering BST (区间DP)
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...
- Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)
题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...
- Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...
- CodeForces 149D Coloring Brackets 区间DP
http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...
- 51Nod XOR key —— 区间最大异或值 可持久化字典树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key 题目来源: HackerRa ...
随机推荐
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- UVa 10891 Sum游戏
https://vjudge.net/problem/UVA-10891 题意: 有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取.每次玩家只能从左端或者右端取任意数量个数,但不能两端都取. ...
- django urlencode
from django.utils.http import urlquote a = urlquote('分享') print(a)
- Tensorflow一些常用基本概念与函数(三)
摘要:本系列主要对tf的一些常用概念与方法进行描述.本文主要针对tensorflow的数据IO.图的运行等相关函数进行讲解.为‘Tensorflow一些常用基本概念与函数’系列之三. 1.序言 本文所 ...
- jquery.validate验证表单
添加引用 <script src="/${appName}/commons/js/validate/jquery.validate.min.js"></scrip ...
- 也来说说C#异步委托 (转自 Rising_Sun)
前些日子,看到园子里面有人用老王喝茶的例子讲解了一下同步和异步,虽然没有代码实现,但是能够通俗易懂的讲解了同步.异步.阻塞.非阻塞的关系了,今天借题发挥,用一个热水器加热洗澡的例子来具体演示一下C#使 ...
- 046——VUE中组件之使用动态组件灵活设置页面布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 201621123005《Java程序设计》第十次实验总结
201621123005<Java程序设计>第十周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 1. 常用异常 1.1 自己 ...
- EM算法及其应用: K-means 与 高斯混合模型
EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 上一篇阐述了EM算法的主要原理,这一篇来看其两大应用 -- K-means 与 高斯混合模型,主要由EM算法的观点出 ...
- Alpha发布
作业链接[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283] 视频展示 链接[https://v.youku.com/v_show/ ...