# Codeforces Round #612

### Garland

#### 代码

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define Nmax 120
int ls[Nmax];
int N;
int cnt[2];
vector<pair<int, int> > sm[2];
int main()
{
scanf("%d", &N);
cnt[0] = N/2;
cnt[1] = N - cnt[0];
for (int i = 1; i <= N; i++)
{
scanf("%d", &ls[i]);
if (ls[i] == 0)
{
ls[i] = -1;
}
else
{
ls[i] %= 2;
cnt[ls[i]]--;
}
}
if (cnt[0] + cnt[1] == N)
{
if (cnt[0] && cnt[1])
{
printf("1\n");
}
else
{
printf("0\n");
}
return 0;
}
for (int i = 1; i <= N; i++)
{
if (ls[i] != -1)
{
ls[0] = ls[i];
break;
}
}
for (int i = N; i >= 1; i--)
{
if (ls[i] != -1)
{
ls[N+1] = ls[i];
break;
}
}
int k;
int ans = 0;
int lst = ls[0];
int ct = 0;
for (int i = 1; i <= N+1; i++)
{
if (i == 1 || i == N+1)
{
k = 1;
}
if (ls[i] == -1)
{
ct++;
}
else
{
if (lst == ls[i]) {
sm[lst].push_back(make_pair(k, ct));
k = 0;
ct = 0;
}
else {
ans++;
k = 0;
ct = 0;
lst = ls[i];
}
}
}
sort(sm[0].begin(), sm[0].end());
sort(sm[1].begin(), sm[1].end());
for (int j = 0; j < 2; j++) {
for (int i = 0; i < sm[j].size(); i++) {
if (sm[j][i].second <= cnt[j]) {
cnt[j] -= sm[j][i].second;
}
else {
if (sm[j][i].first == 0) {
ans += 2;
}
else {
ans += 1;
}
}
}
}
printf("%d\n", ans);
return 0;
}


### Numbers on Tree

#### 代码

#include <cstdio>
#include <list>
#include <vector>
using namespace std;
#define Nmax 2020
list<int> ls;
int N;
vector<int> tr[Nmax];
int C[Nmax];
int A[Nmax];
bool flag = false;
void DFS(int x, list<int>::iterator oit) {
list<int>::iterator it;
it = oit;
for (int i = 0; i < tr[x].size(); i++)
{
DFS(tr[x][i], it);
if (flag)
return;
it = ls.end();
it--;
}
oit++;
for (int i = 0; i < C[x]; i++) {
if (oit == ls.end()) {
flag = true;
return;
}
else {
oit++;
}
}
ls.insert(oit, x);
}
int main() {
ls.push_back(0);
list<int>::iterator it;
it = ls.begin();
scanf("%d", &N);
int p, c;
for (int i = 1; i <= N; i++)
{
scanf("%d %d", &p, &C[i]);
tr[p].push_back(i);
}
DFS(tr[0][0], it);
if (flag) {
printf("NO\n");
}
else {
printf("YES\n");
int i = 1;
for (it++; it != ls.end(); it++) {
A[*it] = i++;
}
for (int i = 1; i <= N; i++) {
printf("%d ", A[i]);
}
}
return 0;
}


Codeforces Round #612

(0)
(0)