A Vasya the Hipster
One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and bblue socks. According to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot.
Every day Vasya puts on new socks in the morning and throws them away before going to bed as he doesn't want to wash them.
Vasya wonders, what is the maximum number of days when he can dress fashionable and wear different socks, and after that, for how many days he can then wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.
Can you help him?
Input
The single line of the input contains two positive integers a and b (1 ≤ a, b ≤ 100) — the number of red and blue socks that Vasya's got.
Output
Print two space-separated integers — the maximum number of days when Vasya can wear different socks and the number of days when he can wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.
Keep in mind that at the end of the day Vasya throws away the socks that he's been wearing on that day.
Sample test(s)
input
3 1
output
1 1
input
2 3
output
2 0
input
7 3
output
3 2
Note
In the first sample Vasya can first put on one pair of different socks, after that he has two red socks left to wear on the second day.
题目类型:水题
算法分析:输出两个数的最小值和另一个数减去最小值除以二的结果
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #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; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 1666; int main() { // CPPFF; int a, b; cin >> a >> b; int c = min (a, b); cout << c << " "; a -= c, b -= c; if (a == 0) cout << b / 2 << endl; else cout << a / 2 << endl; return 0; } |
B Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number ito make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Sample test(s)
input
5
1 2 3 1 2
output
3 2 0 2 0
input
4
3 2 1 4
output
2 3 4 0
题目类型:区间最值查询
算法分析:直接使用线段树查询每个区间的最值即可,注意此时输出”0”的条件!!!
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 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#pragma comment(linker, "/STACK:102400000,102400000") #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; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 1666; #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r LL maxval[maxn<<2], mmax, aa[maxn], len; void PushUp (int rt) { maxval[rt] = max (maxval[rt<<1], maxval[rt<<1|1]); } void Build (int rt, int l, int r) { if (l == r) { scanf ("%d", &maxval[rt]); aa[len] = maxval[rt]; len++; return ; } int m = (l + r) >> 1; Build (lson); Build (rson); PushUp (rt); } void Query (int rt, int l, int r, int L, int R) { if (L <= l && r <= R) { mmax = max (mmax, maxval[rt]); return; } int m = (l + r) >> 1; if (L <= m) Query (lson, L, R); if (R > m) Query (rson, L, R); } int main() { // CFF; LL n; scanf ("%I64d", &n); len = 1; Build (1, 1, n); for (LL i = 1; i <= n; i++) { mmax = -INF; Query (1, 1, n, i + 1, n); if (i != 1) printf (" "); if (aa[i] >= mmax) printf ("0"); else printf ("%I64d", mmax - aa[i] + 1); } return 0; } |
C Developing Skills
Petya loves computer games. Finally a game that he's been waiting for so long came out!
The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the numberx down to the nearest integer.
At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.
Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
Input
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal.
The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
Output
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
Sample test(s)
input
2 4
7 9
output
2
input
3 8
17 15 19
output
5
input
2 2
99 100
output
20
Note
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal tolfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.
In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .
In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
题目类型:排序+贪心
算法分析:由于最后的和是整除10的结果,所以一个贪心策略是先将每个能加的数加到最近整除10的数,这里可以先排序处理。最后如果还有富余,则看能够累加多少个10(需比较此时每个数的值)
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 95 96 97 98 99 100 101 |
#pragma comment(linker, "/STACK:102400000,102400000") #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; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 1666; struct node { int v, cnt; }; bool operator < (const node a, const node b) {return a.cnt < b.cnt;} node aa[maxn]; int main() { // CFF; int n, k; scanf ("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf ("%d", &aa[i].v); aa[i].cnt = 0; for (int j = aa[i].v; j % 10 != 0; j++) aa[i].cnt++; } sort (aa + 1, aa + 1 + n); for (int i = 1; i <= n; i++) { if (aa[i].v == 100) continue; if (aa[i].v < 100 && k >= aa[i].cnt) { k -= aa[i].cnt; aa[i].v += aa[i].cnt; } else break; } int tt = 0; if (k) { for (int i = 1; i <= n; i++) tt += (100 - aa[i].v) / 10; k /= 10; if (k < tt) tt = k; } int res = 0; for (int i = 1; i <= n; i++) res += aa[i].v / 10; printf ("%d\n", res + tt); return 0; } |
D Three Logos
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
Input
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), wherexi and yi determine the length and width of the logo of the i-th company respectively.
Output
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that:
- the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company,
- the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company,
- the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
Sample test(s)
input
5 1 2 5 5 2
output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
input
4 4 2 6 4 2
output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC
题目类型:涂色模拟
算法分析:首先如果每个矩形的面积和不是完全平方数,则直接输出”-1”,否则先将A涂上(由于可以旋转且3个矩形要把一个正方形涂满,易知每个矩形至少覆盖两条正方形的边,则A从左上角涂是合理的)。然后开始涂B,此时需要分类判断剩下的位置是否满足B的范围,注意此时涂B不一定要将某一行(列)都涂满!!!涂完B之后将剩下的所有位置都涂上C(不见得是合理的),最后再判断涂上的C是否是题目所给的矩形,如果不是,则输出”-1”,否则输出答案即可
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
#pragma comment(linker, "/STACK:102400000,102400000") #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; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 166; char aa[maxn][maxn]; int main() { // CFF; int x1, y1, x2, y2, x3, y3; //freopen ("bbb.txt", "w", stdout); scanf ("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3); int tt = x1 * y1 + x2 * y2 + x3 * y3; int val = (int) sqrt ((double) tt); if (val * val != tt) puts ("-1"); else { tt = (int) sqrt ((double)tt); for (int i = 1; i <= tt; i++) for (int j = 1; j <= tt; j++) aa[i][j] = '@'; for (int i = 1; i <= x1; i++) for (int j = 1; j <= y1; j++) aa[i][j] = 'A'; int rrow = tt - x1, rcol = tt - y1; if (rrow && rcol) { if (rcol == x2 || rcol == y2) { int ta; if (rcol == x2) ta = y2; else ta = x2; for (int i = 1; i <= ta; i++) for (int j = y1 + 1; j <= tt; j++) aa[i][j] = 'B'; for (int i = 1; i <= tt; i++) for (int j = 1; j <= tt; j++) if (aa[i][j] != 'A' && aa[i][j] != 'B') aa[i][j] = 'C'; } else { int ta; if (rrow == x2) ta = y2; else ta = x2; for (int i = x1 + 1; i <= tt; i++) for (int j = 1; j <= ta; j++) aa[i][j] = 'B'; for (int i = 1; i <= tt; i++) for (int j = 1; j <= tt; j++) if (aa[i][j] != 'A' && aa[i][j] != 'B') aa[i][j] = 'C'; } } if (!rrow) { swap (rrow, rcol); swap (x1, y1); for (int i = 1; i <= tt; i++) for (int j = i; j <= tt; j++) { if (i == j) continue; swap (aa[i][j], aa[j][i]); } } if (!rcol) { if (tt == x2 || tt == y2) { int ta; if (tt == x2) ta = y2; else ta = x2; for (int i = x1 + 1, k = 1; k <= ta; i++, k++) for (int j = 1; j <= tt; j++) aa[i][j] = 'B'; } else { if (rrow == x2 || rrow == y2) { int ta; if (rrow == x2) ta = y2; else ta = x2; for (int i = x1 + 1; i <= tt; i++) for (int j = 0; j <= ta; j++) aa[i][j] = 'B'; } } for (int i = 1; i <= tt; i++) for (int j = 1; j <= tt; j++) if (aa[i][j] != 'A' && aa[i][j] != 'B') aa[i][j] = 'C'; } bool is_valid = true; for (int i = 1; i <= tt; i++) { int tx = 0; for (int j = 1; j <= tt; j++) if (aa[i][j] == 'C') tx++; if (tx && tx != x3 && tx != y3) { is_valid = false; break; } } if (!is_valid) puts ("-1"); else { printf ("%d\n", tt); for (int i = 1; i <= tt; i++) { for (int j = 1; j <= tt; j++) printf ("%c", aa[i][j]); puts (""); } } } return 0; } |
- poj3617:下一篇 »