AtCoder ABC 128D equeue
题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_d
题目大意
有一个双端队列,里面有 N 个整数,你可以进行如下4种操作:
- A:从队头那一个到手里。
- B:从队尾拿一个到手里。
- C:把手中任意一个数放到队头(这个操作等价于扔掉,我们完全可以执行完全部的AB,在执行CD)。
- D:把手中任意一个数放到队尾。
先给定 K,要求操作数量不大于 K 次,求你手中所有数和能达到的最大值。
分析
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, K, V[];
// dp[L][R][x][k]表示从在区间[L, R]中,在 x 边进行操作(1表示右边,0表示左边),进行 k 次操作所能取到的最大值
int dp[][][][]; // 记忆化搜索过不了,不过留着吧,dp是从记忆化搜索改出来的
inline int dfs(int L, int R, int x, int k) {
if(L > R || k <= ) return ;
if(dp[L][R][x][k]) return dp[L][R][x][k];
int ret = ;
if(x == ) {// 在左边操作
// 拿
ret = max(ret, V[L] + dfs(L + , R, , k - ));
ret = max(ret, V[L] + dfs(L + , R, , k - ));
if(V[L] < ) {
// 扔掉(耗费两次操作)
ret = max(ret, dfs(L + , R, , k - ));
ret = max(ret, dfs(L + , R, , k - ));
}
}
else {// 在右边操作
// 拿
ret = max(ret, V[R] + dfs(L, R - , , k - ));
ret = max(ret, V[R] + dfs(L, R - , , k - ));
if(V[R] < ) {
// 扔掉(耗费两次操作)
ret = max(ret, dfs(L, R - , , k - ));
ret = max(ret, dfs(L, R - , , k - ));
}
}
dp[L][R][x][k] = ret;
return ret;
} int main(){
INIT();
cin >> N >> K;
For(i, , N) cin >> V[i]; //cout << max(dfs(1, N, 0, K), dfs(1, N, 1, K)) << endl; For(k, , K) {
For(L, , N) {
For(R, L, N) {
if(k != )dp[L][R][][k] = max(dp[L + ][R][][k - ], dp[L + ][R][][k - ]);
dp[L][R][][k] = max(dp[L][R][][k], V[L] + dp[L + ][R][][k - ]);
dp[L][R][][k] = max(dp[L][R][][k], V[L] + dp[L + ][R][][k - ]); if(k != )dp[L][R][][k] = max(dp[L][R - ][][k - ], dp[L][R - ][][k - ]);
dp[L][R][][k] = max(dp[L][R][][k], V[R] + dp[L][R - ][][k - ]);
dp[L][R][][k] = max(dp[L][R][][k], V[R] + dp[L][R - ][][k - ]);
}
}
}
cout << max(dp[][N][][K], dp[][N][][K]) << endl;
return ;
}
AtCoder ABC 128D equeue的更多相关文章
- ATCODER ABC 099
ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...
- Atcoder ABC 141
Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...
- Atcoder ABC 139E
Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...
- Atcoder ABC 139D
Atcoder ABC 139D 解法: 等差数列求和公式,记得开 $ long long $ CODE: #include<iostream> #include<cstdio> ...
- Atcoder ABC 139C
Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...
- Atcoder ABC 139B
Atcoder ABC 139B 题意: 一开始有1个插口,你的插排有 $ a $ 个插口,你需要 $ b $ 个插口,问你最少需要多少个插排. 解法: 暴力模拟. CODE: #include< ...
- Atcoder ABC 139A
Atcoder ABC 139A 题意: 给你两个字符串,记录对应位置字符相同的个数 $ (n=3) $ 解法: 暴力枚举. CODE: #include<iostream> #inclu ...
- atcoder abc 244
atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...
- AtCoder ABC 250 总结
AtCoder ABC 250 总结 总体 连续若干次一样的结果:30min 切前 4 题,剩下卡在 T5 这几次卡在 T5 都是一次比一次接近, 什么 dp 前缀和打挂,精度被卡,能水过的题连水法都 ...
随机推荐
- GERALD07加强版题解
题目描述: N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 输入格式: 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来M行,代表图 ...
- Python 爬取各大代理IP网站(元类封装)
import requests from pyquery import PyQuery as pq base_headers = { 'User-Agent': 'Mozilla/5.0 (Windo ...
- Python每日一题 002
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...
- psql内部命令及对应sql语句
\?: 查看所有帮助 \l: 查看所有数据库 SELECT d.datname as "Name", pg_catalog.pg_get_userbyid(d.da ...
- 浅谈学习selenium的一些知识点的总结
学习自动化测试,先得学习一门语言.自动化对语言要求掌握的程度不深,但必须得会基本的入门语法. 我学习的是python2,简单,易懂,上手快. 每天敲就是了. 我的学习路径是: 先学习一段时间pytho ...
- IDEA上传项目到SVN
1.打开IDEA ,上面工具栏选择VCS 选择把项目交给SVN管理 2.选择SVN 3.选择SVN管理后可以看到项目变这个颜色 4.右键项目选择如下 5.点击绿色的+号,选择一个SVN仓库的地址,下面 ...
- Linux 下通过mail命令发送邮件
mail -s "测试" 1968089885@foxmail.com 需要先配置smtp服务器
- zabbix--zabbix server的配置以及zabbix agent的安装配置
1.zabbix server端的配置在进行源码安装zabbix时已经配置好了,具体要配置的参数如下: ListenPort=10051 server服务的监听端口,默认是10051 DBHost= ...
- docker启动elasticsearch异常Failed to create node environment(解决)
异常说是创建节点环境失败,操作/usr/share/elasticsearch/data/nodes的IO错误,尝试给此目录添加读写权限后,依旧没什么**用,灵机一动是不是挂载目录没有权限导致的? c ...
- JVM配置参数理解,Cannot load this JVM TI agent twice
基本参数 -Xms128m JVM初始分配的堆内存 -Xmx512m JVM最大允许分配的堆内存,按需分配 -XX:PermSize=64M JVM初始分配的非堆内存 -XX:MaxPermSize= ...