Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

A. Be Positive

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of nn integers: a1,a2,…,ana1,a2,…,an. Your task is to find some non-zero integer dd (−103≤d≤103−103≤d≤103) such that, after each number in the array is divided by dd, the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least ⌈n2⌉⌈n2⌉). Note that those positive numbers do not need to be an integer (e.g., a 2.52.5 counts as a positive number). If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.

Recall that ⌈x⌉⌈x⌉ represents the smallest integer that is not less than xx and that zero (00) is neither positive nor negative.

Input

The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in the array.

The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (−103≤ai≤103−103≤ai≤103).

Output

Print one integer dd (−103≤d≤103−103≤d≤103 and d≠0d≠0) that satisfies the given condition. If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.

Examples

input

Copy

5
10 0 -7 2 6

output

Copy

4

input

Copy

7
0 0 1 -1 0 0 2

output

Copy

0

Note

In the first sample, n=5n=5, so we need at least ⌈52⌉=3⌈52⌉=3 positive numbers after division. If d=4d=4, the array after division is [2.5,0,−1.75,0.5,1.5][2.5,0,−1.75,0.5,1.5], in which there are 33 positive numbers (namely: 2.52.5, 0.50.5, and 1.51.5).

In the second sample, there is no valid dd, so 00 should be printed.

题意:求一个数d使得n个数里面的正整数的数量大于n/2;

#include<bits/stdc++.h>
using namespace std;
int main(){
	int sum=0;
	int n,x,t,sum1=0;
	scanf("%d",&n);
	if(n%2==0)
		t=n/2;
	else t=n/2+1;
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		if(x>0)
			sum++;
		else if(x<0)
			sum1++;
	}
	if(sum>=t)
		printf("1\n");
	else if(sum1>=t)
		printf("-1\n");
	else printf("0\n");	
} 

B. Two Cakes

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sasha and Dima want to buy two nn-tier cakes. Each cake should consist of nn different tiers: from the size of 11 to the size of nn. Tiers should go in order from the smallest to the biggest (from top to bottom).

They live on the same street, there are 2⋅n2⋅n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the ii-th house you can buy a tier of the size aiai(1≤ai≤n1≤ai≤n).

Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 11, then 22, then 33 and so on up to nn.

Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 11.

Input

The first line of the input contains an integer number nn — the number of tiers in each cake (1≤n≤1051≤n≤105).

The second line contains 2⋅n2⋅n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤n1≤ai≤n), where aiai is equal to the size of the tier, which can be bought in the ii-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 11 to nn occurs in aa exactly two times.

Output

Print one number — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy nn tiers in ascending order of their sizes.

Examples

input

Copy

3
1 1 2 2 3 3

output

Copy

9

input

Copy

2
2 1 1 2

output

Copy

5

input

Copy

4
4 1 3 2 2 3 1 4

output

Copy

17

Note

In the first example, the possible optimal sequence of actions is:

  • Sasha buys a tier of size 11 near the 11-st house (a1=1a1=1);
  • Dima goes to the house 22;
  • Dima buys a tier of size 11 near the 22-nd house (a2=1a2=1);
  • Sasha goes to the house 44;
  • Sasha buys a tier of size 22 near the 44-th house (a4=2a4=2);
  • Sasha goes to the house 55;
  • Sasha buys a tier of size 33 near the 55-th house (a5=3a5=3);
  • Dima goes to the house 33;
  • Dima buys a tier of size 22 near the 33-rd house (a3=2a3=2);
  • Dima goes to the house 66;
  • Dima buys a tier of size 33 near the 66-th house (a6=3a6=3).

So, Sasha goes the distance 3+1=43+1=4, and Dima goes the distance 1+1+3=51+1+3=5. In total, they cover a distance of 4+5=94+5=9. You can make sure that with any other sequence of actions they will walk no less distance.

题意:两个人要买n片蛋糕且要按照升序的方式买,求两个人所走步数和最少是多少。

