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 ...
随机推荐
- spring cache 学习 —— @Cacheable 使用详解
1. 功能说明 @Cacheable 注解在方法上,表示该方法的返回结果是可以缓存的.也就是说,该方法的返回结果会放在缓存中,以便于以后使用相同的参数调用该方法时,会返回缓存中的值,而不会实际执行该方 ...
- PHP jquer网页打印插件 PrintArea
<!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...
- 使用nodejs和express搭建http web服务
目录 简介 使用nodejs搭建HTTP web服务 请求nodejs服务 第三方lib请求post 获取http请求的正文 Express和使用express搭建http web服务 express ...
- 【SpringBoot】前缀树 Trie 过滤敏感词
1.过滤敏感词 Spring Boot实践,开发社区核心功能 完成过滤敏感词 Trie 名称:Trie也叫做字典树.前缀树(Prefix Tree).单词查找树 特点:查找效率高,消耗内存大 应用:字 ...
- C语言逗号运算符(C语言学习笔记)
什么是逗号运算符 逗号运算符 逗号运算符是指在C语言中,多个表达式可以用逗号分开,其中用逗号分开的表达式的值分别结算,但整个表达式的值是最后一个表达式的值. 用法 多个变量赋值 原因:"=& ...
- iptables原理及防火墙规则语法基础
Iptables 防火墙 学习总结: 三张表介绍: filter负责过滤数据包,包括的规则链有,input(进),output(出)和forward(转发); nat则涉及到网络地址转换,包括的规则 ...
- 同步alv的前端显示和输出内表中的数据
在使用CL_GUI_ALV_GRID显示报表的时候,当我们使用了checkbox的时候,或者是有可编辑的字段,当我们 在前段修改了单元格内容的时候,后台的内表并不会自动的更新,此时需要我们调用一个方法 ...
- Java高并发与多线程(四)-----锁
今天,我们开始Java高并发与多线程的第四篇,锁. 之前的三篇,基本上都是在讲一些概念性和基础性的东西,东西有点零碎,但是像文科科目一样,记住就好了. 但是本篇是高并发里面真正的基石,需要大量的理解和 ...
- HTML基础复习3
CSS 可以理解为对HTML的一种补充 CSS由两部分组成:选择器.声明,声明中包含属性和值 CSS中的选择器 HTML标签选择器 类选择器 在标签上使用class属性为标签起个类名,在CSS中使用. ...
- 从定义到AST及其遍历方式,一文带你搞懂Antlr4
摘要:本文将首先介绍Antlr4 grammer的定义方式,如何通过Antlr4 grammer生成对应的AST,以及Antlr4 的两种AST遍历方式:Visitor方式和Listener方式. 1 ...