题目链接

给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1。

这里要注意很多细节 比如全为1,要求必须翻转,这时候我们只要翻转一个1就可以了,对于其他情况,我们只要计算区间里面如果0多于1,将其翻转后计算1的总数,然后取最大值。

//cf 191 A
//2013-07-04-22.13
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[105]; int cnt[105]; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int sum = 0;
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] == 0)
{
cnt[i] = cnt[i-1] + 1;
}
else
{
cnt[i] = cnt[i-1];
sum++;
}
}
if (sum == n)
{
printf("%d\n", sum - 1);
continue;
}
if (sum == n-1)
{
printf("%d\n", n);
continue;
}
int ans = sum + 1;
for (int i = 2; i <= n; i++)
{
for (int j = 1; j < i; j++)
{
if ((cnt[i] - cnt[j-1]) > ((i-j+1) - (cnt[i] - cnt[j-1])))
ans = max (ans, sum - ((i-j+1) - (cnt[i] - cnt[j-1])) + (cnt[i] - cnt[j-1]));
}
}
printf("%d\n", ans);
}
return 0;
}

codeforces 327 A Ciel and Dancing的更多相关文章

  1. codeforces 322 A Ciel and Dancing

    题目链接 题意: 有n个男孩和m个女孩,他们要结对跳舞,每对要有一个女孩和一个男孩,而且其中一个要求之前没有和其他人结对,求出最大可以结多少对. 如图,一条线代表一对,只有这样三种情况. #inclu ...

  2. CF 322A Ciel and Dancing 好简单的题。。最喜欢水题了

    A. Ciel and Dancing time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #190 DIV.2 A. Ciel and Dancing

    #include <cstdio> #include <iostream> #include <vector> using namespace std; int m ...

  4. 网络流(费用流)CodeForces 321B:Ciel and Duel

    Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...

  5. CodeForces - 321E:Ciel and Gondolas (四边形不等式优化DP)

    题意:N个人排成一行,分成K组,要求每组的不和谐值之和最小. 思路:开始以为是斜率优化DP,但是每个区间的值其实已经知道了,即是没有和下标有关的未知数了,所以没必要用斜率. 四边形优化. dp[i][ ...

  6. CodeForces 321 A - Ciel and Robot

    [题目链接]:click here~~ [题目大意]:一个robot 机器人 .能够依据给定的指令行动,给你四种指令,robot初始位置是(0,0).指令一出.robot会反复行动,推断是否能在无限行 ...

  7. codeforces 327 B. Hungry Sequence

    题目链接 题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy. //cf 191 B #incl ...

  8. codeforces 322 B Ciel and Flowers

    题目链接 有红绿蓝三种颜色的画,每种拿三朵可以组成一束花,或者各拿一朵组成花束,告诉你每种花的数目,求出可能组成最多的花束. 如果你的代码过不了,考虑一下 8 8 9这种组合.  因为数据量很大,我的 ...

  9. Codeforces Round #190 (Div. 1 + Div. 2)

    A. Ciel and Dancing 模拟. B. Ciel and Flowers 混合类型的数量只能为0.1.2,否则3个可以分成各种类型各自合成. C. Ciel and Robot 考虑一组 ...

随机推荐

  1. yii中获取当前模块,控制器,方法

    在控制器里 $name = $this->getModule()->id; // module $name = $this->getId();  // controller $nam ...

  2. Spring Boot使用MyBatis Generator、Swagger

    MyBatis是Java目前主流的ORM框架,在Spring Boot中使用MyBatis可以参考这篇文章:http://www.ityouknow.com/springboot/2016/11/06 ...

  3. Smobiler与Windows的异步回调差别

    Smobiler与Windows的异步回调差别--基于.NET的APP开发和Windows开发差别 基于.NET的APP开发和Windows开发,异步回调差别 Windows app开发 异步回调 S ...

  4. Filebeat 7.1.1 安装及使用(连接ES)

    1. 下载 & 解压 # 下载 wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.1.1-linux- ...

  5. POJ 3183:Stump Removal(模拟)

    http://poj.org/problem?id=3183 题意:有n个树桩,分别有一个高度h[i],要用Bomb把树桩都炸掉,如果炸的位置的两边树桩高度小于Bomb炸的树桩高度,那么小于树桩高度的 ...

  6. TypeScript算法与数据结构-队列和循环队列

    本文涉及的源码,均在我的github.有两部分队列和循环队列.有问题的可以提个issue,看到后第一时间回复 1. 队列(Queue) 队列也是一种线性的数据结构, 队列是一种先进先出的数据结构.类似 ...

  7. Dijkstra算法与堆(C++)

    Dijkstra算法用于解决单源最短路径问题,通过逐个收录顶点来确保得到以收录顶点的路径长度为最短.      图片来自陈越姥姥的数据结构课程:https://mooc.study.163.com/l ...

  8. struts2入门Demo

    一.引入必要的jar包,所需jar包如下: 二.配置web.xml.主要目的是拦截请求 <?xml version="1.0" encoding="UTF-8&qu ...

  9. C++学习书籍推荐《More Exceptional C++》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <More Exceptional C++:40个新的工程难题.编程疑问及解决方法(中文版)>作为广为人知的<Exceptional C++ ...

  10. python输出带颜色详解

    书写格式:     开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于 ...