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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python生成时间戳_Python 获取时间戳

    python生成时间戳_Python 获取时间戳importtimetime_tup=time.localtime(time.time())printtime_tupformat_time=’%Y-%m-%d_%a_%H-%M-%S’cur_time=time.strftime(format_time,time_tup)printcur_time参考:https://blog.csdn.net/a542551042/articl…

    2022年10月3日
    1
  • foc无刷电机位置控制(直流无刷电机接线图解)

    序:矢量控制的核心思想是为了简化无刷电机的控制模型,将一个需要换相的无刷电机通过各种算法变换,抽象为一个直流电机的控制模型,只需要控制简单的两个直流分量来控制无刷电机,其中Vq抽象为直流电机的两端电压,Vd可调节电机力矩,但这个模型需要一个实时的电机轴角度θ参与计算。为了实现这个直流电机的控制模型,需要用到两个数学变换,即clarke变换和park变换。需要用到最原始的PID控制器等内容。…

    2022年4月11日
    384
  • 计算机设计大赛作品开发文档[通俗易懂]

    计算机设计大赛作品开发文档[通俗易懂]参加的是2020年的计算机设计大赛,软件应用与开发赛道。我们的开发文档仅供参考。可以去我的资源下载:计算机设计大赛作品开发文档目录第一章需求分析 31.1开发背景 31.2市场分析 31.2.1目标用户 31.2.2竞品分析 31.3作品简介 41.3.1主要功能 41.3.2优势和创新点 4第二章概要设计 52.1总体设计 52.2技术框架 52.2.1前端 52.2.2后端 62.3模块设计 6第三章详细设计 83.1界面设计 83.2数据库设

    2022年7月16日
    14
  • cmpp发送短信[通俗易懂]

    cmpp发送短信[通俗易懂]思路:把各种操作解耦,创建各种线程异步进行1.创建一个阻塞队列用来存储任务2.创建一个任务线程,从待发表里取数据(待发表里有专门标明是否被处理过的字段),注入任务队列3.创建n个接收线程,向接收表中塞入数据4.创建n个发送进程,从任务队列里取数据,发送一共有待发表,接收表,已发表,发送错误表,回执状态表5张基础表具体基础代码参考cmpp2.0文档,或者自己搜索…

    2025年7月5日
    2
  • Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」

    Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」工作中用到一段比较复杂的SQL查询脚本,使用了listagg()函数实现了具有多个值的字段的填充(即,列表聚合,listaggregation(我猜的))。说简单点,listagg()函数可以实现多列记录聚合为一条记录,从而实现数据的压缩、致密化(datadensification)。以下内容转载自http://dacoolbaby.iteye.com/blog/1698957,SQL脚本做了…

    2025年8月29日
    7
  • RecycleView 获取第一个可见条目(掉坑篇)

    RecycleView 获取第一个可见条目(掉坑篇)

    2021年3月12日
    144

发表回复

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

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