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

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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);
}


未完待续...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值