Codeforces Round #484 (Div. 2)

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.



枚举即可。


#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
char s[maxn];

int main() {
	int n;
	scanf("%d",&n);
	scanf("%s",s+1);
	int flag=1;
	for (int i=1;i<=n;i++) {
		if (s[i]=='1'){
			if ((s[i-1]=='1'&&i!=1)||(s[i+1]=='1'&&i!=n)) flag=0;
		} else {
			if ((s[i-1]=='0'||i==1)&&(s[i+1]=='0'||i==n)) flag=0;
		}
	}
	if (flag) cout << "yes"; else cout << "no";
	return 0;
}
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.



用一个队列和一个栈。

把座位按宽度从小到大推入队列,遇到0时取队列首的座位,退队,并把这个位置加入栈顶;遇到1取栈顶的座位并出栈。


#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=400005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
char s[maxn];

struct node {
	int w,p;
};
node a[maxn];

bool cmp(node a,node b) {
	return a.w st;
	queue q;
	for (i=1;i<=n;i++) {
		q.push(a[i].p);
	}
	for (i=1;i<=n*2;i++) {
		if (s[i]=='0') {
			printf("%d ",q.front());
			st.push(q.front());
			q.pop();
		} else {
			printf("%d ",st.top());
			st.pop();
		}
	}
	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−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.



贪心即可,dfs一遍,从叶子向上DP。

如果总点数是偶数,显然无解。

如果当前dfs到的点的子树的节点数是偶数,那么直接断开这个点和父亲的边。


#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f; 
const ld pi=acos(-1.0L);
int head[maxn],s[maxn];
int num=0,ans=0;
bool visit[maxn];

struct Edge {
	int from,to,pre;
};
Edge edge[maxn*2];

void addedge(int from, int to) {
	edge[num]=(Edge){from,to,head[from]};
	head[from]=num++;
	edge[num]=(Edge){to,from,head[to]};
	head[to]=num++;
}

void dfs(int now) {
	s[now]++;
	visit[now]=1;
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!visit[to]) 
			dfs(to),s[now]+=s[to];
	}
	if (s[now]%2==0) ans++,s[now]=0;
}

int main() {
	num=0;mem0(visit);
	memset(head,-1,sizeof(head));
	int n,i,x,y;
	scanf("%d",&n);
	for (i=1;i
D. Shark
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.

Max is a young biologist. For nn days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer kk that if the shark in some day traveled the distance strictly less than kk, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to kk; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least kk.

The shark never returned to the same location after it has moved from it. Thus, in the sequence of nn days we can find consecutive nonempty segments when the shark traveled the distance less than kk in each of the days: each such segment corresponds to one location. Max wants to choose such kk that the lengths of all such segments are equal.

Find such integer kk, that the number of locations is as large as possible. If there are several such kk, print the smallest one.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of days.

The second line contains nn distinct positive integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the distance traveled in each of the day.

Output

Print a single integer kk, such that

  1. the shark was in each location the same number of days,
  2. the number of locations is maximum possible satisfying the first condition,
  3. kk is smallest possible satisfying the first and second conditions.
Examples
input
Copy
8
1 2 7 3 4 8 5 6
output
Copy
7
input
Copy
6
25 1 2 3 14 36
output
Copy
2
Note

In the first example the shark travels inside a location on days 11 and 22 (first location), then on 44-th and 55-th days (second location), then on 77-th and 88-th days (third location). There are three locations in total.

In the second example the shark only moves inside a location on the 22-nd day, so there is only one location.



把所有距离a1,a2...an从小到大排序。之后,一个个扫描,假设扫描到第 i 个距离的时候,a1到ai这些距离对应的天数都为鲨鱼停留的天数。那么,此时k最小是ai+1。

这时还需要判断这些天数是否连成一些大小相等的块,每个块内的天数是连续的。那么,用c[i] (color) 表示第 i 天属于第几号块,即第 i 天的颜色(把相同块的元素视为同一种颜色),cnt[i]表示此时有几个块的大小是i,s[i]表示第 i 号块的size。每次向右多扫描一个元素,把它和左右两边已经染色的块合并,修改对应的c,cnt,s数组。

如果暴力合并,很有可能被卡时间。这时,如果我们启发式合并,即每次合并两个相邻的块时,选择将size较小的块合并到较大的块,只把这些较少的元素染色,那么最坏情况下,平均每个元素的颜色也不过要修改logn次,总的复杂度是O(nlogn)。

实际上,由于数据随机,最后只跑了46ms.


#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int c[maxn],cnt[maxn],s[maxn];

struct node {
	int w,p;
};
node a[maxn];

bool cmp(node a,node b) {
	return a.ws[c[a[i].p]]) {
					int pos=a[i].p;
					s[c[a[i].p+1]]+=s[c[a[i].p]];
					cnt[s[c[a[i].p]]]--;cnt[s[c[a[i].p+1]]+s[c[a[i].p]]]++;
					int q=c[pos];
					c[a[i].p]=c[a[i].p+1];
					while (pos>1&&c[pos-1]==q) {
						pos--;
						c[pos]=c[pos+1];
					}
				} else {
					int pos=a[i].p+1;
					s[c[a[i].p]]+=s[c[a[i].p+1]];
					cnt[s[c[a[i].p+1]]]--;cnt[s[c[a[i].p+1]]+s[c[a[i].p]]]++;
					c[a[i].p+1]=c[a[i].p];
					int q=c[pos];
					while (pos


你可能感兴趣的:(Codeforces)