Codeforces Round #483 (Div. 2) D. XOR-pyramid dp的应用

input:

6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2

output:

60
30
12
3

刚开始没有看到要取最大值,就一直在哪里找哪里错了,仔细一看原来有一个最大的需要。

思路:用一个数组存好每两个数之间的^数值,然后比较^的数、以及推出^的两个数,在三个数中找最大值,然后存进数组中。

单纯的dp。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=5006;
 5 int dp[maxn][maxn];
 6 int n;
 7 
 8 int main()
 9 {
10     ios_base::sync_with_stdio(0); cin.tie(0);
11     cout.tie(0);
12     while( cin >> n){
13         for(int i=1;i<=n;i++){
14             cin >> dp[1][i];
15         }
16         for(int i=2;i<=n;i++)
17             for(int j=1;j<=n-i+1;j++)
18                 dp[i][j]=dp[i-1][j]^dp[i-1][j+1];
19 
20         for(int i=2;i<=n;i++){
21             for(int j=1;j<=n-i+1;j++)
22                 dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-1][j+1]));
23         }
24 
25         int kk;
26         cin >> kk;
27         while(kk--){
28             int x,y;
29             cin >> x >> y;
30             int mid=y-x+1;
31             cout << dp[mid][x] << endl;
32         }
33     }
34 
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/ZQUACM-875180305/p/9047413.html