关闭

code forces round 331 div2

标签: codeforces
242人阅读 评论(0) 收藏 举报
分类:
A. Wilbur and Swimming Pool
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

水题,直接枚举所有情况就行了,注意n=1必定无解,n=2两点要是垂直于坐标轴有可能无解。

/*************************************************************************
    > File Name: A.cpp
    > Author: kanberra
    > Mail: kanberra354562007@gmail.com 
    > Created Time: 三 11/18 20:50:57 2015
 ************************************************************************/
#include <bits/stdc++.h>
using namespace std;
pair<int,int> p[10005];
int main(){
	int n;
	while (scanf("%d",&n)==1){
		for (int i=0;i<n;i++)
			scanf("%d%d",&p[i].first,&p[i].second);
		if (n==1){
			puts("-1");
			continue;
		}
		sort(p,p+n);
		if (n==4){
			cout<<(p[3].second-p[2].second)*(p[3].first-p[0].first);
		}else if (n==3){
			int len=p[2].first-p[0].first;
			int wid=0;
			for (int i=0;i<3;i++){
				for (int j=i+1;j<3;j++){
                    wid=max(wid,abs(p[j].second-p[i].second));
				}
			}
			cout<<wid*len<<endl;
		}else {
            if (p[0].second==p[1].second||p[0].first==p[1].first){
				puts("-1");
				continue;
			}
			else cout<<abs(p[0].second-p[1].second)*abs(p[0].first-p[1].first)<<endl;
		}
	}
	return 0;
}

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

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 subtract 1.

题意大概是这样的,给一个数组b,开始你有一个数组a,长度为n,全部为零,每次泥可以做一个事情,就是把坐标i后面的所有数字都加1或者减1,问最少的次数使得a数组变成b数组。

贪心就可以了,贪心策略是从第一个数字开始,先用一个cnt表示当前加到了几,因为这个是一起变化的,所以说可以代表所有后面的数字,比如当前在i=1,cnt=2,就表示a[1]的后面全是2.然后开始算次数,如果cnt小于sum我们就可以把sum+=(a[i]-cnt),如果cnt大于sum我们就sum+=(cnt-a[i]),从第一个滚到最后一个 sum就是结果。贪心策略的证明我想了一下,粗浅地来说可以这么想,因为如果是从中间开始的话照应不了前面,最后如果是从中间开始前面还得倒头再来一次又会破坏后面的平衡,所以必须从第一个一个一个地变换直到满足了b[i]的需求以后再往后面滚,直到过程结束。sum要long long。

/*************************************************************************
    > File Name: B.cpp
    > Author: kanberra
    > Mail: kanberra354562007@gmail.com 
    > Created Time: 三 11/18 21:08:00 2015
 ************************************************************************/
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main(){
	int n;
	while (scanf("%d",&n)==1){
       for (int i=0;i<n;i++){
		   scanf("%d",&a[i]);
	   }
	   int cnt=0;
	   long long sum=0;
       for (int i=0;i<n;i++){
	       if (a[i]>cnt){
			   sum+=a[i]-cnt;
			   cnt+=a[i]-cnt;
		   }else if (a[i]<cnt){
			   sum+=cnt-a[i];
			   cnt-=(cnt-a[i]);
		   }
	   }
	   cout<<sum<<endl;
	}
	return 0;
}


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5504次
    • 积分:259
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条