Reprinted from: http://www.cnblogs.com/hate13/p/4160751.html
Max Area
Title Description: (Link: http://acm.swust.edu.cn/problem/24/)
Again, please do not be surprised, perhaps you have met, then please do it again. It's wolf's proudest subject.
In Cartesian coordinate system, there are n points on the positive half axis (x>=0,y>=0), the horizontal and vertical axes of these points are given, but the trouble is that the coordinates of these points are not paired well, and your task is to match the axis and ordinate of the n points, so that the n points and the x-axis are the largest area.
Input:
There is a positive integer m in the first row of the data, indicating that there is an M group of test instances. Next there are m rows, each representing a set of test instances. The first number of n for each row indicates that n points are given, followed by n x and y coordinates. (The given x-axis data is not duplicated and y-axis data is not duplicated) (M<5000,1<N<50)
Such as:
2
4 x1 x2 x3 x4 y1 y2 y3 y4
5 x1 x2 x3 x4 x5 y1 y2 y3 y4 y5
Output:
The output calculates the maximum area, the result retains two decimal places, each group of data occupies one row.
Examples:
2
4 0 1 3 5 1 2 3 4
6 14 0 5 4 6 8 1 5 6 2 4 3
15.00
59.00
Simple greedy, the input data should be double, WA one time.
Idea: Drawing, the entire polygon area can be divided into n-1 square trapezoid area of the sum, the n x-coordinate (or y-coordinate) as N-1 high, and then the area equal to all (the bottom + bottom) * High/2 and.
Here the direct processing is not good processing, expand, and then in addition to both sides, the middle of each end need to multiply the adjacent two high, so as Example 1:
Then s= (y1*h1 + y2+ (H1+H2) + y3* (H2+H3) + y4*h3)/2,y denotes ordinate, i.e. bottom, H is high. Then sort greedy.
OK, not clear, the painting will know.
The code is as follows:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6 using namespacestd;7 #defineN 50108 9 intN;Ten DoubleX[n]; One DoubleY[n]; A DoubleH[n]; - - voidSolve () the { - inti,j; -Sort (x+1, x+n+1); - for(i=1; i<=n;i++) + { - if(i==1) h[i]=x[i+1]-X[i]; + Else if(i==n) h[i]=x[i]-x[i-1]; A Elseh[i]=x[i+1]-x[i-1]; at } -Sort (H +1, h+n+1); -Sort (y+1, y+n+1); - Doublecn1=0; - for(i=1; i<=n;i++) - { inans+=h[i]*Y[i]; - } toprintf"%.2f\n", ans/2.0); + } - intMain () the { * intT; $scanf"%d",&T);Panax Notoginseng while(t--) - { thescanf"%d",&n); + for(intI=1; i<=n;i++) scanf ("%LF",&x[i]); A for(intI=1; i<=n;i++) scanf ("%LF",&y[i]); the solve (); + } - return 0; $}
View Code
[Turn]swust OJ 24--max area (drawing analysis)