A - A Flipping Game
这道题判断如何选择区间进行01变换让数列中的1个数最多,可以用暴力做法来做,每选择一个区间求出一个值,最后找到一个最大值。
Iahub got bored, so he invented a game to be played on paper.
He writes n integers a 1, a 2, ..., a n. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values a k for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 - x.
The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are n integers: a 1, a 2, ..., a n. It is guaranteed that each of those n values is either 0 or 1.
Output
Print an integer — the maximal number of 1s that can be obtained after exactly one move.
Examples
5
1 0 0 1 0
4
4
1 0 0 1
4
Note
In the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].
In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.
#include <string>
#include <iostream>
using namespace std;
int f[101],g[101];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&f[i]);
int sum=0,m=0;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
sum=0;
for(int s=1;s<=n;s++)//1到n的和
sum+=f[s];
for(int k=i;k<=j;k++)//取每一段相减,取反相加
{
sum=sum-f[k]+1-f[k];
}
m=max(m,sum);//最大值
}
}
printf("%d\n",m);
}
A - 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 ...
随机推荐
- 联想K5pro手机过夜后声音不能播放录音资源被占用解决方案
联想K5pro手机过夜后声音不能播放(微信头条等),录音机显示录音资源被占用无法录音,一些有声音的图像播放卡顿,关于app录音的权限我也都开了,只能靠重启才能能恢复正常. 经过实机测试,此方法处理后已 ...
- user&group_management.md
登录shell bin/bash sbin/nologin useradd 的参数 -c comment 用户的注释性信息 -u UID 指定用户的UID -g initia ...
- 7.shell脚本编程
1.shell 脚本语言的基本用法 1.1shell 脚本创建 1.格式要求:首行shebang机制 #!/bin/bash #!/usr/bin/python #!/usr/bin/perl 2.添 ...
- 爬虫-urllib模块的使用
urllib是Python中请求url连接的官方标准库,在Python3中将Python2中的urllib和urllib2整合成了urllib.urllib中一共有四个模块,分别如下: request ...
- 【Redis3.0.x】NoSql 入门
Redis3.0.x NoSql 入门 概述 NoSQL(Not Only SQL ),即不仅仅是 SQL,泛指非关系型的数据库.NoSQL 数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑 ...
- 剑指offer 面试题7:重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- python学习笔记 | selenium各浏览器驱动下载地址
Chrome http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的chromedriver.exe 版本也不一样, ...
- 安装MySQL数据库(在Windows下通过zip压缩包安装)
安装MySQL 这里建议大家使用压缩版,安装快,方便.不复杂. 软件下载 mysql5.7 64位下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7 ...
- fileinput模块用法
fileinput模块功能: 提供拼接一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行,从而进行逐行处理(如进行显示.替换.添加行号等). 其功能类似于linux命令的 ...
- kioptrixVM3
简介 Vulnhub是一个提供各种漏洞环境的靶场平台. 个人学习目的:1,方便学习更多类型漏洞.2,为OSCP做打基础. 下载链接 https://www.vulnhub.com/entry/kiop ...