After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room xonly from room x - 1.
The potato pie is located in the n-th room and Vitaly needs to go there.
Each pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key.
In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.
Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.
Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.
Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.
The first line of the input contains a positive integer n (2 ≤ n ≤ 105) — the number of rooms in the house.
The second line of the input contains string s of length 2·n - 2. Let's number the elements of the string from left to right, starting from one.
The odd positions in the given string s contain lowercase Latin letters — the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter — the type of the key that lies in room number (i + 1) / 2.
The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even positioni of the given string s contains an uppercase letter — the type of the door that leads from room i / 2 to room i / 2 + 1.
Print the only integer — the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.
3 aAbB
0
4 aBaCaB
3
5 xYyXzZaZ
2
思路:从前往后扫一遍即可,记录小写字母的个数,即钥匙
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #define LL long long using namespace std; char a[200005]; int num[27]; int main() { int n; while(scanf("%d", &n) != EOF) { scanf("%s", a); memset(num, 0, sizeof(num)); int ans = 0, len = strlen(a); for(int i = 0; i < len; i++) { if(a[i] <= 'z' && a[i] >= 'a') num[a[i] - 'a'] ++; else if(a[i] <= 'Z' && a[i] >= 'A' && num[a[i] - 'A'] > 0) num[a[i] - 'A']--; else if(a[i] <= 'Z' && a[i] >= 'A' && num[a[i] - 'A'] == 0) ans ++; } printf("%d\n", ans); } return 0; }
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.
Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position|s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.
You face the following task: determine what Pasha's string will look like after m days.
The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters.
The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string.
The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.
In the first line of the output print what Pasha's string s will look like after m days.
abcdef 1 2
aedcbf
vwxyz 2 2 2
vwxyz
abcdef 3 1 2 3
fbdcea
思路:因为都是颠倒中间的数,所以可以统计每个字母总共颠倒的次数,如果为奇数则与后交换,否则不交换
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s[200005]; int a[100005]; int m; int main() { scanf("%s", s + 1); int len = strlen(s + 1); scanf("%d", &m); for(int i = 0; i < m; i++) { int t; scanf("%d", &t); a[t] ++; } for(int i = 1; i <= len/2; i++) a[i] += a[i - 1]; for(int i = 1; i <= len/2; i++) if(a[i] & 1) swap(s[i], s[len - i + 1]); printf("%s\n", s + 1); return 0; }
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.
Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.
Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:
A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks5 5 5 7.
Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.
You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks.
The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.
The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.
4 2 4 4 2
8
4 2 2 3 5
0
4 100003 100004 100005 100006
10000800015
思路:我们可以发现,只有当处于最大的边组成矩形时才会有面积最大,所以可以从后往前依次判断
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #define LL long long using namespace std; const int maxn = 1000005; int a[maxn]; int main() { int n; while(scanf("%d", &n) != EOF) { int t; memset(a, 0, sizeof(a)); for(int i = 0; i < n; i ++) { scanf("%d", &t); a[t] ++; a[t - 1] ++; } LL ans = 0; LL stick[2]; int num = 0; for(int i = maxn - 3; i >= 0; i--) { if(a[i + 2] == 1) { //小细节,要注意消除之前状态对当前的影响 a[i + 2] --; a[i + 1] --; } while(a[i] >= 2) { a[i] -= 2; a[i - 1] -= 2 - a[i + 1]; a[i + 1] = 0; stick[num++] = i; if(num == 2) { num = 0; ans += (stick[0] * stick[1]); } } } cout << ans << endl; } return 0; }