贪心。。。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b;
}c[200005];
int cmp(node x,node y){
	if(x.a==y.a)
		return x.b<y.b;
	else return x.a<y.a;
}
int main(){
	int n;
	long long s1=0,s2=0;
	scanf("%d",&n);
	n*=2;
	for(int i=0;i<n;i++){
		scanf("%d",&c[i].a);
		c[i].b=i;
	}
	sort(c,c+n,cmp);
	int d=0,d1=0;
	for(int i=0;i<n;i++){
		if(i==0){
			s1+=c[i].b;
			d=c[i].b;
		}
		else if(c[i].a==c[i-1].a){
			s2+=abs(d1-c[i].b);
			d1=c[i].b;
		}
		else if(c[i].a!=c[i-1].a){
			s1+=abs(d-c[i].b);
			d=c[i].b;
		}
	}
	printf("%lld\n",s1+s2);
}

C. Connect

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alice lives on a flat planet that can be modeled as a square grid of size n×nn×n, with rows and columns enumerated from 11 to nn. We represent the cell at the intersection of row rr and column cc with ordered pair (r,c)(r,c). Each cell in the grid is either land or water.

An example planet with n=5n=5. It also appears in the first sample test.

Alice resides in land cell (r1,c1)(r1,c1). She wishes to travel to land cell (r2,c2)(r2,c2). At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).

Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.

To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells (rs,cs)(rs,cs) and (rt,ct)(rt,ct) is (rs−rt)2+(cs−ct)2(rs−rt)2+(cs−ct)2.

For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2). If no tunnel needs to be created, the cost is 00.

Input

The first line contains one integer nn (1≤n≤501≤n≤50) — the width of the square grid.

The second line contains two space-separated integers r1r1 and c1c1 (1≤r1,c1≤n1≤r1,c1≤n) — denoting the cell where Alice resides.

The third line contains two space-separated integers r2r2 and c2c2 (1≤r2,c2≤n1≤r2,c2≤n) — denoting the cell to which Alice wishes to travel.

Each of the following nn lines contains a string of nn characters. The jj-th character of the ii-th such line (1≤i,j≤n1≤i,j≤n) is 0 if (i,j)(i,j) is land or 1 if (i,j)(i,j) is water.

It is guaranteed that (r1,c1)(r1,c1) and (r2,c2)(r2,c2) are land.

Output

Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2).

Examples

input

Copy

5
1 1
5 5
00001
11111
00111
00110
00110

output

Copy

10

input

Copy

3
1 3
3 1
010
101
010

output

Copy

8

Note

In the first sample, a tunnel between cells (1,4)(1,4) and (4,5)(4,5) should be created. The cost of doing so is (1−4)2+(4−5)2=10(1−4)2+(4−5)2=10, which is optimal. This way, Alice could walk from (1,1)(1,1) to (1,4)(1,4), use the tunnel from (1,4)(1,4) to (4,5)(4,5), and lastly walk from (4,5)(4,5) to (5,5)(5,5).

In the second sample, clearly a tunnel between cells (1,3)(1,3) and (3,1)(3,1) needs to be created. The cost of doing so is (1−3)2+(3−1)2=8(1−3)2+(3−1)2=8.

题意:有一个n*n的地图里面有陆地也有水,给定两个点,从一个点到另一个点只能走陆地求所要建立的最小陆地隧道。

两次bfs(),标记好与起点和终点所连接的陆地,数据比较少暴力求最短距离。

