AtCoder ABC 128D equeue

题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_d

题目大意

  有一个双端队列,里面有 N 个整数,你可以进行如下4种操作:

  1. A:从队头那一个到手里。
  2. B:从队尾拿一个到手里。
  3. C:把手中任意一个数放到队头(这个操作等价于扔掉,我们完全可以执行完全部的AB,在执行CD)。
  4. D:把手中任意一个数放到队尾。

  先给定 K,要求操作数量不大于 K 次,求你手中所有数和能达到的最大值。

分析

  区间DP,见注释。

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20  
 21 #define ms0(a) memset(a,0,sizeof(a))
 22 #define msI(a) memset(a,inf,sizeof(a))
 23 #define msM(a) memset(a,-1,sizeof(a))
 24 
 25 #define MP make_pair
 26 #define PB push_back
 27 #define ft first
 28 #define sd second
 29  
 30 template<typename T1, typename T2>
 31 istream &operator>>(istream &in, pair<T1, T2> &p) {
 32     in >> p.first >> p.second;
 33     return in;
 34 }
 35  
 36 template<typename T>
 37 istream &operator>>(istream &in, vector<T> &v) {
 38     for (auto &x: v)
 39         in >> x;
 40     return in;
 41 }
 42  
 43 template<typename T1, typename T2>
 44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 45     out << "[" << p.first << ", " << p.second << "]" << "
";
 46     return out;
 47 }
 48 
 49 inline int gc(){
 50     static const int BUF = 1e7;
 51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 52     
 53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 54     return *bg++;
 55 } 
 56 
 57 inline int ri(){
 58     int x = 0, f = 1, c = gc();
 59     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 61     return x*f;
 62 }
 63  
 64 typedef long long LL;
 65 typedef unsigned long long uLL;
 66 typedef pair< double, double > PDD;
 67 typedef pair< int, int > PII;
 68 typedef pair< string, int > PSI;
 69 typedef set< int > SI;
 70 typedef vector< int > VI;
 71 typedef vector< PII > VPII;
 72 typedef map< int, int > MII;
 73 typedef pair< LL, LL > PLL;
 74 typedef vector< LL > VL;
 75 typedef vector< VL > VVL;
 76 typedef priority_queue< int > PQIMax;
 77 typedef priority_queue< int, VI, greater< int > > PQIMin;
 78 const double EPS = 1e-10;
 79 const LL inf = 0x7fffffff;
 80 const LL infLL = 0x7fffffffffffffffLL;
 81 const LL mod = 1e9 + 7;
 82 const int maxN = 2e5 + 7;
 83 const LL ONE = 1;
 84 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 85 const LL oddBits = 0x5555555555555555;
 86 
 87 
 88 int N, K, V[57];
 89 // dp[L][R][x][k]表示从在区间[L, R]中,在 x 边进行操作(1表示右边,0表示左边),进行 k 次操作所能取到的最大值 
 90 int dp[57][57][2][107];
 91 
 92 // 记忆化搜索过不了,不过留着吧,dp是从记忆化搜索改出来的 
 93 inline int dfs(int L, int R, int x, int k) {
 94     if(L > R || k <= 0) return 0;
 95     if(dp[L][R][x][k]) return dp[L][R][x][k];
 96     int ret = 0;
 97     if(x == 0) {// 在左边操作 
 98         //
 99         ret = max(ret, V[L] + dfs(L + 1, R, 0, k - 1));
100         ret = max(ret, V[L] + dfs(L + 1, R, 1, k - 1));
101         if(V[L] < 0) {
102             // 扔掉(耗费两次操作) 
103             ret = max(ret, dfs(L + 1, R, 0, k - 2));
104             ret = max(ret, dfs(L + 1, R, 1, k - 2));
105         }
106     }
107     else {// 在右边操作 
108         //
109         ret = max(ret, V[R] + dfs(L, R - 1, 0, k - 1));
110         ret = max(ret, V[R] + dfs(L, R - 1, 1, k - 1));
111         if(V[R] < 0) {
112             // 扔掉(耗费两次操作) 
113             ret = max(ret, dfs(L, R - 1, 0, k - 2));
114             ret = max(ret, dfs(L, R - 1, 1, k - 2));
115         }
116     }
117     dp[L][R][x][k] = ret;
118     return ret;
119 }
120 
121 int main(){
122     INIT(); 
123     cin >> N >> K;
124     For(i, 1, N) cin >> V[i];
125 
126     //cout << max(dfs(1, N, 0, K), dfs(1, N, 1, K)) << endl;
127     
128     For(k, 1, K) {
129         For(L, 1, N) {
130             For(R, L, N) {
131                 if(k != 1)dp[L][R][0][k] = max(dp[L + 1][R][0][k - 2], dp[L + 1][R][1][k - 2]);
132                 dp[L][R][0][k] = max(dp[L][R][0][k], V[L] + dp[L + 1][R][0][k - 1]);
133                 dp[L][R][0][k] = max(dp[L][R][0][k], V[L] + dp[L + 1][R][1][k - 1]);
134                 
135                 if(k != 1)dp[L][R][1][k] = max(dp[L][R - 1][0][k - 2], dp[L][R - 1][1][k - 2]);
136                 dp[L][R][1][k] = max(dp[L][R][1][k], V[R] + dp[L][R - 1][0][k - 1]);
137                 dp[L][R][1][k] = max(dp[L][R][1][k], V[R] + dp[L][R - 1][1][k - 1]);
138             }
139         }
140     }
141     cout << max(dp[1][N][0][K], dp[1][N][1][K]) << endl;
142     return 0;
143 }
View Code
原文地址:https://www.cnblogs.com/zaq19970105/p/10943902.html