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的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. [LeetCode] Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  4. C++: Perfect Forwarding

    Link: Rvalue References and Perfect Forwarding in C++0x (https://www.justsoftwaresolutions.co.uk/cpl ...

  5. 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 ...

  6. 在执行xp_cmdshell的过程中出错,调用'LogonUserW'失败,错误代码:'1909'

    在上篇文章Could not obtain information about Windows NT group/user 'xxxx\xxxx', error code 0x5里面,我介绍了SQL ...

  7. POJ1274 The Perfect Stall[二分图最大匹配]

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  8. 【leetcode】Perfect Squares (#279)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  9. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

随机推荐

  1. SpringMVC拦截器及多拦截器时的执行顺序

    本文链接:https://blog.csdn.net/itcats_cn/article/details/80371639拦截器的配置步骤 springmvc.xml中配置多个拦截器配置自定义拦截器并 ...

  2. K Simple question (第十届山东理工大学ACM网络编程擂台赛 正式赛)

    题解:素数筛+唯一分解定理 可以把素数筛那部分放到while之外,减小时间复杂度. #include <stdio.h> #include <stdlib.h> #includ ...

  3. jQuery的ajax请求express服务器返回数据

    html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. 基于部标1078视频协议和苏标Adas协议构建主动安全平台

    苏标本身仍然是基于部标808协议的基础上递增起草的,苏标协议是包容808协议的, 不能脱离808协议而独立存在的, 主要基于<JT/T 796 道路运输车辆卫星定位系统平台技术要求>.&l ...

  5. 浅谈Min_25筛(一看就懂的那种)

    作用 前提:一个积性函数\(F(i)\),要求\(F(P^k),P\in prime\)可以快速计算 实现\(O(\frac{n^{\frac{3}{4}}}{logn})\):\(\sum\limi ...

  6. 小程序 image跟view标签上下会有间隙

    图片文字等inline元素默许是跟父级元素的baseline对齐,而baseline又和父级底边有必定间距 我是使用: 加上这个消除了间隙,如果没有解决,你可以分别用 vertical-align:t ...

  7. 1.linux 基本操作和命令

    整理复习之前的linux学习笔记,正好贴出来了.   1.[root@chen ~]#    [当前登录用户@主机名  当前所在目录]# 当前用户身份     #号表示管理员root     $号表示 ...

  8. FM与FFM深入解析

    因子机的定义 机器学习中的建模问题可以归纳为从数据中学习一个函数,它将实值的特征向量映射到一个特定的集合中.例如,对于回归问题,集合 T 就是实数集 R,对于二分类问题,这个集合可以是{+1,-1}. ...

  9. arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634

    学习:http://zhihu.esrichina.com.cn/article/634使用光标和内存中的要素类将数据加载到要素集 import arcpy arcpy.env.overwriteOu ...

  10. 运行模型,COM错误,解决问题步骤

    运行模型,COM错误,解决问题步骤 1.数据新建一个,路径短一点,不要有中文 2.所有数据重新导入 3.文档新建 4,问题莫名奇妙解决了