A. Flipping Game
A. Flipping Game
本质上是让我们找出一段区间内\(0\)的个数大于\(1\)的个数的最多的区间,且必须进行一次操作,所以可以考虑区间\(dp\),或者最小子序列和
1 最小子序列和
dp_i是以a_i结尾的最小子序列和 \\
dp_i=\min(dp_{i-1}+a[i],a[i])
\end{aligned}
\]
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1);
i64 ans = 0 ;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
ans += a[i];
if (!a[i]) a[i] = -1;
}
if (ans == n) ans --;
vector<int> dp(n + 1);
int k = 0;
for (int i = 1; i <= n; i ++) {
dp[i] = min(dp[i - 1] + a[i], a[i]);
if (dp[i] < dp[k])
k = i;
}
cout << ans - dp[k] << '\n';
return 0;
}
2 区间dp
dp_{i,j}&为从i到j区间所有元素取反的和 \\
dp_{i,j}&=\max(dp_{i+1,j}+(1-a_i),dp_{i,j-1}+(1-a_j))
\end{aligned}
\]
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1), pre(n + 1);
vector dp(n + 2, vector<int>(n + 2));
int ans = 0 ;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
dp[i][i] = a[i] ^ 1;
}
for (int len = 1; len <= n; len ++) {//枚举区间长度
for (int i = 1; i + len - 1 <= n; i ++) {
int j = i + len - 1;
dp[i][j] = max(dp[i + 1][j] + (a[i] ^ 1), dp[i][j - 1] + (a[j] ^ 1));
ans = max(ans, pre[i - 1] + dp[i][j] + pre[n] - pre[j]);
}
}
cout << ans << '\n';
return 0;
}
A. Flipping Game的更多相关文章
- Flipping elements with WPF
http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTim ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- 【OpenMesh】Some basic operations: Flipping and collapsing edges
这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...
- Flipping Game(枚举)
Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #191 (Div. 2)---A. Flipping Game
Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Flipping Parentheses~Gym 100803G
Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...
- [LeetCode] Flipping an Image 翻转图像
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- [Swift]LeetCode832. 翻转图像 | Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- LeetCode 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
随机推荐
- 【UnityTips】如何自定义脚本模版
[UnityTips]如何自定义脚本模版 通常我们创建新脚本时大家看到的是这个样子: using System.Collections; using System.Collections.Generi ...
- Flink状态(二)
Flink提供了不同的状态存储方式,并说明了状态如何存和存储在哪里. 状态可以被存储在Jvm的堆和堆外.根据状态存储方式的不同,Flink也能代替应用管理状态,意思是Flink能够进行内存管理(有必要 ...
- python的requirements.txt_维护项目依赖包
pycharm没有类似maven用于管理依赖包的工具,当一个项目在新的环境运行前,需要将对应依赖的包下载回来,如果一个个下载,会出现缺漏或版本号不对应的情况,这个时候可以用requirements.t ...
- Linux OpenGrok搭建
目录 一.目的 二.环境 三.相关概念 3.1 OpenGrok 3.2 CTags 3.3 Tomcat 四.OpenGrok搭建 4.1 安装jdk 4.2 安装ctags依赖 4.3 安装uni ...
- 个人团队兼职开发app(社交,语聊1v1,视频直播)
如果您有意向创业,意向社交类产品,如语聊,及时通信,视频直播,1v1等,又苦无没有人力资源. 我们岁数都是30+,在互联网行业摸爬滚打十年有余. 后端,前端,客户端,运维,四个人. 我们共事很长一段时 ...
- ZYNQ:Linux添加I2C-RTC驱动
硬件情况 使用的是DS1338这款RTC时钟芯片,I2C总线对应到PS端的I2C1. 配置 内核 添加有关的驱动: 因为DS1338用的驱动与DS13307相似,一找发现是同一个配置. CONFIG_ ...
- 无业游民写的最后一个.net有关项目框架
理想很丰满,现实往往很残酷. 一种按照ddd的方式,根据业务来把自己需要的模块一个一个写出来,再按照模块把需要的接口一个一个的写出来,堆砌一些中间件,以及解耦的command,handler等等 ,一 ...
- yb课堂之跨域配置 《二十三》
CorsInterceptor.java package net.ybclass.online_ybclass.interceptor; import org.springframework.http ...
- 也说一说IDEA热部署Web项目最终解决方案,确实大大提高工作效率
热部署就是正在运行状态的应用,修改了它的源码之后,在不重新启动的情况下能够自动把增量内容编译并部署到服务器上,使得修改立即生效.热部署为了解决的问题有两个: 1.在开发的时候,修改代码后不需要重启应用 ...
- 转载 | ofd转pdf最好用的软件,ofd文件如何转化成pdf?
1.背景 需要将ofd转换为pdf 2.使用方法 使用taurusxin 开发的软件Ofd2Pdf.exe即可实现,软件版权归原作者所有.这里表示感谢! 3.下载地址 官网:https://githu ...