日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【每日一题】8月25日题目精讲 XOR-pyramid

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【每日一题】8月25日题目精讲 XOR-pyramid 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 題目描述:
    • 題解:
    • 代碼:

題目描述:


鏈接:https://ac.nowcoder.com/acm/problem/112798
來源:牛客網

輸入描述:

輸出描述:

Print q lines — the answers for the queries.

示例1
輸入
復制

3 8 4 1 2 2 3 1 2

輸出
復制

5 12

示例2
輸入
復制

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][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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。