比赛链接:http://codeforces.com/contest/596
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.
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.
Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print - 1.
2
0 0
1 1
1
1
1 1
-1
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);
}
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.
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).
Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.
5
1 2 3 4 5
5
4
1 2 2 1
3
In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, 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;
}
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) 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 (x, y) 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 (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4.
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 w1, w2,..., 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.
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 (x, y) 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.
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.
5
2 0
0 0
1 0
1 1
0 1
0 -1 -2 1 0
YES
0 0
1 0
2 0
0 1
1 1
3
1 0
0 0
2 0
0 1 2
NO
In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) 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,1, 2. 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);
}
未完待续...