A. Flipping Game

本质上是让我们找出一段区间内\(0\)的个数大于\(1\)的个数的最多的区间,且必须进行一次操作,所以可以考虑区间\(dp\),或者最小子序列和

1 最小子序列和

\[\begin{aligned}
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

\[\begin{aligned}
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的更多相关文章

  1. Flipping elements with WPF

    http://yichuanshen.de/blog/2010/11/13/flipping-elements-with-wpf/ Have you already seen ForgottenTim ...

  2. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  3. 【OpenMesh】Some basic operations: Flipping and collapsing edges

    这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...

  4. Flipping Game(枚举)

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 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 ...

  6. Flipping Parentheses~Gym 100803G

    Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...

  7. [LeetCode] Flipping an Image 翻转图像

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  8. [Swift]LeetCode832. 翻转图像 | Flipping an Image

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  9. Flipping an Image

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  10. LeetCode 832. Flipping an Image

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

随机推荐

  1. yolov1-yolov5 网络结构&正负样本筛选&损失计算

    学习yolo系列,最重要的,最核心的就是网络模型.正负样本匹配.损失函数等三个方面.本篇汇总了yolov1-yolov5等5个版本的相关知识点,主要看点是在yolo框架搭建.初学者可以通过相关篇章搭建 ...

  2. fseek在 fopen 带有'a'模式下不起作用

    关于 fseek 在 追加写模式的注意事项 结论:fseek在 fopen 带有'a'模式的文件指针偏移不起作用. int main(int argc, char *argv[]) { FILE * ...

  3. 记一次难忘的json反序列化问题排查经历

    前言 最近我在做知识星球中的商品秒杀系统,昨天遇到了一个诡异的json反序列化问题,感觉挺有意思的,现在拿出来跟大家一起分享一下,希望对你会有所帮助. 案发现场 我最近在做知识星球中的商品秒杀系统,写 ...

  4. Python读取YAML配置数据

    python编写的一些脚本需要一些简单配置时可以使用yaml文件进行设置.本文将介绍如何使用pyyaml进行读取配置数据. 首先安装pyyaml pip install pyyaml 简单使用下pyy ...

  5. Maven的依赖详解和打包方式

    设置maven maven下载与安装教程: https://blog.csdn.net/YOL888666/article/details/122008374 1. 在File->setting ...

  6. c 语言学习第三天

    字符和字符串 字符 当我们定义了一个字符变量 c 为a时,打印的时候让它使整数形式显示.会出现怎么一个情况? #include<stdio.h> int main(){ char c = ...

  7. 在宝塔上配置打包好的vue3项目

    配置文件如下 server{ listen 80; server_name gongchang.365cb.cn; index index.html index.htm default.php def ...

  8. 关联的巧妙用法limit_choices_to

    sa_no = models.ForeignKey(CU004HModel, verbose_name='销货单', on_delete=models.PROTECT, related_name='C ...

  9. JMeter 逻辑控制之IF条件控制器

    逻辑控制之IF条件控制器 测试环境 JMeter-5.4.1 循环控制器介绍 添加While Controller 右键线程组->添加->逻辑控制器->While控制器 控制器面板介 ...

  10. Javascript 转Date对象为字符串实现函数

    转Date对象为字符串实现函数 function formatDate(time, format = "Y-MM-dd HH:mm:ss") { /** 格式化字符说明 Y 年 四 ...