# Codeforces Global Round 16(A - E)

### A. Median Maximization

The simple greedy construction question is intended to find the maximum median. If all the numbers in the known sequence are non negative, we can make all the numbers before the median zero, and then the median can get the maximum total divided by the remaining numbers and rounded down
code

```#include <bits/stdc++.h>

using namespace std;

int n, s;

void solve()
{
cin >> n >> s;
if (n % 2 == 1)
{
int m = n - n / 2;
cout << s / m << endl;
}
else
{
int m = n - (n / 2 - 1);
cout << s / m << endl;
}
}

int main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int T;
cin >> T;

while (T -- )
{
solve();
}

return 0;
}
```

### B. MIN-MEX Cut

The greedy question still means that a given string can be divided into several small strings, in which the value of each string is mex (the number contained in the current string). For 01, we can get 2, for 0, we can get 1, for 1, we can get 0, and the minimum value and
Therefore, we can see how many consecutive substrings contain only 0. If the number is greater than 2, we can directly take the value of the whole string as 2. If it is less than 2, we can take the number of consecutive 0 blocks

```#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

string s;

void solve()
{
cin >> s;

int tot = 0;
for (int i = 1; i < s.size(); i ++ )
if (s[i] == '1' && s[i] != s[i - 1]) tot ++ ;
if (s[s.size() - 1] == '0') tot ++ ;
if (tot < 2) cout << tot << endl;
else cout << 2 << endl;
}

int main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int T;
cin >> T;

while (T -- )
{
solve();
}

return 0;
}
```

### C - MAX-MEX Cut

Almost the same as b. given that the two lines of string mex are the mex of all 01 in the upper and lower lines of [l, r], let's divide it into several sub strings to find the maximum value sum
For the simple construction question, we can choose to keep the top and bottom 0 (represented by a0 b0) to see whether it is a1 b1. If so, we can use these two lines to get 2. If it is a0 b0, we can choose ans plus a0 b0 (value is 1), and then see whether there is a1 b1 after the current line. If a0 b1 or a1 b0 is encountered, we can directly add 2
code

```#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n;
string a, b;

void solve()
{
cin >> n;
cin >> a >> b;
int t1 = -1, t2 = -1; // t1 is the front row and t2 is the rear row

int ans = 0;
for (int i = 0; i < n; i ++ )
{
if (a[i] != b[i]) t2 = 2;
else if (a[i] == '1') t2 = 0;
else t2 = 1;

if (t2 == 2)
{
if (t1 != -1) ans += t1;
ans += t2;
t1 = t2 = -1;
}
else if (t1 != -1 && t1 ^ t2)
{
ans += 2;
t1 = t2 = -1;
}
else if (t1 != -1 && t1 == t2)
{
if (t1 == 1) ans += t1, t1 = 1;
t1 = -1;
}
t1 = t2;
}
if (t1 != -1) ans += t1;
cout << ans << endl;
}

int main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int T;
cin >> T;

while (T -- )
{
solve();
}

return 0;
}
```

### D1. Seating Arrangements (easy version)

Reading comprehension questions feel a little brain disabled
The idea is very simple. There is only a group of people. Everyone should be sorted according to their eyesight. After sorting, if there is a serial number in front that is less than his value, add one to find the minimum value sum
code

```#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 10;

int n, m;
int a[N];

void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; i ++ ) cin >> a[i];
int ans = 0;
for (int i = 1; i <= m; i ++ )
for (int j = 1; j < i; j ++ )
if (a[j] < a[i]) ans ++ ;
cout << ans << endl;
}

signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int T;
cin >> T;

while (T -- )
{
solve();
}

return 0;
}
```

### D2. Seating Arrangements (hard version)

The difficulty increasing version of D1 has n lines. When each person passes through the front person, ans should be sorted by visual acuity. Finally, the sequence of the current visual acuity is certain. In order to reduce the value and need to be optimized through certain sorting, if the eyesight of the last person in line i and the first person in line i+1 are equal, it is obvious that the queue number of the last person in line i is small It can make the value sum smaller. For the ith person and the i + 1st person in the same row, it is obvious that when the queue number of the ith person is larger, it can make the value sum smaller
Then we establish two sorts to sort the total sequence and the sequence of each row to find ans
code

```#include <bits/stdc++.h>

using namespace std;

const int N = 350;

int n, m;
struct Node
{
int value, idx;
}a[N * N];

bool cmp1(Node x, Node y)
{
if (x.value == y.value) return x.idx < y.idx;
return x.value < y.value;
}

bool cmp2(Node x, Node y)
{
if (x.value == y.value) return x.idx > y.idx;
return x.value < y.value;
}

void solve()
{
cin >> n >> m;

for (int i = 1; i <= n * m; i ++ )
{
cin >> a[i].value;
a[i].idx = i;
}

sort(a + 1, a + 1 + n * m, cmp1);

int ans = 0;
for (int i = 1; i <= n; i ++ )
{
sort(a + 1 + (i - 1) * m, a + 1 + i * m, cmp2);
int l = (i - 1) * m + 1, r = i * m;
for (int j = l; j <= r; j ++ )
for (int k = l; k < j; k ++ )
if (a[j].idx > a[k].idx) ans ++ ;
}

cout << ans << endl;
}

int main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int t;
cin >> t;

while (t -- )
{
solve();
}

return 0;
}
```

### E. Buds Re-hanging

Make each flower bud a child node, only contain leaf nodes, and the number of child nodes is greater than 1. Then you can move the flower bud node to other leaf nodes. What is the minimum number of leaf nodes after several moves
In this problem, we can separate all flower buds and nodes that can become flower buds and leaf nodes containing them, and then combine them randomly. Each combination will make ans-1 find from bottom to top, and then add them up to be the optimal solution
code

```#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10, M = N * 2;

int n, ans;
int h[N], e[M], ne[M], idx;

void init()
{
memset(h, -1, sizeof h);
idx = 0, ans = 1;
}

{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

int dfs(int cur, int fa)
{
int cnt = 0;
for (int i = h[cur]; ~i; i = ne[i])
{
int j = e[i];
if (j == fa) continue;
cnt += dfs(j, cur);
}

if (cnt)
{
ans += (cnt - 1);
return 0;
}
return 1;
}

void solve()
{
init();
cin >> n;

for (int i = 1; i < n; i ++ )
{
int u, v;
cin >> u >> v;
}

dfs(1, 0);

cout << ans << endl;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);

int T;
cin >> T;

while (T -- )
{
solve();
}

return 0;
}
```

At the beginning of writing the problem solution, so sometimes the expression of ideas is not very clear. Push the electric sound shaking leg of station b. It can't be changed. It will be explained almost every time