zoj 3194--Coverage
Given n points (xi, yi) (i = 1, 2 ... n) in a plane, where all xi will be distinct. After connecting the n points with staight lines in order from the leftmost point to the rightmost point, the area below the lines is called coverage. Now, it's your job to calculate the maximum coverage area, if yi (i = 1, 2 ... n) can be swapped arbitrarily.
Input
The first line of the input is an integer t (t <= 100), indicate the number of cases.
Each case starts with one integer n (2 <= n <= 1000) in a line. Then follows n lines, each consists of two integers x y (1 <= x, y <= 105) representing a point.
Cases are separated by one blank line.
Output
For each case, output the answer in a line, keep 1 digit after the decimal point.
Sample Input
2 3 1 1 2 2 3 3 3 1 2 4 1 2 5
Sample Output
4.5 10.0
给n个点,x固定,但是y可以随意变动,现在要求以这些点往x轴做垂直,构成m个梯形,这些梯形的最大面积是多少
对于第一个点,只可能是与x[1]-x[0]的长度构成高,同理,对于最后一点也只能与x[n]-x[n-1]构成
但是对于中间的点,都是x[n]-x[n-2]的高度差。所以我们可以将高度差先计算出来,经排序之后再与排序好的y相乘即可
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int N=1010; 6 int main() 7 { 8 double x[N],y[N],ans[N],sum; 9 int t,n,i; 10 scanf("%d",&t); 11 while(t--) 12 { 13 scanf("%d",&n); 14 for(i=0;i<n;i++) 15 scanf("%lf%lf",&x[i],&y[i]); 16 sort(x,x+n); 17 sort(y,y+n); 18 ans[0]=x[1]-x[0]; 19 ans[n-1]=x[n-1]-x[n-2]; 20 for(i=1;i<=n-2;i++) 21 ans[i]=x[i+1]-x[i-1]; 22 sort(ans,ans+n); 23 for(i=0,sum=0;i<n;i++) 24 sum+=ans[i]*y[i]; 25 printf("%.1lf\n",sum/2.0); 26 } 27 return 0; 28 }