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 ...
随机推荐
- 07-Linux文件权限管理
文件的类型 Linux的哲学思想:一切皆文件. Linux的文件分为多种类型. 可以通过ll命令查看文件的类型: ll #输出: -rw-------. 1 root root 1266 2月 29 ...
- EF Core并发控制
EF Core并发控制 并发控制概念 并发控制:避免多个用户同时操作资源造成的并发冲突问题. 最好的解决方案:非数据库解决方案 数据库层面的两种策略:悲观.乐观 悲观锁 悲观并发控制一般采用行锁 ,表 ...
- uCos 学习:0-有关概念
先说一下UCOSIII:Micrium在2009年推出了UCOSIII,相对于之前的UCOSII版本,在性能上有了进一步的提升,主要是支持时间片轮调度,极短的关中断事件等. 可剥夺多任务管理: 什么是 ...
- 价破天荒!99元国产ARM工业“评估板”再袭,14天限量抢购!
上线即爆款!2000家企业选择! 凭借"79元超高性价比"."双核A7@1.2GHz"."国产化率100%"."ARM + DSP ...
- 2024年,AI驱动测试管理工具会有哪些发展前景呢?
随着人工智能技术的日新月异,2024年的测试管理工具将迎来全新的发展机遇.AI赋能将助力测试管理工具实现前所未有的智能化升级,为软件研发团队带来革命性的变革. 一.什么是AI? 人工智能(AI)是一种 ...
- mobaXterm 查看密码
参考:MobaXterm中密码的查看方式 检查是否把密码保存到了注册表中 然后从https://github.com/HyperSine/how-does-MobaXterm-encrypt-pass ...
- SpringBoot项目启动执行任务的几种方式
1.直接在启动类下面调用方法 @SpringBootApplication public class TestApplication { public static void main(String[ ...
- 效率工具RunFlow完全手册之进阶篇
欢迎来到RunFlow手册的进阶篇,如果您还不了解RunFlow,建议先阅读我们的基础篇. (Solo 社区投稿) 搜索文件 按文件大小过滤,添加 len 参数,比如:len:1kb-2kb,len: ...
- Happus:给准备离职成为独立开发者的你 5 点建议
名字:Happus 开发者 / 团队:Regina Dan 平台:iOS, visionOS 请简要介绍下这款产品 Happus 是你追寻幸福健康关系.甚至提高婚姻生活品质的贴心助手.无论是关系维系. ...
- [oeasy]python0141_自制模块_module_reusability_复用性
自制包内容 回忆上次内容 上次导入了外部的py文件 import my_module 导入一个自己定义的模块 可以使用my_module中的变量 不能 直接使用 my_module.py文件中的变 ...