Codeforces 327A-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 xmeans 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
题意:翻牌游戏。 给出n张牌,每张牌仅仅有0和1两种状态。给出初始状态。对于翻牌操作这样规定:每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0)。求一次翻转操作后,1的个数尽量多。
枚举区间+遍历区间推断,O(n^3);
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
int a[110];
int main()
{
int n,num[2];
while(~scanf("%d",&n))
{
int ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
if(a[i]==1)
ans++;
}
int pos=ans;
if(pos==n)
{
printf("%d\n",n-1);
continue;
}
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
{
memset(num,0,sizeof(num));
for(int k=i;k<=j;k++)
num[a[k]]++;
if(num[0]>num[1])
ans=max(ans,pos+num[0]-num[1]);
}
printf("%d\n",ans);
}
return 0;
}
Codeforces 327A-Flipping Game(暴力枚举)的更多相关文章
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- codeforces Restore Cube(暴力枚举)
/* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...
- codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)
题意:n个点,m条无向边,每个边有权值,给你 s 和 t,问你至多删除两条边,让s,t不连通,问方案的权值和最小为多少,并且输出删的边 分析:n<=1000,m是30000 s,t有4种情况( ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- CodeForces 496D Tennis Game (暴力枚举)
题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增 ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举
B. Covered Path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...
- 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 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
随机推荐
- SpringCloud学习笔记(19)----Spring Cloud Netflix之服务网关Zuul自定义过滤器
zuul不仅只是路由,还可以自定义过滤器来实现服务验证. 实现案例:自定义过滤器,检验头部是否带有token,如果token=wangx,则通过校验,若不存在或不为wangx则返回提示token错误. ...
- ES6 学习3 函数
1.函数默认参数 在ES5我们给函数定义参数默认值是怎么样? function action(num) { num = num || 200 //当传入num时,num为传入的值 //当没传入参数时, ...
- Kneser猜想与相关推广
本文本来是想放在Borsuk-Ulam定理的应用这篇文章当中.但是这个文章实在是太长,导致有喧宾夺主之嫌,从而独立出为一篇文章,仅供参考.$\newcommand{\di}{\mathrm{dist} ...
- CentOS6.5安装redis(3.0.3)
如果没有安装gcc需要安装gcc 才能编译成功 yum install gcc 离线安装gcc的方法 # rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm # rpm -i ...
- Mac OS X10.10_xcode6.1_ios8.1环境下,编译lame静态库libmp3lame.a,支持arm64 armv7s x86_64 i386 armv7指令集
近期升级了系统到Mac OS X 10.10 而且更新了XCode6.1和iOS 8.1 之前app用到的libmp3lame.a静态库.也要支持64位的模拟器(x86_64)和64位的真机(arm6 ...
- hadoop-16-sqoop导入oracle数据
sqoop = sql + hadoop 1,系统方面的准备 scp ./ojdbc6.jar root@server-1:/usr/hdp/2.6.0.3-8/sqoop/lib/scp ./ojd ...
- jsp urlrewrite 中正則表達式不包括某个字符串写法
因在程序中须要做城市间跳转,可是页面中包括的css.scripts和图片等路径是要排除在外的. 这就须要在正则中指定当遇到哪些 字符时须要略过. 正则例如以下: /((? !css)(?!script ...
- hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)
题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...
- sage开发url替换字符串
/// <summary> /// Url字段值替换,无该字段则加入 /// </summary> /// <param ...
- taglist安装
注意:taglist依赖于ctags,所以要先装ctags,否则taglist装了也没法用!1.首先安装ctags1)ubuntu安装sudo apt-get install exuberant-ct ...