Codeforces Round #484 (Div. 2)

题目链接:http://codeforces.com/contest/982

A. Row
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

You're given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold:

  1. There are no neighbors adjacent to anyone seated.
  2. It's impossible to seat one more person without violating the first rule.

The seating is given as a string consisting of zeros and ones (00 means that the corresponding seat is empty, 11 — occupied). The goal is to determine whether this seating is "maximal".

Note that the first and last seats are not adjacent (if n2n≠2).

Input

The first line contains a single integer nn (1n10001≤n≤1000) — the number of chairs.

The next line contains a string of nn characters, each of them is either zero or one, describing the seating.

Output

Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".

You are allowed to print letters in whatever case you'd like (uppercase or lowercase).

Examples
Input
Copy
3
101
Output
Copy
Yes
Input
Copy
4
1011
Output
Copy
No
Input
Copy
5
10001
Output
Copy
No
Note

In sample case one the given seating is maximal.

In sample case two the person at chair three has a neighbour to the right.

In sample case three it is possible to seat yet another person into chair three.

题意:给你n张凳子,0表示空的,1表示有人。然后让你判断当前位置是否是最大的合法安排方法。其中合法指1的左右都要是0。

思路:模拟题,判断是否有两个1相邻(合法性),是否有三个0相邻(最大性),不过对于两端的0要注意最左端的0只要右边的是0那么就不是最大的,最右端的同理。

代码实现如下:

 1 #include <cstdio>
 2 #include <string>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 #define IO ios::sync_with_stdio(false);
 8 int n;
 9 string s;
10 
11 int main() {
12     IO;
13     cin >>n >>s;
14     if(n == 1) {
15         if(s[0] == '0') {
16             cout <<"No" <<endl;
17             return 0;
18         }
19     }
20     int flag = 1;
21     for(int i = 0; i < n; i++) {
22         if(s[i] == '1' && (i + 1) < n) {
23             if(s[i+1] == '1') {
24                 flag = 0;
25                 break;
26             }
27         }
28     }
29     for(int i = 0; i < n; i++) {
30         if(i == 0 && s[i] == '0' && s[i + 1] == '0') {
31             flag = 0;
32             break;
33         } else if(i == n - 2 && s[i] == '0' && s[i + 1] == '0') {
34             flag = 0;
35             break;
36         } else if(s[i] == '0' && s[i+1] == '0' && s[i+2] == '0') {
37             flag = 0;
38             break;
39         }
40     }
41     if(flag) cout <<"Yes" <<endl;
42     else cout <<"No" <<endl;
43     return 0;
44 }
B. Bus of Characters
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

In the Bus of Characters there are nn rows of seat, each having 22 seats. The width of both seats in the ii -th row is wiwi centimeters. All integers wiwi are distinct.

Initially the bus is empty. On each of 2n2n stops one passenger enters the bus. There are two types of passengers:

  • an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it;
  • an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it.

You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.

Input

The first line contains a single integer nn (1n2000001≤n≤200000 ) — the number of rows in the bus.

The second line contains the sequence of integers w1,w2,,wnw1,w2,…,wn (1wi1091≤wi≤109 ), where wiwi is the width of each of the seats in the ii -th row. It is guaranteed that all wiwi are distinct.

The third line contains a string of length 2n2n , consisting of digits '0' and '1' — the description of the order the passengers enter the bus. If the jj -th character is '0', then the passenger that enters the bus on the jj -th stop is an introvert. If the jj -th character is '1', the the passenger that enters the bus on the jj -th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal nn ), and for each extrovert there always is a suitable row.

Output

Print 2n2n integers — the rows the passengers will take. The order of passengers should be the same as in input.

Examples
Input
Copy
2
3 1
0011
Output
Copy
2 1 1 2 
Input
Copy
6
10 8 9 11 13 5
010010011101
Output
Copy
6 6 2 3 3 1 4 4 1 2 5 5 
Note

In the first example the first passenger (introvert) chooses the row 22 , because it has the seats with smallest width. The second passenger (introvert) chooses the row 11 , because it is the only empty row now. The third passenger (extrovert) chooses the row 11 , because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 22 , because it is the only row with an empty place.

