#枚举和排序 A - Flipping Game
来源:互联网 发布:网络配置切换器 编辑:程序博客网 时间:2024/06/04 18:39
Description
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.
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 Input
Input
5
1 0 0 1 0
Output
4
Input
4
1 0 0 1
Output
4
Hint
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.
主要思路:暴力求解
写代码时思路不够清晰,代码不简洁,落了几种情况。有待改进。
#include <stdio.h>#include <stdlib.h>int main(){ int i,j,m,n; int a[110]; scanf("%d",&n); int e=0; for(i=0; i<n; i++) { scanf("%d",&a[i]); if(a[i]==1)e++; } if(e==n-1&&a[n-1]==0) { printf("%d\n",n); } else if(n==1&&a[0]==1)printf("0\n"); else if(e==n)printf("%d\n",n-1); else if(n==1&&a[0]==0)printf("1\n"); else { int max=0; for(i=0; i<n; i++) { if(a[i]==0) { int cnt1=1,cnt2=0; for(j=i+1; j<n; j++) { if(a[j]==1)cnt2++; else cnt1++; int q=cnt1-cnt2; max=max>q?max:q; } } } for(i=0; i<n; i++) { if(a[i]==0) { int cnt1=1,cnt2=0; for(j=i+1; j<n; j++) { if(a[j]==1)cnt2++; else cnt1++; int q=cnt1-cnt2; if(max==q) { int l,r; for( l=i-1; l>=0; l--)if(a[l]==1)cnt1++; for( r=j; r<n; r++)if(a[r]==1)cnt1++; printf("%d\n",cnt1); return 0; } } } } } //system("pause"); return 0;}
附简单代码(转)
#include <stdio.h>int main(){ int arr[105]; int n, mx=0, x, y,i, j, m; scanf("%d", &n); arr[0]=0; for(i=1; i<=n; i++){ scanf("%d", &m); arr[i]=arr[i-1]+m; //区间【1,i】的元素和 } arr[n+1]=0; for(i=1; i<=n; i++){ for(j=i; j<=n; j++){ x=arr[j]-arr[i-1]; y=(j-i+1)-x+arr[i-1]+arr[n]-arr[j]; //变化区间和+左右区间和 if(y>mx)mx=y; } } printf("%d\n", mx); return 0;}
- #枚举和排序 A - Flipping Game
- Flipping Game(枚举)
- Codeforces 327A-Flipping Game(暴力枚举)
- Codeforces 327A Flipping Game(区间DP,暴力枚举)
- CF:A. Flipping Game
- A. Flipping Game
- Flipping Game(Codeforces)(枚举)
- CF 327A - Flipping Game
- CF 327 A Flipping Game
- CF 327A Flipping Game
- codeforces 327A. Flipping Game
- Codeforces - 327A - Flipping Game
- CodeForces-327A Flipping Game
- Codeforces 327A Flipping Game
- codeforces 327A. Flipping Game
- CodeForces 327A Flipping Game
- Codeforces-327A-Flipping Game
- A. Flipping Game DP水题
- 牛顿法与拟牛顿法学习笔记(五)L-BFGS 算法
- C++编程(四)--- 代码规范
- Nexus私服安装
- Android编码规范
- 【转】GO 环境配置
- #枚举和排序 A - Flipping Game
- H5页面设计
- 输入输出流
- Android 之 高德地图学习 三 覆盖物
- Scala主构造器、私有构造器、构造器重载实战详解
- 视音频数据处理入门:RGB、YUV像素数据处理
- 4.Which two statements are true regarding single row functions? (Choose two.)
- 数列特征
- python修饰符——带参数