zoukankan      html  css  js  c++  java
  • ZOJ 3194 Coverage【贪心】【思维题】

    Coverage

    Time Limit: 1 Second      Memory Limit: 32768 KB

    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 <= xy <= 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
    题意:对于题目给的点,x固定,而与x组合的y可以任意交换,求如何安置y可使这些点组成线段下面的面积最大,最大面积是多少



    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    typedef long long ll;
    
    const int mod = 1e9 + 7;
    const int maxn = 1010;
    
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while (t--)
    	{
    		int n;
    		double x[maxn], y[maxn], dis[maxn];
    		scanf("%d", &n);
    		for (int i = 0; i < n; i++)
    		{
    			scanf("%lf%lf", x + i, y + i);
    		}
    		sort(x, x + n);
    		sort(y, y + n);
    		dis[0] = x[1] - x[0];
    		dis[n - 1] = x[n - 1] - x[n - 2];
    		for (int i = 1; i < n - 1; i++)
    		{
    			dis[i] = x[i + 1] - x[i - 1];
    		}
    		sort(dis, dis + n);
    		double ans = 0;
    		for (int i = 0; i < n; i++)
    		{
    			ans += dis[i] * y[i];
    		}
    		printf("%.1f
    ", ans / 2.0);
    	}
    	return 0;
    }




    Fighting~
  • 相关阅读:
    PAT Basic 1023 组个最小数 (20 分)
    PAT Advanced 1048 Find Coins (25 分)
    PAT Basic 1005 继续(3n+1)猜想 (25 分)
    PAT Advanced 1050 String Subtraction (20 分)
    PAT Advanced 1041 Be Unique (20 分)
    PAT Basic 1047 编程团体赛 (20 分)
    SpringSecurity整合SpringBoot
    给正在运行的Docker容器动态绑定卷组
    linux 中格式化json字符串
    docker/kubernetes国内源/镜像源解决方式
  • 原文地址:https://www.cnblogs.com/Archger/p/8451588.html
Copyright © 2011-2022 走看看