题目链接: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. OSG学习:转动的小汽车示例

    由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成. 代码如下: #include <osg\ShapeDrawable> #include <osg\Animat ...

  2. 火狐浏览器(FireFox)安装Flash插件失败处理方法

    最近不知道怎么了,总是嫌弃IE,可能是被网络流量监测的网址给搞得了,弄了火狐浏览器,也安装了猎豹,这里不对浏览器做评价 好多朋友安装好火狐(FireFox)的时候发现之前不是有装IE的Flash播放插 ...

  3. Ajax在jQuery中的应用($.ajax()方法)

    Ajax() 方法 $.ajax() 中的参数及使用方法 在jQuery中,$.ajax() 是最底层的方法,也是功能最强的方法.$.get().$.post().$.getScript().getJ ...

  4. CF#508 1038E Maximum Matching

    ---题面--- 题解: 感觉还是比较妙的,复杂度看上去很高(其实也很高),但是因为n只有100,所以还是可以过的. 考虑一个很暴力的状态f[i][j][x][y]表示考虑取区间i ~ j的方格,左右 ...

  5. 字符串构造的dp 【bzoj1009 &bzoj1030】

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 4305  Solved: 2637 [Submit][Sta ...

  6. 使用py-faster-rcnn训练VOC2007数据集时遇到问题

    使用py-faster-rcnn训练VOC2007数据集时遇到如下问题: 1. KeyError: 'chair' File "/home/sai/py-faster-rcnn/tools/ ...

  7. 一维的Haar小波变换

    小波变换的基本思想是用一组小波函数或者基函数表示一个函数或者信号,例如图像信号.为了理解什么是小波变换,下面用一个具体的例子来说明小波变换的过程. 1. 求有限信号的均值和差值 [例] 假设有一幅分辨 ...

  8. #define用法详解

    1.#define 的作用 在C或C++语言源程序中允许用一个标识符来表示一个字符串,称为“宏”.被定义为“宏”的标识符称为“宏名”.在编译预处理时,对程序中所有出现的“宏名”,都用宏定义中的字符串去 ...

  9. 解决 cmd dos 下 无法显示中文

    在做程序开发的时候经常需要在使用命令行进行操作, dos环境本身是不支持中文的,有时候中文编码的问题就像苍蝇一样讨厌,下面提供几种常用的手段解决win7环境下中文显示乱码的问题: 方法一: 修改注册表 ...

  10. C++虚基类的初始化

    #include<iostream> using namespace std; class Base{ public: Base(int sa) { a=sa; cout<<& ...