Flipping Game(枚举)
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.
题意:有n张牌,仅仅有0和1,问在[i,j]范围内翻转一次使1的数量最多。
输出1最多的牌的数量
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int n,i,j,k,t;
int a[110];
int sum[2];
int cnt=0;
while(~scanf("%d",&n))
{
cnt=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
cnt++;//记录開始时1的牌数
}
t=cnt;
if(cnt==n)
{
printf("%d\n",n-1);//假设全是1的话 你得翻一张牌 所以剩下的最大数为总数-1
}
else
{ for(i=0; i<n; i++)
for(j=i; j<n; j++)
{
memset(sum,0,sizeof(sum));
for(k=i; k<=j; k++)
sum[a[k]]++;
if(sum[0]>sum[1])
{
if(cnt<t+sum[0]-sum[1])
{
cnt=t+sum[0]-sum[1];
}
}
}
printf("%d\n",cnt);
}
}
return 0;
}
Flipping Game(枚举)的更多相关文章
- 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 ...
- UVALive 3953 Strange Billboard (状态压缩+枚举)
Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...
- 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 ...
- POJ:3185-The Water Bowls(枚举反转)
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...
- Codeforces 327A-Flipping Game(暴力枚举)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- ZOJ - 4114 Flipping Game
ZOJ - 4114 Flipping Game 题目大意:给出两个串s,t,n个灯泡的序列,1代表开着,0代表关着,一共操作k轮,每轮改变m个灯泡的状态,问最终能把s串变成t串的方案数. 坤神题解. ...
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)
建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...
- Objective-C枚举的几种定义方式与使用
假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateC ...
随机推荐
- CSS样式渐变写法
.first_tree li:hover{ color:#FFF; cursor:pointer; background-color:#ff8a00; background: -ms-linear-g ...
- Get RSA public key ASN.1 encode from a certificate in DER format
RSA public key ASN.1 encode is defined in PKCS#1 as follows: RSAPublicKey :: = SEQUENCE { modul ...
- linux—select具体解释
linux—select具体解释 select系统调用时用来让我们的程序监视多个文件句柄的状态变化的.程序会停在select这里等待,直到被监视的文件句柄有一个或多个发生了状态改变. 关于文件句柄,事 ...
- java基础---->java调用oracle存储过程(转)
存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.今天 ...
- Lisp的永恒之道 好文
http://www.cnblogs.com/weidagang2046/archive/2012/06/03/tao_of_lisp.html
- SpringMVC+Spring3+Hibernate4开发环境的搭建
在项目早期比较简单,大多用JSP .Servlet + JDBC 直接获取,以后使用 Struts1(Struts2)+Spring+Hibernate, 严格格按照分层概念驱动项目开发.利用这段时间 ...
- delphi 对抗任务管理器关闭(提升进程到Debug模式,然后设置进程信息SE_PROC_INFO)
[delphi] view plain copy program Project1; uses Windows; {$R *.res} function MakeMeCritical(Yes: Boo ...
- JAVA进阶----ThreadPoolExecutor机制(转)
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...
- Java程序猿面试题集(181- 199)
Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181. 182. 183. 184. 185. 186. 1 ...
- gradle多模块开发(转)
参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56.gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常事 ...