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 ...
随机推荐
- java中封装的概念
封装(Encapsulation)是类的三大特性之一, 就是将类的状态信息隐藏在类的内部,不允许外部程序直接访问, 而是通过该类提供的方法来实现对隐藏信息的操作和访问. 简而言之,就是隐藏内部实现,提 ...
- eclipse把jar包引入项目的两种方法
方法一: build path引入jar包 方法二: 把jar包放入lib文件夹 区别: 把jar包放入lib文件夹,以后把程序发给别人,别人可以直接运行而无需再自己添加jar包 总结: 1.有时即使 ...
- B - Help Jimmy
B - Help Jimmy Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem ...
- js动态生成下拉列表
经常需要用到js动态生成下拉列表的功能,记录下来备用. 示例需求:通过ajax请求,从后台获取用户姓名列表,并添加到下拉列表中.js代码如下: function getNameList(){ //如果 ...
- 第十三篇:HTML
本篇内容 选择器 属性 一. 选择器 1.id 选择器 id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. id 选择器以 "#" 来定义. <!DOCTY ...
- jmeter的build.xml
<?xml version="1.0"?><!-- Licensed to the Apache Software Foundation (ASF) unde ...
- BZOJ 1731: [Usaco2005 dec]Layout 排队布局
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- LACP链路聚合控制协议
LACP链路聚合控制协议 来源: https://www.cnblogs.com/taosim/articles/4378691.html http://storage.chinabyte.com/6 ...
- mysql截取字符串与reverse函数
mysql的函数大全: http://www.jb51.net/Special/606.htm 这个网页上很多知识点,可以学习下,关于mysql的函数,也可以作为API查询: 这里只说下mysql的截 ...
- Lesson 7: C#多线程
C#多线程 1.适用于: 通过网络进行通信 执行占用时间的操作 区分具有不同优先级的任务 使用户界面在执行后台任务时能快速响应用户的交互 2.Thread类常用属性及方法 属性: IsAlive:显示 ...