【每日一题】8月25日题目精讲 XOR-pyramid
生活随笔
收集整理的這篇文章主要介紹了
【每日一题】8月25日题目精讲 XOR-pyramid
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 題目描述:
- 題解:
- 代碼:
題目描述:
鏈接:https://ac.nowcoder.com/acm/problem/112798
來源:牛客網
輸入描述:
輸出描述:
Print q lines — the answers for the queries.
示例1
輸入
復制
輸出
復制
示例2
輸入
復制
輸出
復制
備注:
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][3,6]$, for
second query — $[2,5][2,5][2,5]$, for third — $[3,4][3,4][3,4]$, for fourth —
$[1,2][1,2][1,2]$.
題解:
我們要先摸索出f的規律
f(i,j)=f(i,j-1) xor f(i+1,j)
dp[l][r]為區間[l,r]的最大值
dp[l][r]=max(f(l,r),dp[l][r?1],dp[l+1][r])
代碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=5030; ll a[maxn]; ll dp[maxn][maxn]; ll res[maxn][maxn]; int main() {int n;cin>>n;for(int i=1;i<=n;i++){cin>>a[i];res[i][i]=dp[i][i]=a[i];}int q;cin>>q;for (int len = 2; len <= n; len++) {for (int l = 1; l + len - 1 <= n; l++) {int r = l + len - 1;dp[l][r] = dp[l + 1][r] ^ dp[l][r - 1];res[l][r] = max({dp[l][r], res[l + 1][r], res[l][r - 1]});}}for(int i=1;i<=q;i++){int l,r;cin>>l>>r;printf("%d\n",res[l][r]);}return 0; } /* 1 2 4 8 f(1,4)=f(1,3) xor f(2,4) f(1,3)=f(1,2) xor f(2,3) 1 2 4 f(1 xor 2, 2 xor 4) f(1,2)=f(1,1) xor f(2,2) */ //f(i,j) = f(i, j-1) xor f(i+1,j)總結
以上是生活随笔為你收集整理的【每日一题】8月25日题目精讲 XOR-pyramid的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【每日一题】8月17日题目精讲-[SCO
- 下一篇: 【每日一题】8月28日题目精讲 编号