题目链接: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 n≠2n≠2).

Input

The first line contains a single integer nn (1≤n≤10001≤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那么就不是最大的,最右端的同理。

代码实现如下:

 #include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std; #define IO ios::sync_with_stdio(false);
int n;
string s; int main() {
IO;
cin >>n >>s;
if(n == ) {
if(s[] == '') {
cout <<"No" <<endl;
return ;
}
}
int flag = ;
for(int i = ; i < n; i++) {
if(s[i] == '' && (i + ) < n) {
if(s[i+] == '') {
flag = ;
break;
}
}
}
for(int i = ; i < n; i++) {
if(i == && s[i] == '' && s[i + ] == '') {
flag = ;
break;
} else if(i == n - && s[i] == '' && s[i + ] == '') {
flag = ;
break;
} else if(s[i] == '' && s[i+] == '' && s[i+] == '') {
flag = ;
break;
}
}
if(flag) cout <<"Yes" <<endl;
else cout <<"No" <<endl;
return ;
}
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 (1≤n≤2000001≤n≤200000 ) — the number of rows in the bus.

The second line contains the sequence of integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1091≤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进行排序,越小的越靠前,此时内向的人所乘坐顺序就是这个顺序,而对于外向的人用优先队列来维护顺序,在确定一个内向的人所乘坐的位置时,将该排位置放入优先队列中。

代码实现如下:

 #include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define IO ios::sync_with_stdio(false),cin.tie(0);
const int maxn = 2e5 + ;
int n;
string s;
int a[maxn], b[ * maxn]; struct node {
int id;
int w;
}p[maxn], nw; struct cmp {
bool operator() (const node& x, const node& y) {
return x.w < y.w;
}
}; bool cmp2(const node& x, const node& y) {
return x.w < y.w;
} int main() {
IO;
cin >>n;
for(int i = ; i <= n; i++) {
cin >>p[i].w;
p[i].id = i;
}
cin >>s;
int t = ;
sort(p+, p+n+, cmp2);
priority_queue<node, vector<node>, cmp> q;
for(int i = ; i < * n; i++) {
if(s[i] == '') {
b[i] = p[t].id;
q.push(p[t]);
t++;
} else {
nw = q.top(), q.pop();
b[i] = nw.id;
}
}
int flag = ;
for(int i = ; i < * n; i++) {
if(flag) cout <<" ";
cout <<b[i];
flag = ;
}
cout <<endl;
return ;
}
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 (1≤n≤1051≤n≤105) denoting the size of the tree.

The next n−1n−1 lines contain two integers uu, vv (1≤u,v≤n1≤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++。子树节点数为奇数时则将子树节点数和当前节点数相加。

代码实现如下:

 #include <cstdio>
#include <vector>
#include <cstring>
using namespace std; const int maxn = 1e5 + ;
int n, u, v, ans;
int ed[maxn];
vector<int> G[maxn]; void dfs(int u, int p) {
ed[u] = ;
int t = G[u].size();
for(int i = ; i < t; i++) {
int v = G[u][i];
if(v != p) {
dfs(v, u);
if(ed[v] % == && ed[v] > ) ans++;
else ed[u] += ed[v];
}
}
} int main() {
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(n & ) {
printf("-1\n");
} else {
ans = ;
dfs(, );
printf("%d\n", ans);
}
return ;
}

Codeforces Round #484 (Div. 2)的更多相关文章

  1. Codeforces Codeforces Round #484 (Div. 2) E. Billiard

    Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...

  2. Codeforces Codeforces Round #484 (Div. 2) D. Shark

    Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...

  3. Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B

    原博主:https://blog.csdn.net/amovement/article/details/80358962 B. Bus of Characters time limit per tes ...

  4. Codeforces Round #484 (Div. 2)Cut 'em all!(dfs)

    题目链接 题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小. 思路:考虑dfs,从1出发,若当前节点的子节点和自己的数目是偶数,说明当前节点和父亲节点的边是可以删除的,答案+1 ...

  5. 【数论】【扩展欧几里得】Codeforces Round #484 (Div. 2) E. Billiard

    题意:给你一个台球桌面,一个台球的初始位置和初始速度方向(只可能平行坐标轴或者与坐标轴成45度角),问你能否滚进桌子四个角落的洞里,如果能,滚进的是哪个洞. 如果速度方向平行坐标轴,只需分类讨论,看它 ...

  6. 【set】【multiset】Codeforces Round #484 (Div. 2) D. Shark

    题意:给你一个序列,让你找一个k,倘若把大于等于k的元素都标记为不可用,那么剩下的所有元素形成的段的长度相同,并且使得段的数量尽量大.如果有多解,输出k尽量小的. 把元素从大到小排序插回原位置,用一个 ...

  7. 【推导】Codeforces Round #484 (Div. 2) C. Cut 'em all!

    题意:给你一棵树,让你切掉尽可能多的边,使得产生的所有连通块都有偶数个结点. 对于一棵子树,如果它有奇数个结点,你再从里面怎么抠掉偶数结点的连通块,它都不会变得合法.如果它本来就有偶数个结点,那么你怎 ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. ACM 第七天

    水题 B - Minimum’s Revenge There is a graph of n vertices which are indexed from 1 to n. For any pair ...

  2. IO Model- 同步,异步,阻塞,非阻塞

    同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出 ...

  3. 【bzoj4196】[Noi2015]软件包管理器 树链剖分+线段树

    题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个 ...

  4. [洛谷P1452]Beauty Contest

    题目大意:给你$n$个点,求出其中最远点的距离 题解:求出凸包,最远点一定都在凸包上,可以对每条边求出最远的点(可以双指针),然后求出和这条边的端点的距离,更新答案 卡点:最开始对每个点求出最远点,但 ...

  5. [BZOJ2961] 共点圆 [cdq分治+凸包]

    题面 BZOJ传送门 思路 首先考虑一个点$(x_0,y_0)$什么时候在一个圆$(x_1,y_1,\sqrt{x_1^2+y_1^2})$内 显然有:$x_1^2+y_1^2\geq (x_0-x_ ...

  6. HDU 1002 (高精度加法运算)

    A + B ProblemII Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Vue.js中的常用的指令缩写

    Vue.js为两个最为常用的指令提供了特别的缩写: v-bind缩写 <!--完整语法--> <a v-bind:href="url">测试</a&g ...

  8. STL之一:字符串用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8539471 字符串是程序设计中最复杂的变成内容之一.STL string类提供了强大 ...

  9. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. POJ 3111 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10507   Accepted: 2709 Case Time ...