A. Union of Doubly Linked Lists

记录进步历程20170924

A. Union of Doubly Linked Lists

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Doublylinked list is one of the fundamental data structures. A doubly linked list isa sequence of elements, each containing information about the previous and thenext elements of the list. In this problem all lists have linear structure.I.e. each element except the first has exactly one previous element, eachelement except the last has exactly one next element. The list is not closed ina cycle.

In thisproblem you are given n memorycells forming one or more doubly linked lists. Each cell contains informationabout element from some list. Memory cells are numbered from 1 to n.

For eachcell i youare given two values:

·        li — cellcontaining previous element for the element in the cell i;

·        ri — cellcontaining next element for the element in the cell i.

Ifcell i containsinformation about the element which has no previous element then li = 0. Similarly,if cell i containsinformation about the element which has no next element then ri = 0.

Three lists are shown on the picture.

For example,for the picture above the values of l and r arethe following: l1 = 4r1 = 7l2 = 5r2 = 0l3 = 0r3 = 0l4 = 6r4 = 1l5 = 0r5 = 2l6 = 0r6 = 4l7 = 1r7 = 0.

Your task isto unite all given lists in a single list, joining them to each other in anyorder. In particular, if the input data already contains a single list, thenthere is no need to perform any actions. Print the resulting list in the formof values liri.

Any otheraction, other than joining the beginning of one list to the end of another, cannot be performed.

Input

The firstline contains a single integer n (1 ≤ n ≤ 100) — thenumber of memory cells where the doubly linked lists are located.

Each of thefollowing n linescontains two integers liri (0 ≤ li, ri ≤ n) — thecells of the previous and the next element of list for cell i.Value li = 0 ifelement in cell i has noprevious element in its list. Value ri = 0 ifelement in cell i has nonext element in its list.

It isguaranteed that the input contains the correct description of a single or moredoubly linked lists. All lists have linear structure: each element of listexcept the first has exactly one previous element; each element of list exceptthe last has exactly one next element. Each memory cell contains informationabout one element from some list, each element of each list written in oneof n given cells.

Output

Print n lines,the i-th linemust contain two integers li and ri — thecells of the previous and the next element of list for cell i afterall lists from the input are united in a single list. If there are manysolutions print any of them.

Example

input

7 4 7 5 0 0 0 6 1 0 2 0 4 1 0

output

4 7 5 6 0 5 6 1 3 2 2 4 1 0

 

 

 

 

 

 

 

 

A. Quasi-palindrome

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let quasi-palindromic numberbe such number that adding some leading zeros (possible none) to it produces apalindromic string.

String t iscalled a palindrome, if it reads the same from left to right and from right toleft.

For example,numbers 131 and 2010200 are quasi-palindromic,they can be transformed to strings "131" and"002010200", respectively, which arepalindromes.

You aregiven some integer number x. Check ifit's a quasi-palindromic number.

Input

The firstline contains one integer number x (1 ≤ x ≤ 109). This numberis given without any leading zeroes.

Output

Print "YES" ifnumber x is quasi-palindromic.Otherwise, print "NO" (without quotes).

Examples

input

131

output

YES

input

320

output

NO

input

2010200

output

YES

 

 

 

 

D. YetAnother Array Queries Problem

time limitper test

2 seconds

memory limitper test

256megabytes

input

standardinput

output

standardoutput

You aregiven an array a ofsize n, and q queriesto it. There are queries of two types:

·      1 li ri —perform a cyclic shift of the segment [li, ri] to theright. That is, for every x suchthat li ≤ x < ri newvalue of ax + 1 becomesequal to old value of ax, and newvalue of ali becomesequal to old value of ari;

·      2 li ri —reverse the segment [li, ri].

Thereare m importantindices in the array b1b2, ..., bm. Foreach i suchthat 1 ≤ i ≤ m youhave to output the number that will have index bi in thearray after all queries are performed.

Input

The firstline contains three integer numbers nq and m (1 ≤ n, q ≤ 2·1051 ≤ m ≤ 100).

The secondline contains n integernumbers a1a2, ..., an (1 ≤ ai ≤ 109).

Then q linesfollow. i-th of themcontains three integer numbers tiliri,where ti is thetype of i-th query,and [li, ri] is thesegment where this query is performed (1 ≤ ti ≤ 21 ≤ li ≤ ri ≤ n).

The lastline contains m integernumbers b1b2, ..., bm (1 ≤ bi ≤ n) —important indices of the array.

Output

Print m numbers, i-th of whichis equal to the number at index bi afterall queries are done.

Example

input

6 3 5 1 2 3 4 5 6 2 1 3 2 3 6 1 1 6 2 2 1 5 3

output

3 3 1 5 2

 

 

 

 

G. Graphic Settings

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

RecentlyIvan bought a new computer. Excited, he unpacked it and installed his favouritegame. With his old computer Ivan had to choose the worst possible graphicsettings (because otherwise the framerate would be really low), but now hewants to check, maybe his new computer can perform well even with the bestpossible graphics?

Thereare m graphicsparameters in the game. i-thparameter can be set to any positive integer from 1 to ai, andinitially is set to bi(bi ≤ ai). So thereare  different combinations ofparameters. Ivan can increase or decrease any of these parameters by 1; after thatthe game will be restarted with new parameters (and Ivan will have theopportunity to check chosen combination of parameters).

Ivan wantsto try all p possiblecombinations. Also he wants to return to the initial settings after trying allcombinations, because he thinks that initial settings can be somehow bestsuited for his hardware. But Ivan doesn't really want to make a lot ofrestarts.

