题目链接: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. TCP系列24—重传—14、F-RTO虚假重传探测

    一.虚假重传 在一些情况下,TCP可能会在没有数据丢失的情况下初始化一个重传,这种重传就叫做虚假重传(Spurious retransmission).发生虚假重传的原因可能是包传输中重排序.传输中发 ...

  2. 在windows搭建react

    1.安装必须的软件 1.Python 2    注意勾选 Add python.exe to Path,选项,这样就可以在安装完成后,不用手动去添加环境变量    安装完,打开cmd.exe,输入py ...

  3. linux svn apache

    借助apache运行的svn服务器 一:安装Apache HTTP Server 1.安装Apache HTTP Server yum install httpd httpd-devel -y 2.修 ...

  4. VM新安装centos7无法连接网络的问题

    https://blog.csdn.net/u012110719/article/details/42264601 https://blog.csdn.net/kexiaoling/article/d ...

  5. TScreen 类 - 通过 Screen 更换光标

    //更换窗体或某个控件的光标可以不通过 Screen 对象, 譬如: begin   Self.Cursor := crAppStart;   Panel1.Cursor := crHandPoint ...

  6. windows 2008 iis7 上传大文件限制的真正解决办法

    以前做了一个网站 ,当时本机测试时上传文件大小没有问题,上G也应该可以,可是放在服务器后只能上传小于30M以下文件,当时基本需要也基本在30M以下,就没有管,后在网上发现原来是window2008本身 ...

  7. matlab eval【转】

    Matlab 简单谈谈EVAL函数的用法 EVAL(s)相当于把字符串s的内容作为语句来执行. 比如:eval('a=3*5') 和直接在command 窗口中输入 a=3*5 等效 eval 一个经 ...

  8. bzoj3502[PA2012]Tanie Linie(最大k区间和)

    题意:给定一个长为n的数列,要求选出最多k个不相交的区间(可以不选),使得选中的数字之和最大.(1<=k<=n<=1000000)分析:首先我们通过预处理对问题做一些简化.原序列中的 ...

  9. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  10. 最新发现:Object.defineProperty()让数组的length属性变成只读

    将属性添加到对象, 或修改现有属性的特性---MSDN的解释. 语法 object.defineProperty(object, propertyname, descriptor) object: 必 ...