csu 1909: Perfect Chocolate
1909: Perfect Chocolate
Submit Page Summary Time Limit: 3 Sec Memory Limit: 128 Mb Submitted: 90 Solved: 42
Description
There is a chocolate, is composed of black and white small pieces. Xiao Ming is very fond of this chocolate, and the absolute difference between the number of black pieces and the number of white pieces is not more than 1, he think this chocolate is perfect.
Now Xiao Ming has one this chocolate in his hand, maybe it is not perfect.He will cut it into small blocks, making these small blocks are all perfect, he wanted to know how many times he will cut at least.
Input
There are multiple test cases.
For each test case.There is only one string composed of ‘0’ and ‘1’.’0’ is for the white piece while ‘1’ for the black piece.The length of the string for each case is not more than 100000.
Output
For each test case, you output one line “Case #%d:%d”
Sample Input
10011100010000
1
Sample Output
Case #1:3
Case #2:0
Hint
Source
2017年湖南多校对抗赛第8场
Author
HNU
题解:这个题目也是一道比较简单的题目
第一种方法:每一次选从前面断点开始的最长的满足要求的那个位置断开,成为新的断点 一直到最后面为止
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <math.h> using namespace std;
char x[];
int vis[];
int sum[],sumb,sumw;;
int main()
{
int cas=;
while(cin>>x)
{
sumb=sumw=;
int len = strlen(x);
for(int i=; i<len; ++i)
{
if(x[i]=='')sumb++,vis[i]=;
else sumw++,vis[i]=-;
}
int ans=;
cout<<"Case #"<<++cas<<":";
int now=;
while(now<len)
{
int sum=,pos=now;
for(int i=now; i<len; i++)
{
sum+=vis[i];
if(sum==||sum==-||!sum) pos=i;
}
ans++;
now=pos+;
}
cout<<ans-<<endl;
} return ;
}
第二种:我以每一块设置一个权值 1多一个权值为1 0多一个为-1 相同为0
我们可以发现 我们分成的每一块 除了不需要分的整个一块1和0的数目相同
其他的情况 我们分出来的每一块的权值一定相同
因为 1 0 可以合成1 1 -1可以合成0 0和1 0和-1
如果相邻的俩块权值是不相同 就可以像上面一样合成一块
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
const int N = 1e5 + ;
string str;
int GetAns(){
int n = str.length();
int sum = ;
for (int i = ; i < n; i++){
if (str[i] == '') sum++;
else sum--;
}
if (sum < ) sum = -sum;
if (sum > ) sum--;
return sum;
}
int main(){
int ca = ;
while (cin >> str){
ca++;
int ans = GetAns();
cout << "Case #" << ca << ":" << ans << endl;
}
return ;
}
csu 1909: Perfect Chocolate的更多相关文章
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- C++: Perfect Forwarding
Link: Rvalue References and Perfect Forwarding in C++0x (https://www.justsoftwaresolutions.co.uk/cpl ...
- How to write perfect C code
Several days ago, I was involved in an argument about choice of C or C++. What I ignored was "l ...
- 在执行xp_cmdshell的过程中出错,调用'LogonUserW'失败,错误代码:'1909'
在上篇文章Could not obtain information about Windows NT group/user 'xxxx\xxxx', error code 0x5里面,我介绍了SQL ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
随机推荐
- sql server select 1 from 的作用
select 1 from table 语句中的1代表什么意思 在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条selec ...
- CF798D Mike and distribution 贪心
我感觉这道题挺神的~ 假设 $a[i]=b[i]$,那么我们可以将 $a$ 降序排序,然后你发现只要你按照 $1,3,5......n$ 这么取一定是合法的. 而我们发现 $2$ 比取 $3$ 优,取 ...
- __stdcall、Q_DECL_IMPORT、Q_DECL_EXPORT
https://www.cnblogs.com/songfeixiang/p/3733661.html 在C语言中,假设我们有这样的一个函数:int function(int a,int b)调用时只 ...
- 学生管理系统——数据库、java基础
1.项目分层 view层:视图层 controller层:控制层 service层:业务层 dao层:数据库访问层 domain:实体包 tools:工具类 2.jar包 3.配置文件 4.程序设计 ...
- Apache的安装和配置
一.官网下载Apache 官网地址:https://httpd.apache.org/ 点击Download--->点击Files for Microsoft Windows--->点击A ...
- 3、spark Wordcount
一.用Java开发wordcount程序 1.开发环境JDK1.6 1.1 配置maven环境 1.2 如何进行本地测试 1.3 如何使用spark-submit提交到spark集群进行执行(spar ...
- Python中匹配IP的正则表达式
下面是IPv4的IP正则匹配表达式 import re #简单的匹配给定的字符串是否是ip地址,下面的例子它不是IPv4的地址,但是它满足正则表达式 if re.match(r"^(?:[0 ...
- SqlServer 获取 当前地址下 所有数据库字段信息 / 快速 批量插入数据库(TVPs)
SQL执行 --拼装 当前地址下 所有数据库字段信息 BEGIN DECLARE @dataBaseName NVARCHAR(MAX)--数据库名称 DECLARE @tableName NVARC ...
- js小脚本解析后台数据
java代码 List<CodeTableBean> clfsList = StandardCodeTable.getCodeTable("clfs", "& ...
- 关于long_query_time的设置,可不可以说是mysql的一个小小bug呢
我们知道对对于MySQL的日志功能,我们可以完全自己控制到底写还是不写.一般来说,binlog我们一般会开启,而对于慢查询我们一般会在开发的时候调试和观察SQL语句的执行速度.但今天发现一个问题.在使 ...