Codeforces Round #541 (Div. 2)
A. Sea Battle
Description:
In order to make the “Sea Battle” game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of and a height of , while the second rectangle has a width of and a height of , where . In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field.
The rectangles are placed on field in the following way:
- the second rectangle is on top the first rectangle;
- they are aligned to the left, i.e. their left sides are on the same line;
- the rectangles are adjacent to each other without a gap.
See the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue.
Formally, let’s introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates , the rightmost top cell of the first rectangle has coordinates , the leftmost bottom cell of the second rectangle has coordinates and the rightmost top cell of the second rectangle has coordinates .
After the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don’t belong to the ship are marked. On the pictures in the notes such cells are colored green.
Find out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction.
Input:
Four lines contain integers and (, ) — the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can’t rotate the rectangles.
Output
Print exactly one integer — the number of cells, which should be marked after the ship is destroyed.
Sample Input:
2 1 2 1
Sample Output:
12
Sample Input:
2 2 1 2
Sample Output:
16
題目連結
兩個上下相接的矩形求將其包圍的方格數
畫幾個圖數一下規律很顯然
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(int argc, char *argv[]) {
ll w1, h1, w2, h2; cin >> w1 >> h1 >> w2 >> h2;
ll ans = 0;
ans += 2 * ((h1 + h2) + 2) + 2 * (max(w1, w2) + 2);
ans -= 4;
cout << ans << endl;
return 0;
}
B. Draw!
Description:
You still have partial information about the score during the historic football match. You are given a set of pairs , indicating that at some point during the match the score was ": ". It is known that if the current score is «:», then after the goal it will change to “:” or “:”. What is the largest number of times a draw could appear on the scoreboard?
The pairs “:” are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.
Input:
The first line contains a single integer () — the number of known moments in the match.
Each of the next lines contains integers and (), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).
All moments are given in chronological order, that is, sequences and are non-decreasing. The last score denotes the final result of the match.
Output
Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.
Sample Input:
3
2 0
3 1
3 4
Sample Output:
2
Sample Input:
3
0 0
0 0
0 0
Sample Output:
1
Sample Input:
1
5 4
Sample Output:
5
題目連結
遞增的一對數,每次可以對一個數+1,現每輪需達到指定的數對,求兩數相等的最大次數
對每輪遊戲的指定數對手動模擬一下
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(int argc, char *argv[]) {
int n; cin >> n;
vector<pair<ll, ll>> p(n);
for (auto &it : p) cin >> it.first >> it.second;
ll ans = min(p[0].first, p[0].second) + 1;
cerr << ans << endl;
pair<ll, ll> cur = p[0];
for (int i = 1; i < n; ++i) {
if (p[i] == cur) continue;
if (cur.first < cur.second) {
if (p[i].first >= cur.second) {
ans += min(p[i].first, p[i].second) - cur.second + 1;
}
}
else if (cur.second < cur.first) {
if (p[i].second >= cur.first) {
ans += min(p[i].first, p[i].second) - cur.first + 1;
}
}
else if (cur.first == cur.second) {
ans += min(p[i].first, p[i].second) - cur.first;
}
cur = p[i];
cerr << ans << endl;
}
cout << ans << endl;
return 0;
}
C. Birthday
Description:
Cowboy Vlad has a birthday today! There are children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.
Formally, let’s number children from to in a circle order, that is, for every child with number will stand next to the child with number , also the child with number stands next to the child with number . Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.
Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.
Input:
The first line contains a single integer () — the number of the children who came to the cowboy Vlad’s birthday.
The second line contains integers () denoting heights of every child.
Output
Print exactly integers — heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.
If there are multiple possible answers, print any of them.
Sample Input:
5
2 1 1 3 2
Sample Output:
1 2 3 2 1
Sample Input:
3
30 10 20
Sample Output:
10 20 30
題目連結
每個人有個高度(身高),現將他們排成圈,求排列後的最小最大相鄰身高差
一個人的身邊肯定選身高和其相近的人站,所以排序後用雙向佇列左右依次新增(就是把最低的人放中間然後左右依次排)
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int n; cin >> n;
vector<int> a(n);
for (auto &it : a) cin >> it;
sort(a.begin(), a.end());
deque<int> Deq;
bool Flag = true;
for (auto &it : a) {
if (Flag) Deq.push_back(it);
else Deq.push_front(it);
Flag = !Flag;
}
for (auto &it : Deq) cout << it << " ";
return 0;
}
D. Gourmet choice
Description:
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn’t like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer.
Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he’s confused about evaluating dishes.
The gourmet tasted a set of dishes on the first day and a set of dishes on the second day. He made a table of size , in which he described his impressions. If, according to the expert, dish from the first set was better than dish from the second set, then is equal to “>”, in the opposite case is equal to “<”. Dishes also may be equally good, in this case is “=”.
Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if is “<”, then the number assigned to dish from the first set should be less than the number of dish from the second set, if is “>”, then it should be greater, and finally if is “=”, then the numbers should be the same.
Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible.
Input:
The first line contains integers and () — the number of dishes in both days.
Each of the next lines contains a string of symbols. The -th symbol on -th line is . All strings consist only of “<”, “>” and “=”.
Output
The first line of output should contain “Yes”, if it’s possible to do a correct evaluation for all the dishes, or “No” otherwise.
If case an answer exist, on the second line print integers — evaluations of dishes from the first set, and on the third line print integers — evaluations of dishes from the second set.
Sample Input:
3 4
>>>>
>>>>
>>>>
Sample Output:
Yes
2 2 2
1 1 1 1
Sample Input:
3 3
>>>
<<<
>>>
Sample Output:
Yes
3 1 3
2 2 2
Sample Input:
3 2
==
=<
==
Sample Output:
No
題目連結
有兩個數列,給出第一個數列對於第二個數列每個數的大小關係,求兩個數列的可能情況
這道題目首先肯定要先把所有相等的數縮到一個數上(利用並查集),之後按照大小關係排序,從小到大依次填數(利用拓撲排序)就好了
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
int Pre[maxn];
int Find(int Key) {return Pre[Key] == Key ? Key : Pre[Key] = Find(Pre[Key]);}
void Union(int Key1, int Key2) {Pre[Find(Key1)] = Find(Key2);}
int N, M;
string G[maxn];
set<int> R[maxn];
int Deg[maxn];
set<int> Vis;
map<int, int> Ans;
bool Flag;
vector<int> Temp;
void TopoSort() {
queue<int> Que;
for (int i = 0; i < N + M; ++i) {
if (Vis.find(Find(i)) != Vis.end()) continue;
if (Deg[Find(i)] == 0) {
Que.push(Find(i));
Vis.insert(Find(i));
}
}
int Cur = 1;
while (!Que.empty()) {
Temp.clear();
while (!Que.empty()) {
Temp.push_back(Que.front());
Que.pop();
}
for (auto &i : Temp) {
Ans[i] = Cur;
for (auto &j : R[i]) {
if (Vis.find(j) != Vis.end()) continue;
Deg[j]--;
if (Deg[j] == 0) {
Que.push(j);
Vis.insert(j);
}
}
}
Cur++;
}
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
Flag = true;
cin >> N >> M;
for (int i = 0; i < N + M; ++i) Pre[i] = i;
for (int i = 0; i < N; ++i) {
cin >> G[i];
for (int j = 0; j < M; ++j)
if (G[i][j] == '=')
if (Find(i) != Find(N + j))
Union(i, N + j);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (G[i][j] == '=') continue;
int X = Find(i), Y = Find(N + j);
if (X == Y) Flag = false;
if (G[i][j] == '>') swap(X, Y);
if (R[X].find(Y) == R[X].end()) {
R[X].insert(Y);
Deg[Y]++;
}
}
}
if (!Flag) {
cout << "No" << endl;
return 0;
}
TopoSort();
for (int i = 0; i < N + M; ++i)
if (Ans[Find(i)] == 0)
Flag = false;
if (Flag) {
cout << "Yes" << endl;
for (int i = 0; i < N; ++i) cout << Ans[Find(i)] << " ";
cout << endl;
for (int i = N; i < N + M; ++i) cout << Ans[Find(i)] << " ";
cout << endl;
}
else cout << "No" << endl;
return 0;
}
F. Asya And Kittens
Description:
Asya loves animals very much. Recently, she purchased kittens, enumerated them from and and then put them into the cage. The cage consists of one row of cells, enumerated with integers from to from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were partitions originally. Initially, each cell contained exactly one kitten with some number.
Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day , Asya:
- Noticed, that the kittens and , located in neighboring cells want to play together.
- Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.
Since Asya has never putted partitions back, after days the cage contained a single cell, having all kittens.
For every day, Asya remembers numbers of kittens and , who wanted to play together, however she doesn’t remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into cells.
Input:
The first line contains a single integer () — the number of kittens.
Each of the following lines contains integers and (, ) — indices of kittens, which got together due to the border removal on the corresponding day.
It’s guaranteed, that the kittens and were in the different cells before this day.
Output
For every cell from to print a single integer — the index of the kitten from to , who was originally in it.
All printed integers must be distinct.
It’s guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.
Sample Input:
5
1 4
2 5
3 1
4 5
Sample Output:
3 1 4 2 5
題目連結
一串數列,每個數是單獨的一塊,現給出 次並查集操作,求原始數列
顯然把每個並查集用連結串列儲存起來,之後並查集合並的時候進行連結串列合併即可
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int Pre[maxn];
int Find(int Key) {return Pre[Key] == Key ? Key : Pre[Key] = Find(Pre[Key]);}
void Union(int Key1, int Key2) {Pre[Find(Key2)] = Find(Key1);}
int N;
list<int> L[maxn];
int main(int argc, char *argv[]) {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) Pre[i] = i;
for (int i = 1; i <= N; ++i) L[i].push_back(i);
for (int i = 1, U, V; i < N; ++i) {
scanf("%d%d", &U, &V);
L[Find(U)].splice(L[Find(U)].end(), L[Find(V)]);
Union(U, V);
}
for (auto &it : L[Find(1)]) printf("%d ", it);
printf("\n");
return 0;
}
相關文章
- Codeforces Round 940 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round 987 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 986 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 967 (Div. 2)
- Codeforces Round 965 (Div. 2)
- Codeforces Round 963 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 945 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 932 (Div. 2) ABCD
- Codeforces Round 945 (Div. 2) (A - E)
- Codeforces Round 892 (Div. 2) E
- Codeforces Round 936 (Div. 2) E
- 2024.11.15 Codeforces Round 987(Div. 2)