题目链接

给你一串只有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. Linux下docker的安装

    前言: 因为之前在自己的mac上直接使用HomeBrew的包管理安装的,使用brew install docker即可,这种方法简单,但最近想尝试在Linux下安装,费了一些时间,主要是启动docke ...

  2. Educational Codeforces Round 66 (Rated for Div. 2) A

    A. From Hero to Zero 题目链接:http://codeforces.com/contest/1175/problem/A 题目 ou are given an integer n ...

  3. PATB 1015. 德才论 (25)

    1015. 德才论 (25) 比较函数折腾好久,最后还因为cout,printf的区别而超时,超时是因为cout输出效率低. 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 ...

  4. gRPC入坑记

    概要 由于gRPC主要是谷歌开发的,由于一些已知的原因,gRPC跑demo还是不那么顺利的.单独写这一篇,主要是gRPC安装过程中的坑太多了,记录下来让大家少走弯路. 主要的坑: 如果使用PHP.Py ...

  5. Kibana 7.1.1 安装及简单使用

    1. 下载 & 解压 # 下载 wget https://artifacts.elastic.co/downloads/kibana/kibana-7.1.1-linux-x86_64.tar ...

  6. scikit-learn算法选择路径图

     原文链接:https://blog.csdn.net/guang_mang/article/details/73658496

  7. guava cache大量的WARN日志的问题分析

    一.问题显现 2019-04-21 11:16:32 [http-nio-4081-exec-2] WARN com.google.common.cache.LocalCache - Exceptio ...

  8. Python编程菜鸟成长记--A1--04--Hello World!

    1.重点知识 掌握使用 命令行.文件.Jupyter 的方式执行 Python 代码 2.Hello World! 自从 C 语言之父 丹尼斯.M.里奇 在<The C Programming ...

  9. Python入门(一) 异常处理

    异常处理 捕捉异常可以使用try/except语句. try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理. 以下是语法: try: <语句> # ...

  10. HDU 4055:Number String(DP计数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...