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

题解:这个题目也是一道比较简单的题目

第一种方法:每一次选从前面断点开始的最长的满足要求的那个位置断开,成为新的断点  一直到最后面为止

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <cstring>
 5 #include <math.h>
 6
 7 using namespace std;
 8 char x[100005];
 9 int vis[100005];
10 int sum[100005],sumb,sumw;;
11 int main()
12 {
13     int cas=0;
14     while(cin>>x)
15     {
16         sumb=sumw=0;
17         int len = strlen(x);
18         for(int i=0; i<len; ++i)
19         {
20             if(x[i]==‘1‘)sumb++,vis[i]=1;
21             else sumw++,vis[i]=-1;
22         }
23         int ans=0;
24         cout<<"Case #"<<++cas<<":";
25         int now=0;
26         while(now<len)
27         {
28             int sum=0,pos=now;
29             for(int i=now; i<len; i++)
30             {
31                 sum+=vis[i];
32                 if(sum==1||sum==-1||!sum) pos=i;
33             }
34             ans++;
35             now=pos+1;
36         }
37         cout<<ans-1<<endl;
38     }
39
40     return 0;
41 }

第二种:我以每一块设置一个权值    1多一个权值为1      0多一个为-1          相同为0

我们可以发现  我们分成的每一块  除了不需要分的整个一块1和0的数目相同

其他的情况  我们分出来的每一块的权值一定相同

因为       1  0  可以合成1        1 -1可以合成0       0和1   0和-1

如果相邻的俩块权值是不相同  就可以像上面一样合成一块

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <fstream>
 4 #include <string>
 5 #include <cstring>
 6 using namespace std;
 7 const int N = 1e5 + 10;
 8 string str;
 9 int GetAns(){
10     int n = str.length();
11     int sum = 0;
12     for (int i = 0; i < n; i++){
13         if (str[i] == ‘0‘) sum++;
14         else               sum--;
15     }
16     if (sum < 0) sum = -sum;
17     if (sum > 0) sum--;
18     return sum;
19 }
20 int main(){
21     int ca = 0;
22     while (cin >> str){
23         ca++;
24         int ans = GetAns();
25         cout << "Case #" << ca << ":" << ans << endl;
26     }
27     return 0;
28 }
时间: 2024-07-29 03:20:12

csu 1909: Perfect Chocolate的相关文章

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50

CSU 1112: 机器人的指令【模拟题】

1112: 机器人的指令 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1858  Solved: 682 [Submit][Status][Web Board] Description 数轴原点有一个机器人.该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置. ·LEFT:往左移动一个单位 ·RIGHT: 往右移动一个单位 ·SAME AS i: 和第i 条执行相同的动作.输入保证i 是一个正整数,且不超过之前执行指令数 In

codeforces 598E E. Chocolate Bar(区间dp)

题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you ma

CSU 1416 Practical Number

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1416 结论题,具体判断方法请点击这个网址. 筛素数是肯定的,但一开始定的范围太大了,想当然要筛到10^9的质数,但仔细想想,只要到sqrt(10^9)就可以了,最后的那一个质数是最后一步的比较,不用筛出来. #include <stdio.h> #include <string.h> #include <iostream> using namespace st

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

CSU 1412 Line and Circles

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1412 题目要求判断是否有一条直线可以穿过所有的圆. 做法:把所有圆心做一次凸包,然后判断这个凸包是否能通过一个宽度为2*R的通道. 做法和求凸包直径差不多,只是判断的时候把点到两个端点的距离换成点到直线的距离. #include <stdio.h> #include <string.h> #include <math.h> #include <stdli

Poj-1274-The Perfect Stall-匈牙利算法

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19174   Accepted: 8696 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th