983B XOR-pyramid(區間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[5005][5005]; int dp[5005][5005]; int a[5005]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); dp[0][i]=a[i]; } for(int i=1;i<=n-1;i++) { b[1][i]=a[i]^a[i+1]; dp[1][i]=max(a[i],a[i+1]);//第一排也是要比較得到dp[1][i],否則第三個樣例wadp[1][i]=max(b[1][i],dp[1][i]); } for(int i=2;i<=n-1;i++) { for(int j=1;j<=n-i;j++) { b[i][j]=b[i-1][j]^b[i-1][j+1]; } } for(int i=2;i<=n-1;i++) { for(int j=1;j<=n-i;j++) { dp[i][j]=max(dp[i-1][j],dp[i-1][j+1]); dp[i][j]=max(dp[i][j],b[i][j]); } } for(int i=0;i<=n-1;i++) { for(int j=1;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 0; }
相關推薦
983B XOR-pyramid(區間dp,異或)
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include&l
POJ2955 Brackets(區間DP,括號匹配)
POJ2955 首先考慮怎麼樣定義dp讓它滿足具有通過子結構來求解、 定義dp [ i ] [ j ] 為串中第 i 個到第 j 個括號的最大匹配數目 那麼我們假如知道了 i 到 j 區間的最大匹
【LightOJ - 1031】Easy Game (區間dp,博弈)
題幹: You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chan
POJ 1191 棋盤分割 (區間DP,記憶化搜索)
bool for ring def bsp sca printf namespace http 題面 思路:分析公式,我們可以發現平均值那一項和我們怎麽分的具體方案無關,影響答案的是每個矩陣的矩陣和的平方,由於數據很小,我們可以預處理出每個矩陣的和的平方,執行狀態轉移。 設
Codeforces Round #538 (Div. 2)D(區間DP,思維)
name 區間 force spa ORC str ont using mil #include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){ in
Palindromic characteristics CodeForces - 835D (區間DP,預處理回文串問題)
false back == ostream seq push_back else make () Palindromic characteristics of string s with length |s| is a sequence of |s|integers, wh
CodeForces - 55D(數位dp,離散化)
題目來源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb
洛谷4366——最短路(dijkstra,思維,異或)
tor 分享 fontsize tdi font %d mar fin space 題目大意 給定一個n個點,m條邊的圖,每條邊有邊權,而每個點\(i\)也可以直接到達\(j\),代價是\(i\ xor\ j\),給定一個S和T,求S到T的最小代價 其中\(n\le1000
HDU 1698 Just a Hook 線段樹(區間更新,詢問總值)
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sti
HDU 3943 —— K-th Nya Number(數位DP,二分答案)
給定X和Y的值,一個數字用十進位制表示,對於數位4出現恰好X次,7恰好Y次的數字稱為nya數。 然後給P和Q, 後面是一系列查詢, 每個查詢K就是在區間(P,Q]上找到第K個nya數,不存在則輸出Nya!(= = 這是尖叫的意思麼) 因為數字的範圍比較大,又是跟數位有關,
POJ 3190 Stall Reservations-奶牛分欄(區間貪心,優先佇列)
題目大意:每一隻奶牛要求在時間區間[A,B]內獨享一個牛欄。問最少需要多少個牛欄。 貪心策略是優先滿足A最小的奶牛,維持一個牛欄B最小堆,將新來的奶牛塞進B最小的牛欄裡。 <p><span style="color: rgb(51, 51, 51);
Brackets(區間dp之括號匹配)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence,if s is a regular brackets
石子歸併(區間dp & 四邊形不等式優化)
基準時間限制:1 秒 空間限制:131072 KB 分值: 20 難度:3級演算法題 收藏 取消關注 N堆石子擺成一條線。現要將石子有次序地合併成一堆。規定每次只能
hdoj1561The more, The Better(樹形dp,依賴揹包)
題意:ACboy很喜歡玩一種戰略遊戲,在一個地圖上,有N座城堡,每座城堡都有一定的寶物,在每次遊戲中ACboy允許攻克M個城堡並獲得裡面的寶物。但由於地理位置原因,有些城堡不能直接攻克,要攻克這些
AtCoder Grand Contest 011D(思維,規律,異或)
style continue rand grand nbsp bsp esp () 一位 #include<bits/stdc++.h>using namespace std;char s[200007];int ans[200007];int main(){
【AtCoder - 4244 】AtCoder Express 2 (區間dp 或 暴力列舉,思維)
題幹: In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from we
區間dp(整數劃分,石子劃分)
整數劃分(四) 基礎區間dp,程式碼: #include <cstdio> #include <cstring> long long a[20][20]; long lon
LightOJ - 1422 Halloween Costumes (區間DP)
wan things strong cas book article printf ase con Description Gappu has a very busy weekend ahead of him. Because, next weekend is Ha
POJ 1141 Brackets Sequence (區間DP)
ive bsp rip mes character har typedef som memset Description Let us define a regular brackets sequence in the following way: 1.
Brackets Sequence POJ - 1141 (區間dp)
gif == urn ++ char img ems utc pre Brackets Sequence POJ - 1141 題意:給一個括號序列,問最少添加多少個括號似的原序列匹配,並輸出新序列。 用dp[i][j]表示i到j最少添加幾個括號,flag[i][j]表