Flipping Game(枚举)

Flipping Game(枚举)

大家好,又见面了,我是全栈君。

Flipping Game

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 = 1x.

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: a1, a2, …, an. 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.

Sample test(s)
Input
5
1 0 0 1 0

Output
4

Input
4
1 0 0 1

Output
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.

题意:有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;
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116604.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Pytest(13)命令行参数–tb的使用

    Pytest(13)命令行参数–tb的使用前言pytest使用命令行执行用例的时候,有些用例执行失败的时候,屏幕上会出现一大堆的报错内容,不方便快速查看是哪些用例失败。–tb=style参数可以设置报错的时候回溯打印内容,可以设置参

    2022年7月30日
    5
  • linux 查看文件内容 显示行号

    linux 查看文件内容 显示行号怎么在linux系统中查看文件时显示行号?1.使用vi或者vim命令打开文件打开后的文件内容日如下2.直接输入以下命令,按Ente健显示文件行号:setnu或者:setnumber成功后显示如下…

    2022年6月22日
    63
  • matlab绘制二元函数图像z=1/(1-x^2)+y^2_python画二元二次函数图像

    matlab绘制二元函数图像z=1/(1-x^2)+y^2_python画二元二次函数图像MATLAB内置有强大的绘图功能,以下将以几个实例进行说明。绘制三维参数曲线t=-5:0.1:5;%设定参数范围theta=0:0.02*pi:2*pi;%注意如果有两个参数的话,它们的维数必须统一x=sqrt(1+t.^2).*cos(theta);%注意乘方和乘法的写法y=sqrt(1+t.^2).*sin(theta);z=2*t;%设定参数方程plot3(x,y,z,’-g’);%绘图,最后一个选项为颜色,绿色title(‘参数曲线’);%添加标题效果:可以拖动图片从不同

    2022年9月7日
    0
  • 鸿蒙系统v30能用吗_v30pro升级鸿蒙系统使用感受

    鸿蒙系统v30能用吗_v30pro升级鸿蒙系统使用感受鸿蒙鸿蒙发布在gitee上https://gitee.com/openHarmony入门指导,以Hi3516DV300为例https://gitee.com/openharmony/docs/tree/master/quick-start搭建环境在ubuntu18.4上,环境搭建可参考gitee上的入门的指导,编译顺利通过后,回头重点理一下:安装Pythonsudoaptinstall-ypythonsudoaptinstall-ypython3下载编译工具w

    2022年9月16日
    0
  • js对象中什么是可枚举性(enumerable)?[通俗易懂]

    js对象中什么是可枚举性(enumerable)?[通俗易懂]概念可枚举性(enumerable)用来控制所描述的属性,是否将被包括在for…in循环之中。具体来说,如果一个属性的enumerable为false,下面三个操作不会取到该属性。*for..in循环*Object.keys方法*JSON.stringify方法varo={a:1,b:2};o.c=3;Object.definePropert

    2022年10月29日
    0
  • STM32——软件SPI控制AD7705[通俗易懂]

    一、AD7705简介AD7705为差分输入的16位ADC,拥有两组差分输入通道。自带可编程增益,增益可在1到128调节。支持SPI接口。AD7705功能框图如图所示:AD7705的主要寄存器有通信寄存器(CommunicationRegister)、时钟寄存器(ClockRegister)、建立寄存器(SetupRegister)和数据寄存器(DataRegister)。在这些寄存器中,只有数据寄存器是16位的。通信寄存器负责寄存器寻址、读写控制…

    2022年4月15日
    142

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号