A.Case of the Zeros and Ones
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Sample test(s)
input
4
1100
output
0
input
5
01010
output
1
input
8
11101111
output
6
Note
In the first sample test it is possible to change the string like the following: .
In the second sample test it is possible to change the string like the following: .
In the third sample test it is possible to change the string like the following: .
题目类型:简单数学
算法分析:由题目可知,只要字符串中存在有’0’和’1’,就会发生”合并”进而将字符串的长度缩小,所以只要计算出字符串中’0’和’1’出现的最小次数,然后直接乘以2,再使用总长度减去即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; int main() { // ifstream cin ("aaa.txt"); int n; cin >> n; string s; cin >> s; int aa = 0, bb = 0; for (int i = 0; s[i]; i++) { if (s[i] == '0') aa++; else if (s[i] == '1') bb++; } if (aa == 0 || bb == 0) cout << n << endl; else { aa = min (aa, bb); cout << n - aa * 2 << endl; } return 0; } |
B.Case of Fake Numbers
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.
Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.
Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.
Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.
Input
The first line contains integer n (1 ≤ n ≤ 1000) — the number of gears.
The second line contains n digits a1, a2, ..., an (0 ≤ ai ≤ n - 1) — the sequence of active teeth: the active tooth of thei-th gear contains number ai.
Output
In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.
Sample test(s)
input
3
1 0 0
output
Yes
input
5
4 2 1 4 3
output
Yes
input
4
0 2 3 1
output
No
Note
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
题目类型:暴力枚举
算法分析:直接按照题目的要求模拟10000次,然后判断是否满足条件即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; int a[maxn], temp[maxn]; int main() { // ifstream cin ("aaa.txt"); int n; cin >> n; for (int i = 1; i <= n; i++) cin >>a[i]; bool is_valid = false; int cnt = 0; while (cnt <= 10000 +66) { for (int i = 1; i <= n; i++) { if (i & 1) { a[i] = (a[i] + 1) % n; } else { if (a[i] == 0) a[i] = n - 1; else a[i]--; } } int j; for (j = 1; j <= n; j++) if (a[j] != j - 1) break; if (j > n) { is_valid = true; break; } cnt++; } if (is_valid) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
C.Case of Matryoshkas
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example,1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
- Having a matryoshka athat isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
- Having a matryoshka adirectly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and thenmi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshkaai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Sample test(s)
input
3 2
2 1 2
1 3
output
1
input
7 3
3 1 3 7
2 2 5
2 4 6
output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
题目类型:贪心+枚举
算法分析:贪心策略是找到一个从1开始每个元素之间相差1的最长递增数列a,即这个数列标号的娃娃不拆出来,然后其他的娃娃如果在某一个娃娃中,则将其取出;最后将每一个a之后的娃娃一个一个地套入即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100000 + 66; int a[maxn], b[maxn]; bool flag[maxn]; int main() { // ifstream cin ("aaa.txt"); int n, k; cin >> n >> k; long long res = 0; memset (flag, false, sizeof(flag)); for (int i = 1; i <= k; i++) { int bb; cin >> bb; for (int j = 1; j <= bb; j++) cin >> b[j]; for (int j = 1; j - 1 <= bb; ) { if (b[j] == 1 && b[j+1] == 2) { while (j - 1 <= bb && b[j+1] == b[j] + 1) { flag[j] = flag[j+1] = true; j++; } } else { res += bb - j; break; } } } int i; for (i = 1; i <= n; i++) { if (!flag[i]) break; } if (i == 1) cout << res + (n - i) << endl; else cout << res + (n - (i - 1)) << endl; return 0; } |