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. Pandas中的常用函数

    1. map.apply.applymap 参考:Pandas教程 | 数据处理三板斧--map.apply.applymap详解 在日常的数据处理中,经常会对一个DataFrame进行逐行.逐列和逐 ...

  2. vue2.x版本升级2.7版本

    2022年7月1日,vue正式迎来2.7版本,代号:"Naruto".支持 Composition API + <script setup> .原文链接 也就是说,你可 ...

  3. ComfyUI进阶篇:ControlNet核心节点

    前言: ControlNet_aux库包含大量的图片预处理节点,功能丰富,适用于图像分割.边缘检测.姿势检测.深度图处理等多种预处理方式.掌握这些节点的使用是利用ControlNet的关键,本篇文章将 ...

  4. TP5系列 | Queue消息队列

    消费信息如下ThinkPHP5 Queue消息队列 优点 1.Queue内置了 Redis,Database,Topthink ,Sync这四种驱动,本文使用Redis驱动 2.Queue消息队列适用 ...

  5. 从Mybatis-Plus开始认识SerializedLambda

    从Mybatis-Plus开始认识SerializedLambda 背景 对于使用过Mybatis-Plus的Java开发者来说,肯定对以下代码不陌生: @TableName("t_user ...

  6. JS实现复制粘贴图片

    最近在开发公司的可视化编辑器应用, 同事们提了一个需求, 即可以直接复制图片到编辑器中粘贴, 生成对应的图片组件. 因为传统的点击上传太麻烦, 得先把图片保存到本地, 然后再回到编辑器点击上传, 选择 ...

  7. OpenGL book note

    OpenGL Shading language 4.0vertex shader geometry shader: 格式解释 https://blog.csdn.net/hankern/article ...

  8. 2023/4/15 SCRUM个人博客

    1.我昨天的任务 获得了人脸识别作弊检测和绘制界面的分工,准备先从作弊检测入手 2.遇到了什么困难 对作弊检测的组件不熟悉,进展缓慢,需要进行对点的学习 3.我今天的任务 初步学习cython

  9. 什么是web3 为什么web3很重要

    中心化网络已经帮助数十亿人融入了互联网,并在其上创建了稳定.可靠的基础设施. 与此同时,少数中心化巨头几乎垄断了互联网,甚至可以为所欲为. Web3 是摆脱这一困境的方案. 不同于科技巨头垄断的传统互 ...

  10. 【Mycat】01 概述

    什么是Mycat? 数据库中间件 中间件:是一类连接软件组件和应用的计算机软件,以便于软件各部件之间的沟通. 例子:Tomcat,web中间件. 数据库中间件:连接java应用程序和数据库 为什么要用 ...