Codeforces Round #261 (Div. 2)(ABCD)

A. Pashmak and Garden

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Pashmak has fallen in love with an attractive girl called Parmida since one year ago...

Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.

Input

The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.

Output

If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.

Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).

Examples

input

Copy

0 0 0 1

output

Copy

1 0 1 1

input

Copy

0 0 1 1

output

Copy

0 1 1 0

input

Copy

0 0 1 2

output

Copy

-1

题意:给出两点,求正方形内其他两点,水题,注意一下细节即可

#include
using namespace std;
int main()
{
	int x1,y1,x2,y2,x3,y3,x4,y4;
	cin>>x1>>y1>>x2>>y2;
	if(x1==x2)
	{
		if(y1>y2)
		{
			swap(x1,x2);
			swap(y1,y2);
		}
	}
	else if(x1>x2)
	{
		swap(x1,x2);
		swap(y1,y2);
	}
//	printf("x1:%d y1:%d\n",x1,y1);
	if(x1==x2)
	{
		y3=y1;
		y4=y2;
		x3=abs(y1-y2)+x1,x2;
		x4=x3;
	}
	else if(y1==y2)
	{
		x3=x1;
		x4=x2;
		y3=abs(x2-x1)+y1,y2;
		y4=y3;
	}
	else
	{
		if(abs(x2-x1)!=abs(y2-y1))
		{
			printf("-1");
			return 0;
		}
		x3=x1;
		y3=y2;
		x4=x2;
		y4=y1;
	}
	printf("%d %d %d %d\n",x3,y3,x4,y4);
}

 

B. Pashmak and Flowers

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!

Your task is to write a program which calculates two things:

  1. The maximum beauty difference of flowers that Pashmak can give to Parmida.
  2. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.

Input

The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).

Output

The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.

Examples

input

Copy

2
1 2

output

Copy

1 1

input

Copy

3
1 4 5

output

Copy

4 1

input

Copy

5
3 1 2 3 1

output

Copy

2 4

Note

In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:

  1. choosing the first and the second flowers;
  2. choosing the first and the fifth flowers;
  3. choosing the fourth and the second flowers;
  4. choosing the fourth and the fifth flowers.

题意:找最大的数量和最小的数量组合方案数,注意mx==mi的情况

#include
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
ll a[maxn],mii,mxx,n;
int main()
{
	cin>>n;
	mxx=0,mii=0x3f3f3f3f;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		mxx=max(mxx,a[i]);
		mii=min(mii,a[i]);
	}
	ll n1=0,n2=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]==mxx) n2++;
		if(a[i]==mii) n1++;
	}
	if(mxx==mii)
		printf("0 %lld",n1*(n1-1)/2);
	else 
	cout<

后面两题就不会了,全是补的

C. Pashmak and Buses

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.

Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.

Input

The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).

Output

If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.

Examples

input

Copy

3 2 2

output

Copy

1 1 2 
1 2 1 

input

Copy

3 2 1

output

Copy

-1

Note

Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day.

构造n个d位k进制数。

把1,到d的数  全转换成K进制数即可,注意d位能不能构成n个数

#include
using namespace std;
const int N=1e3+10;
int a[N][N];
int n,k,d;
int main()
{
	cin>>n>>k>>d;
	if(pow(k,d)

 

D. Pashmak and Parmida's problem

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Examples

input

Copy

7
1 2 1 1 2 2 1

output

Copy

8

input

Copy

3
1 1 1

output

Copy

1

input

Copy

5
1 2 3 4 5

output

Copy

0

树状数组的题

题意: f(1, i, ai) > f(j, n, aj).Codeforces Round #261 (Div. 2)(ABCD)_第1张图片

#include
using namespace std;
typedef long long ll;
const int N=1e6+10;
mapmp1,mp2;
int n,a[N],c[N];
int low(int x)
{
	return x&(-x);
}
void up(int id,int x)
{
	for(int i=id;i<=n;i+=low(i))
	c[i]+=x;//printf("1\n");
}
int qu(int id)
{
	int sum=0;
	for(int i=id;i>=1;i-=low(i))
	sum+=c[i];
	return sum;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=n;i>=1;i--)
	{
		mp1[a[i]]++;
		up(mp1[a[i]],1);
	}
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		mp2[a[i]]++;
		up(mp1[a[i]],-1);
		mp1[a[i]]--;
		ans+=qu(mp2[a[i]]-1);
	}
	cout<

 

你可能感兴趣的:(数据结构---树状数组&RMQ,codeforce题解)