Codeforces Round #191 (Div. 2) A. Flipping Game(简单)
1 second
256 megabytes
standard input
standard output
Iahub got bored, so he invented a game to be played on paper.
He writes n integers a1, a2, ..., an. 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 ak 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.
The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are n integers: a1, a2, ..., an. It is guaranteed that each of those n values is either 0 or 1.
Print an integer — the maximal number of 1s that can be obtained after exactly one move.
5
1 0 0 1 0
4
4
1 0 0 1
4
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.
思路:设数列为array,one[i]表示从array[1]到array[i](包括上下界)1的个数。故当对[i,j]范围内的数执行flip操作后,数列1的个数为:
one[n] - (one[j] - one[i-1]) + (j - i + 1 - (one[j] - one[i-1]));
式子中(one[j] - one[i-1])为[i,j]范围内1的个数,(j - i + 1 - (one[j] - one[i-1]))自然就是[i,j]中0的个数。
AC Code:
#include <iostream>
#include <cstdio> using namespace std; const int maxn = ;
int one[maxn], n; int main()
{
while(scanf("%d", &n) != EOF)
{
int b;
one[] = ;
for(int i = ; i <= n; i++)
{
one[i] = one[i-];
scanf("%d", &b);
one[i] += b;
}
int cnt = -;
for(int i = ; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
int tmp = one[n] - (one[j] - one[i-]) + (j - i + - (one[j] - one[i-]));
if(cnt < tmp) cnt = tmp;
}
}
printf("%d\n", cnt);
}
return ;
}
Codeforces Round #191 (Div. 2) A. Flipping Game(简单)的更多相关文章
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
- 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 ...
- codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)
题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...
- Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #191 (Div. 2)
在div 188中,幸运的达成了这学期的一个目标:CF1800+,所以这次可以打星去做div2,有点爽. A.Flipping Game 直接枚举 B. Hungry Sequence 由于素数的分布 ...
- Codeforces Round #191 (Div. 2) E题
状态压缩DP,算sum,本来是枚举的,结果TLE了.. #include <iostream> #include <cstring> #include <cstdio&g ...
- Codeforces Round #191 (Div. 2) D. Block Tower
D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #191 (Div. 2) B. Hungry Sequence(素数筛选法)
. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #245 (Div. 1) B. Working out (简单DP)
题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...
随机推荐
- c# Webservice技术整理
因为平常项目中使用webservice比较少,然后就将本来不太熟悉的webservice给忘记掉了.所以再次整理如下: 百度搜索关键词 :c# webservice 1. 联接地址: http://w ...
- Java中的生产者、消费者问题
Java中的生产者.消费者问题描述: 生产者-消费者(producer-consumer)问题, 也称作有界缓冲区(bounded-buffer)问题, 两个进程共享一个公共的固定大小的缓冲区(仓库) ...
- 解决:Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986
目录 背景 原因分析 处理方法 参考 背景 在将tomcat升级到7.0.81版后,发现系统的有些功能不能使用了,查询日志发现是有些地址直接被tomcat认为存在不合法字符,返回HTTP 400错误响 ...
- Spring中jdbc Template使用
http://1358440610-qq-com.iteye.com/blog/1826816
- 基于图形学混色问题OpenGl的收获
void myDisplay(void) {glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_B ...
- HDU 2132 An easy problem
http://acm.hdu.edu.cn/showproblem.php?pid=2132 Problem Description We once did a lot of recursional ...
- oracle 行转列和列转行
WITH L AS ( ), m AS ( SELECT A.LV AS LV_A, B.LV AS LV_B, TO_CHAR(B.LV) || 'x' || TO_CHAR(A.LV) || '= ...
- 【移动端debug-1】css3中box-shadow的溢出问题
今天做项目遇到一个box-shadow的溢出父容器的问题,如下面的代码中,子容器inner的box-shadow在没有任何设置的情况下是溢出父容器的. 代码: <!DOCTYPE html> ...
- 51nod 1503 猪和回文(多线程DP)
虚拟两个点,一个从左上角开始走,一个从右下角开始走,定义dp[i][j][k]表示走了i步后,第一个点横向走了j步,第二个点横向走了k步后形成的回文方法种数. 转移方程显然可得,然后滚动数组搞一搞. ...
- 洛谷 P1231 教辅的组成(网络最大流+拆点加源加汇)
题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书 ...