Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】
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 nintegers: 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.
【题意】:题意是输入一个只有0和1的序列,要求找出一个合理的区间,在这个区间里面把0变成1,1变成0,使得该区间和该区间外的1的个数达到最大。
【分析】:暴力,遍历每个区间段,小区间内的1个数 = 小区间长度 - 小区间内1的个数。小区间外1个数 = 大区间1个数 - 小区间内1个数。然后每次更新。
dp,就是求出最大区间0的个数(这个区间中1的影响为-1,0的影响为1),然后加上所有1个数就是最终答案了。
【代码】:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std; const int maxn = +; int main()
{
int n, a[maxn], i, j, k, count1, count0, max, t0, t1, t;
while (cin >> n)
{
for (t = i = ; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i])
t++; // 统计整个序列中1的个数 }
max = -;
for (i = ; i < n; i++)
{
for (j = i; j < n; j++)
{
count1 = count0 = ;
for (k = i; k <= j; k++) // 暴搜,统计每个区间的0、1数目
{
if (a[k]==)
count1++;
else
count0++;
}
if (max < count0 - count1) //max保存的是当前0、1数目最大的差(0最多,1最少)
{
max = count0 - count1;
}
}
}
printf("%d\n", t + max);
}
return ;
}
暴力枚举
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; int main()
{
int n, x, mx = ;
cin >> n;
int cnt0 = , cnt1 = ;
for (int i = ; i < n; ++i)
{
cin >> x;
if (x == ) ++cnt1; //整个区间中1的个数
if (x == )
{
++cnt0;
if (cnt0 > mx) //翻转区间所得最多1的个数
mx = cnt0;
}
else if (cnt0) --cnt0; //1的值对cnt0的取值影响为-1
}
if (mx == ) --mx; //没有0,那么至少也要翻一次
cout << mx + cnt1 << '\n';
return ;
}
DP
Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】的更多相关文章
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
- 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 ...
- Codeforces Round #191 (Div. 2) A. Flipping Game(简单)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)
题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #191 (Div. 2)
在div 188中,幸运的达成了这学期的一个目标:CF1800+,所以这次可以打星去做div2,有点爽. A.Flipping Game 直接枚举 B. Hungry Sequence 由于素数的分布 ...
- Codeforces Round #191 (Div. 2) D. Block Tower
D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)
题意 一棵 \(n\) 个点的树,每个点有权值 \(a_i\) .你想砍树. 你可以砍任意次,每次你选择一些边断开,需要满足砍完后每个连通块的权值和是相等的.求有多少种砍树方案. \(n \le 10 ...
随机推荐
- Android学习记录(4)—在java中学习多线程下载的基本原理和基本用法①
多线程下载在我们生活中非常常见,比如迅雷就是我们常用的多线程的下载工具,当然还有断点续传,断点续传我们在下一节来讲,android手机端下载文件时也可以用多线程下载,我们这里是在java中写一个测试, ...
- C#中接口与抽象类的区别
接口与抽象类是面试中经常会考到的点,容易混淆.首先了解下两者的概念: 一.抽象类: 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类 ...
- GTID环境中手动修复主从故障一例(Error 1146)
Preface In my last test of pt-heartbeat,both of master and slave were out of disk.And the my ...
- Python 绘制棋盘
import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子宽度 count = 18 # 横向纵向格子数 o = width * co ...
- jenkins shell脚本构建项目--留
1.echo `date` "doing compling . . . " if [ "$RELEASE" == "false" ]; ...
- Oz代码梳理
https://files.cnblogs.com/files/gushiren/oz%E6%B5%81%E7%A8%8B%E5%9B%BE.pdf https://files.cnblogs.com ...
- 剑指offer-重建二叉树04
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- Homework-09 二维数组动态显示
思路 主要是把计算的函数由一次跑完全部改成一次一步 主要是把一个3重for循环改为能一步一次的实现 public int nstep(int n){ while((n--)!=0){ //n步长 // ...
- java课后作业2017.10.20
动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...
- 微信公众号开发java框架:wx4j(入门篇)
导航 入门 http://www.cnblogs.com/2333/p/6617819.html WxServlet介绍 MaterialUtils 素材工具类使用说明 http://www.cnbl ...