#include<bits/stdc++.h>
using namespace std;
char a[505][505];
struct node{
	int x,y;
}now,next1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int vis[505][505],p[505][505],n;
void bfs(int x,int y,int pos){
	memset(vis,0,sizeof(vis));
	queue<node>q;
	now.x=x,now.y=y;
	q.push(now);
	p[x][y]=pos;
	vis[x][y]=1;
	while(!q.empty()){
		now=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			next1.x=now.x+dx[i],next1.y=now.y+dy[i];
			if(a[next1.x][next1.y]=='0'&&next1.x>=1&&next1.x<=n&&next1.y>=1&&next1.y<=n&&vis[next1.x][next1.y]==0){
				p[next1.x][next1.y]=pos;
				vis[next1.x][next1.y]=1;
				q.push(next1);
			}
		}
	}
}
int main(){
	int r1,c1,r2,c2;
	int ans=0x3f3f3f3f;
	scanf("%d",&n);
	scanf("%d%d",&r1,&c1);
	scanf("%d%d",&r2,&c2);
	for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
	bfs(r1,c1,1);
	if(p[r2][c2]==1){
		printf("0\n");
		return 0;
	}
	bfs(r2,c2,2);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int l=1;l<=n;l++){
				for(int k=1;k<=n;k++){
					if(p[i][j]==1&&p[l][k]==2){
						ans=min(ans,(i-l)*(i-l)+(j-k)*(j-k));
					}
				}
			}
		}
	}
	printf("%d\n",ans);
}

D1. Toy Train (Simplified)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.

Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of nn stations, enumerated from 11through nn. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station ii is station i+1i+1 if 1≤i<n1≤i<n or station 11 if i=ni=n. It takes the train 11 second to travel to its next station as described.

Bob gave Alice a fun task before he left: to deliver mm candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 11 through mm. Candy ii (1≤i≤m1≤i≤m), now at station aiai, should be delivered to station bibi (ai≠biai≠bi).

The blue numbers on the candies correspond to bibi values. The image corresponds to the 11-st example.

The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.

Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.

Input

The first line contains two space-separated integers nn and mm (2≤n≤1002≤n≤100; 1≤m≤2001≤m≤200) — the number of stations and the number of candies, respectively.

The ii-th of the following mm lines contains two space-separated integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n; ai≠biai≠bi) — the station that initially contains candy ii and the destination station of the candy, respectively.

Output

In the first and only line, print nn space-separated integers, the ii-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station ii.

Examples

input

Copy

5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5

output

Copy

10 9 10 10 9 

input

Copy

2 3
1 2
1 2
1 2

output

Copy

5 6 

Note

Consider the second sample.

If the train started at station 11, the optimal strategy is as follows.

  1. Load the first candy onto the train.
  2. Proceed to station 22. This step takes 11 second.
  3. Deliver the first candy.
  4. Proceed to station 11. This step takes 11 second.
  5. Load the second candy onto the train.
  6. Proceed to station 22. This step takes 11 second.
  7. Deliver the second candy.
  8. Proceed to station 11. This step takes 11 second.
  9. Load the third candy onto the train.
  10. Proceed to station 22. This step takes 11 second.
  11. Deliver the third candy.

Hence, the train needs 55 seconds to complete the tasks.

If the train were to start at station 22, however, it would need to move to station 11 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1=65+1=6 seconds.

题意:有n个车站和m次运输,把a站的糖果运往b站,求每个车站所需要走的最短距离。

优先运往与车站远的车站,贪心。

#include<bits/stdc++.h>
using namespace std;
vector<int>v[105];
int id,a[105],b[105],n;
int cmp(int x,int y){
	int xx=x>id?x-id:n-id+x;
	int yy=y>id?y-id:n-id+y;
	return xx>yy;
}
int main(){
	int m,x,y;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
	}
	for(int i=1;i<=n;i++){
		id=i;
		sort(v[i].begin(),v[i].end(),cmp);
	}
	for(int i=1;i<=n;i++){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int pos=i,ans=0,cnt=0;
		while(1){
			if(a[pos]<v[pos].size()){
				b[v[pos][a[pos]]]++;
				a[pos]++;
			}
				cnt+=b[pos];
				b[pos]=0;
				if(cnt==m)
					break;
				ans++;
				pos++;
				if(pos==n+1)
					pos=1;
			}	
			printf("%d ",ans);
		}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值