题意:给你n排位置,每排2个位置,宽度为wi,再给你2*n个乘客,0表示内向,1表示外向。内向的乘客喜欢坐目前没有任何一人坐的那排,且尽量坐w最小的位置,而外向的乘客刚好相反(不过,外向的人只能和内向的人坐一起),按顺序输出每个乘客乘坐的排数。

思路:首先将位置按照w进行排序,越小的越靠前,此时内向的人所乘坐顺序就是这个顺序,而对于外向的人用优先队列来维护顺序,在确定一个内向的人所乘坐的位置时,将该排位置放入优先队列中。

代码实现如下:

 1 #include <queue>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 #define IO ios::sync_with_stdio(false),cin.tie(0);
10 const int maxn = 2e5 + 7;
11 int n;
12 string s;
13 int a[maxn], b[2 * maxn];
14 
15 struct node {
16     int id;
17     int w;
18 }p[maxn], nw;
19 
20 struct cmp {
21     bool operator() (const node& x, const node& y) {
22         return x.w < y.w;
23     }
24 };
25 
26 bool cmp2(const node& x, const node& y) {
27     return x.w < y.w;
28 }
29 
30 int main() {
31     IO;
32     cin >>n;
33     for(int i = 1; i <= n; i++) {
34         cin >>p[i].w;
35         p[i].id = i;
36     }
37     cin >>s;
38     int t = 1;
39     sort(p+1, p+n+1, cmp2);
40     priority_queue<node, vector<node>, cmp> q;
41     for(int i = 0; i < 2 * n; i++) {
42         if(s[i] == '0') {
43             b[i] = p[t].id;
44             q.push(p[t]);
45             t++;
46         } else {
47             nw = q.top(), q.pop();
48             b[i] = nw.id;
49         }
50     }
51     int flag = 0;
52     for(int i = 0; i < 2 * n; i++) {
53         if(flag) cout <<" ";
54         cout <<b[i];
55         flag = 1;
56     }
57     cout <<endl;
58     return 0;
59 }
C. Cut 'em all!
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

You're given a tree with nn vertices.

Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

Input

The first line contains an integer nn (1n1051≤n≤105) denoting the size of the tree.

The next n1n−1 lines contain two integers uu, vv (1u,vn1≤u,v≤n) each, describing the vertices connected by the ii-th edge.

It's guaranteed that the given edges form a tree.

Output

Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or 1−1 if it is impossible to remove edges in order to satisfy this property.

Examples
Input
Copy
4
2 4
4 1
3 1
Output
Copy
1
Input
Copy
3
1 2
1 3
Output
Copy
-1
Input
Copy
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
Output
Copy
4
Input
Copy
2
1 2
Output
Copy
0
Note

In the first example you can remove the edge between vertices 11 and 44. The graph after that will have two connected components with two vertices in each.

In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is 1−1.

题意:给你一棵,有n个节点,n-1条边,问你最多能删多少条边使得新的森林每棵树都有偶数个节点。

思路:首先,易知当n为奇数的时候,无论如何删都是不可能符合题意的,故输出-1.当n为偶数的时候,则用dfs来进行处理,当子树有偶数个节点时就将当前节点与其子树断开(使删的边数最大),ans++。子树节点数为奇数时则将子树节点数和当前节点数相加。

代码实现如下:

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 1e5 + 7;
 7 int n, u, v, ans;
 8 int ed[maxn];
 9 vector<int> G[maxn];
10 
11 void dfs(int u, int p) {
12     ed[u] = 1;
13     int t = G[u].size();
14     for(int i = 0; i < t; i++) {
15         int v = G[u][i];
16         if(v != p) {
17             dfs(v, u);
18             if(ed[v] % 2 == 0 && ed[v] > 0) ans++;
19             else ed[u] += ed[v];
20         }
21     }
22 }
23 
24 int main() {
25     scanf("%d", &n);
26     for(int i = 1; i < n; i++) {
27         scanf("%d%d", &u, &v);
28         G[u].push_back(v);
29         G[v].push_back(u);
30     }
31     if(n & 1) {
32         printf("-1\n");
33     } else {
34         ans = 0;
35         dfs(1, 0);
36         printf("%d\n", ans);
37     }
38     return 0;
39 }

猜你喜欢

转载自www.cnblogs.com/Dillonh/p/9057210.html