So he wantsyou to tell the following:

·        If there exists a way to make exactly p changes(each change either decreases or increases some parameter by 1) to try allpossible combinations and return to initial combination, then Ivan wants toknow this way.

·        Otherwise, if there exists a way to make exactly p - 1 changesto try all possible combinations (including the initial one), then Ivan wantsto know this way.

Help Ivan byshowing him the way to change parameters!

Input

The firstline of input contains one integer number m (1 ≤ m ≤ 6).

The secondline contains m integernumbers a1, a2, ..., am (2 ≤ ai ≤ 1000). It is guaranteedthat .

The thirdline contains m integernumbers b1, b2, ..., bm (1 ≤ bi ≤ ai).

Output

If there isa way to make exactly p changes(each change either decreases or increases some parameter by 1) to try allpossible combinations and return to initial combination, then output Cycle in thefirst line. Then p linesmust follow, each desribing a change. The line must be either inc x (increaseparameter x by 1) or dec x (decreaseit).

Otherwise,if there is a way to make exactly p - 1 changesto try all possible combinations (including the initial one), then output Path in thefirst line. Then p - 1 linesmust follow, each describing the change the same way as mentioned above.

Otherwise,output No.

Examples

input

1 3 1

output

Path inc 1 inc 1

input

1 3 2

output

No

input

2 3 2 1 1

output

Cycle inc 1 inc 1 inc 2 dec 1 dec 1 dec 2

 

 

F. AlmostPermutation

time limitper test

3 seconds

memory limitper test

512megabytes

input

standardinput

output

standardoutput

RecentlyIvan noticed an array a while debugginghis code. Now Ivan can't remember this array, but the bug he was trying to fixdidn't go away, so Ivan thinks that the data from this array might help him toreproduce the bug.

Ivan clearlyremembers that there were n elementsin the array, and each element was not less than 1 andnot greater than n. Also heremembers q factsabout the array. There are two types of facts that Ivan remembers:

·      1 li ri vi — foreach x suchthat li ≤ x ≤ ri ax ≥ vi;

·      2 li ri vi — foreach x suchthat li ≤ x ≤ ri ax ≤ vi.

Also Ivanthinks that this array was a permutation, but he is not so sure about it. Hewants to restore some array that corresponds to the qfacts thathe remembers and is very similar to permutation. Formally, Ivan has denotedthe cost ofarray as follows:

, where cnt(i) is thenumber of occurences of i in thearray.

Help Ivan todetermine minimum possible cost of thearray that corresponds to the facts!

Input

The firstline contains two integer numbers n and q (1 ≤ n ≤ 500 ≤ q ≤ 100).

Then q lines follow,each representing a fact about the array. i-th linecontains the numbers tiliri and vi for i-th fact (1 ≤ ti ≤ 21 ≤ li ≤ ri ≤ n1 ≤ vi ≤ nti denotesthe type of the fact).

Output

If the factsare controversial and there is no array that corresponds to them, print -1. Otherwise,print minimum possible cost of thearray.

Examples

input

3 0

output

3

input

3 1 1 1 3 2

output

5

input

3 2 1 1 3 2 2 1 3 2

output

9

input

3 2 1 1 3 2 2 1 3 1

output

-1

 


#include <stdio.h>
#include <stdlib.h>


int main()
{
	int N;
	scanf("%d",&N);
	int L[N];int R[N];

	//输入 
	int zero_L; int zero_R;
 	int num_err; 
	num_err=1;
	while(num_err!=0)
	{	
		for( int i=1; i<=N; i++)scanf("%d%d",&L[i],&R[i]);
		
		zero_L=0; zero_R=0; 
		num_err=0; 
		for(int i=1; i<=N; i++)
		{
			if(L[i]==0)zero_L++; 
			if(R[i]==0)zero_R++; 
		}
		if(zero_L!=zero_R||zero_L==0||zero_R==0)num_err=1; 
	
		for(int i=1; i<=N; i++)
		{	
			if(!(1<=L[i]&&L[i]<=N)){num_err=2; break; }
			if(!(1<=R[i]&&R[i]<=N)){num_err=2; break; }
			if(L[i]!=0)if(R[L[i]]!=i){num_err=3; break; }
			if(R[i]!=0)if(L[R[i]]!=i){num_err=3; break; }
		}
		
		if(num_err==1)
		{
			printf("err\n%d",N);
		}
		else if(num_err==2)
		{
			
		}
		else if(num_err==3)
		{
			
		}
		else
		{
			//正常 
		}
	}
	
	//处理 
	int a[N]; int num=0; int head; int j=0;
	for(int i=1; i<=N; i++)
	{
		if(L[i]==0)
		{
			//头 :head

			num++;
			head=i;
			a[num]=i;
			
			if(j!=0)
			{
				R[j]=head;
				L[head]=j;
			}
			
			for(int z=1; z<=N; z++)
			{
				if(R[a[num]]!=0)
				{
					num++;
					a[num]=R[a[num-1]];
				}
			}
			j=a[num];

			//尾 :j	
				
		}
	}
	
	for(int i=1; i<=N; i++)printf("\n%d %d",L[i],R[i]);
	
	
//	for(int i=1; i<=N; i++)printf("\n%d",a[i]);
	scanf("%d",&N);//保留框 
	return 0;
}          

/*
7
4 7
5 0
0 0
6 1
0 2
0 4
1 0
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值