Codeforces Round #331 (Div. 2) (ABC題解)

_TCgogogo_發表於2015-11-16

比賽連結:http://codeforces.com/contest/596


A. Wilbur and Swimming Pool
time limit per test
1 second
memory limit per test
256 megabytes

After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices.

Now Wilbur is wondering, if the remaining n vertices of the initial rectangle give enough information to restore the area of the planned swimming pool.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 4) — the number of vertices that were not erased by Wilbur's friend.

Each of the following n lines contains two integers xi and yi ( - 1000 ≤ xi, yi ≤ 1000) —the coordinates of the i-th vertex that remains. Vertices are given in an arbitrary order.

It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes.

Output

Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print  - 1.

Sample test(s)
input
2
0 0
1 1
output
1
input
1
1 1
output
-1
Note

In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.

In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area.


題目大意:矩形四個點去掉了其中幾個,問能否恢復,水

#include <cstdio>
#include <cstdlib>
int x[4], y[4];

int main() 
{
	int n, ans = -1;
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
		scanf("%d %d", &x[i], &y[i]);
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			if(x[i] != x[j] && y[i] != y[j])
				ans = abs(x[i] - x[j]) * abs(y[i] - y[j]);
	printf("%d", ans);
}



B. Wilbur and Array
time limit per test
2 seconds
memory limit per test
256 megabytes

Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn.

Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input.

The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).

Output

Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

Sample test(s)
input
5
1 2 3 4 5
output
5
input
4
1 2 2 1
output
3
Note

In the first sample, Wilbur may successively choose indices 1234, and 5, and add 1 to corresponding suffixes.

In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract1.


題目大意:初始序列都為0,要得到目標序列,可進行兩種操作,從ai到an每個數都加1或減1,問最少要操作幾次,掃一遍即可

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 200000 + 5;
ll a[MAX];

int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	ll ans = abs(a[0]);
	for(int i = 1; i < n; i++)
		ans += abs(a[i] - a[i - 1]);
	cout << ans << endl;
}



C. Wilbur and Points
time limit per test
2 seconds
memory limit per test
256 megabytes

Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (xy) belongs to the set, then all points (x'y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set.

Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (xy) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (00), (01), (10) and (11), there are two aesthetically pleasing numberings. One is 1234 and another one is 1324.

Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w1w2,..., wn, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to wi, that is s(xi, yi) = yi - xi = wi.

Now Wilbur asks you to help him with this challenge.

Input

The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with.

Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (xy) is present in the input, then all points (x'y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input.

The last line of the input contains n integers. The i-th of them is wi ( - 100 000 ≤ wi ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering.

Output

If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print "YES" on the first line of the output. Otherwise, print "NO".

If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them.

Sample test(s)
input
5
2 0
0 0
1 0
1 1
0 1
0 -1 -2 1 0
output
YES
0 0
1 0
2 0
0 1
1 1
input
3
1 0
0 0
2 0
0 1 2
output
NO
Note

In the first sample, point (20) gets number 3, point (00) gets number one, point (10) gets number 2, point (11) gets number 5 and point (01) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi.

In the second sample, the special values of the points in the set are 0 - 1, and  - 2 while the sequence that the friend gives to Wilbur is 0,12. Therefore, the answer does not exist.


題目大意:給n對數(xi, yi)每對都不同且,每對都要賦一個值vi(1-n),給n個值,現在問能否存在一種順序使得yi - xi = wi,且對於仍任意的x[i] <= x[i + 1]且y[i] <= y[i + 1]要求v[i] < v[i + 1]


題目分析:先按x排序,然後因為w的順序是固定的,先按w的順序賦值,這裡用一個佇列維護,因為根據先進先出,可以把小值賦給小對,比如(0,0)和(1,1)在樣例1中我就要給前者賦0後者賦4,因為有負數,加一個100000就好了,賦值的時候根據差值從佇列裡找即可,如果找不到且佇列不空則無解,,找完按賦的值排序,判斷是否合法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int const MAX = 2e5 + 5;
int const CON = 100000;

queue <int> q[MAX];

struct PAIR
{
	int x, y, d;
	int val;
}p[MAX];
int w[MAX];

bool cmp(PAIR a, PAIR b)
{
	if(a.x == b.x)
		return a.y < b.y;
	return a.x < b.x;
}

bool cmp2(PAIR a, PAIR b)
{
	return a.val < b.val;
}

int main()
{
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d %d", &p[i].x, &p[i].y);
		p[i].d = p[i].y - p[i].x;
	}
	sort(p, p + n, cmp);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &w[i]);
		q[w[i] + CON].push(i);
	}
	bool flag = true;
	for(int i = 0; i < n; i++)
	{
		if(!q[p[i].d + CON].empty())
		{
			p[i].val = q[p[i].d + CON].front();
			q[p[i].d + CON].pop();
		}
		else
		{
			flag = false;
			break;
		}
	}
	sort(p, p + n, cmp2);
	for(int i = 0; i < n - 1; i++)
	{
		if(p[i].x >= p[i + 1].x && p[i].y >= p[i + 1].y)
		{
			flag = false;
			break;
		}
	}
	if(!flag)
	{
		printf("NO\n");
		return 0;
	}
	printf("YES\n");
	for(int i = 0; i < n; i++)
		printf("%d %d\n", p[i].x, p[i].y);
}


未完待續...

相關文章