题目链接: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. iOS开发SDWebImage源码解析之SDWebImageManager的注解

    最近看了两篇博客,写得很不错,关于SDWebImage源码解析之SDWebImageManager的注解: 1.http://www.jianshu.com/p/6ae6f99b6c4c 2.http ...

  2. TCP系列31—窗口管理&流控—5、TCP流控与滑窗

    一.TCP流控 之前我们介绍过TCP是基于窗口的流量控制,在TCP的发送端会维持一个发送窗口,我们假设发送窗口的大小为N比特,网络环回时延为RTT,那么在网络状况良好没有发生拥塞的情况下,发送端每个R ...

  3. JSP传递数组给JS的方法

    由于JSP页面的数组无法直接传到JS.所以采用以下方法来获取数组. <% String[] title = { "姓名 ", "学号 ", "性 ...

  4. RHEL 6.4(i386)安装MySQL 5.6的方法

  5. DEDE去掉会员登录及注册验证码的方法

    1.登录打开member/index_do.php 删除245-250行,即: if(strtolower($vdcode)!=$svali || $svali=='') { ResetVdValue ...

  6. docker配置网络

    1.暂停服务,删除旧网桥#service docker stop#ip link set dev docker0 down#brctl delbr docker0 2.创建新网桥bridge0#brc ...

  7. leetcode 整理

    1.Two Sum 构造Comparator,KSum 这一类的问题最基本的一题, 解法: 先sort,然后双指针,头尾各一个.进行加逼找值. 对于其余的KSum最终是降次到2次. 如3Sum固定一个 ...

  8. udp->ip & tcp->ip 发送数据包的目的地址的源地址是什么时候确定的?

    udp->ip & tcp->ip udp到ip层是:ip_send_skb tcp到ip层是: ip_queue_xmit 拿tcp为例,在使用[ip_queue_xmit, i ...

  9. 前端基础:CSS样式选择器

    前端基础:CSS样式选择器 CSS概述 CSS是Cascading Style Sheets的简称,中文意思是:层叠样式表,对html标签的渲染和布局.CSS规则由两个主要的部分组成:1.选择器:2. ...

  10. C# 面向对象——多态

    多态分三种:1.虚方法 2.抽象类 3.接口 1.虚方法1.将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. 如: class Program { static ...