ZOJ 3194 Coverage【贪心】【思维题】

本文介绍了一个算法问题,即如何通过调整一组给定点中 y 坐标的顺序来最大化由这些点构成的线段下方的覆盖面积。文章提供了一段 C++ 代码实现,通过排序 x 和 y 坐标并计算每一对相邻 x 值之间的距离来解决该问题。

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\n", ans / 2.0);
	}
	return 0;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值