Codeforces Round #484

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.

/**
 * 有两个规则,首先不能违反第一个规则;就是相邻的位置上不能有“邻居"(邻居就是一样的意思):
 * 第二个就是如果不违反第一条的话就不可能坐更多的人(其实就是相隔两个以上空位可以坐一个)
 * 然后要求的是"maximal",意思就是在符合条件下坐最多的人。
 */
import java.util.*;
public class A {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int n = in.nextInt();
			String str = in.next();
			if (n == 1) {
				if (str.equals("1")) System.out.println("Yes");
				else System.out.println("No");
				continue;
			}
			else {
				int flag = 1;
				for (int i = 0; i < str.length(); i++) {
					if (i >= 1 && str.charAt(i) == '1' && str.charAt(i - 1) == '1') {
						flag = 0;
					}
					if (i > 1) {
						if (str.charAt(i) == '0' && str.charAt(i - 1) == '0' && str.charAt(i - 2) == '0') {
							flag = 0;
						}
					}
					if (str.charAt(0) == '0' && str.charAt(1) == '0') {
						flag = 0;
					}
					if (str.charAt(str.length() - 2) == '0' && str.charAt(str.length() - 1) == '0') {
						flag = 0;
					}
				}
				if (flag == 1) System.out.println("Yes");
				else System.out.println("No");
			}
		}
	}
}
B. Bus of Characters
time limit per test
2 seconds
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.


这题Java 20000那个点超时也不知道擦回事,然后用c++ 写了一发,过了;

总的思路就是创建一个大顶堆,可以用优先队列来用,然后每次如果那个人是内向的人,那么就从现有的两个位置都空的row中取长度最小的放入堆中,如果那个是外向的人, 那么就从堆中取堆顶,pop,就行;;;


#include <iostream>
#include <cstdio>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <map>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int mod = 10000;
const int maxn = 200000 + 10;
const int INF = 0x3f3f3f3f;
typedef long long ll;

struct node {
    int v;
    int id;
    node(){}
    node(int _v, int _id){v = _v; id = _id;}
    bool operator<(const node &a)const{
        return v < a.v;
    }
};
int w[maxn], result[maxn*2 + 10];
map<int, int> mp;
priority_queue<node> que;
int main() {
    memset(w, 0, sizeof(w));
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        int k; cin >> k;
        w[i] = k;
        mp[k] = i;
    }
    int move = 0;
    sort(w + 1, w + 1 + n);
    string str;
    cin >> str;
    int l = 1, r = n;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '0') {
            int mmax = w[l]; l++;
            int id = mp[mmax];
            que.push(node(mmax, id));
            result[move++] = id;
        }
        else {
            node q = que.top(); que.pop();
            result[move++] = q.id;
        }
    }
    for (int i = 0; i < move; i++) {
        if (i == 0) cout << result[i];
        else cout << " " << result[i];
    }
    return 0;
}

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 uuvv (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−1if 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是奇数那么直接输出-1;
 * 如果是偶数:
 * 要求的是去掉尽可能多的边使得剩下的每一个集合中 的顶点数都是偶数。
 * 然后就dfs,每次在回溯的时候把子树集合点数是偶数的话就要 去掉一次边,进行ans++,
 * 最后输出ans就行
 */
import java.util.*;
public class C {
	static int maxn = 100000 + 10;
	static ArrayList<Integer>[] list;
	static HashMap<Integer, Integer> map;
	static int[] sz = new int[maxn];
	static int ans = 0;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			list = new ArrayList[100000 + 10];
			map = new HashMap<Integer, Integer>();
			Arrays.fill(sz, 0);
			
			int n = in.nextInt();
			
			for (int i = 0; i < n - 1; i++) {
				int a = in.nextInt();
				int b = in.nextInt();
				if (map.containsKey(a)) {
					list[a].add(b);
				}
				else {
					list[a] = new ArrayList<Integer>();
					map.put(a, 1);
					list[a].add(b);
				}
				if (map.containsKey(b)) {
					list[b].add(a);
				}
				else {
					list[b] = new ArrayList<Integer>();
					map.put(b, 1);
					list[b].add(a);
				}
			}
			if (n%2 != 0) System.out.println("-1");
			else {
				ans = 0;
				dfs(1, -1);
				System.out.println(ans);
			}
		}
	}
	private static void dfs(int u, int f) {
		sz[u] = 1;
		for (int v : list[u]) {
			if (v == f) continue;
			dfs(v, u);
			sz[u] += sz[v];
			if (sz[v]%2 == 0) ans++;
		}
	}
}


猜你喜欢

转载自blog.csdn.net/qq_34649947/article/details/80372879