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 ...
随机推荐
- QoS in RoCE
QoS in RoCE 首页分类标签留言关于订阅2018-03-22 | 分类 Network | 标签 RDMA RoCE ECN PFC Overview TCP/IP协议栈满足不了现代I ...
- 齿轮 HYSBZ - 4602 (DFS实现)
齿轮 HYSBZ - 4602 题意:很好理解就不啰嗦了. 致谢:感谢队友小明. 题解:嗯,一开始想到的是并查集,后来,就先看了另一道题,xj写dfs和暴力,就卡死了.于是来补这题了,前向星建图 题解 ...
- ID生成算法(一)——雪花算法
JavaScript生成有序GUID或者UUID,这时就想到了雪花算法. 原理介绍: snowFlake算法最终生成ID的结果为一个64bit大小的整数,结构如下图: 解释: 1bit.二进制中最高位 ...
- python 根据时间戳获取秒🐱
print("当前时间: ",time.strftime('%Y.%m.%d %H:%M:%S ',time.localtime(time.time()))) import tim ...
- MySQL表连接
有3种: JOIN 按照功能大致分为如下三类: CROSS JOIN (交叉连接) INNER JOIN (内连接或等值连接). OUTER JOIN (外连接) 交叉连接CROSS JOIN 交叉连 ...
- Java核心复习 —— J.U.C 并发工具类
一.CountDownLatch 文档描述 A synchronization aid that allows one or more threads to wait until* a set of ...
- 深入理解JVM虚拟机7:JNDI,OSGI,Tomcat类加载器实现
打破双亲委派模型 JNDI JNDI 的理解 JNDI是 Java 命名与文件夹接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之中的一 ...
- legend3---17、如何抽象和复用控制器中的方法
legend3---17.如何抽象和复用控制器中的方法 一.总结 一句话总结: 比如不同的控制器中都用了检查手机号是否已经注册,是否没注册这样的功能,我应该如何抽象和复用方法 新建一个处理手机号码的模 ...
- C#实现获取当前文件路径的上级路径
界面: 声明: textBox1.Text为指定文件路径:string path = @"F:\ABB-pragram\ABB工作站\ABB Station\Systems\Situatio ...
- Hive SQL NVL 相关函数
1.NVL函数 NVL函数的格式如下:NVL(expr1,expr2)含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值. NVL(exp ...