[Swustoj 24] Max Area
Max Area
题目描述:
又是这道题,请不要惊讶,也许你已经见过了,那就请你再来做一遍吧。这可是wolf最骄傲的题目哦。
在笛卡尔坐标系正半轴(x>=0,y>=0)上有n个点,给出了这些点的横坐标和纵坐标,但麻烦的是这些点的坐标没有配对好,你的任务就是将这n个点的横坐标和纵坐标配对好,使得这n个点与x轴围成的面积最大。
输入:
在数据的第一行有一个正整数m,表示有m组测试实例。接下来有m行,每行表示一组测试实例。每行的第一个数n,表示给出了n个点,接着给出了n个x坐标和y坐标。(给出的x轴的数据不会重复,y轴数据也不会重复)(m<5000,1<n<50)
如:
2
4 x1 x2 x3 x4 y1 y2 y3 y4
5 x1 x2 x3 x4 x5 y1 y2 y3 y4 y5
输出:
输出所计算的最大面积,结果保留两位小数,每组数据占一行。
样例:
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
简单贪心、输入数据应该为double、Wa一次。
思路:画图、整个多边形面积可以划分为n-1个直角梯形的面积之和、将n个x坐标(或y坐标)看做n-1个高,然后面积等于所有(上底+下底)*高/2的和。
这里直接处理不好处理,展开、然后除了两边、中间的每个底需要乘以相邻两个高,故如样例1:
则S=(y1*h1 + y2+(h1+h2) + y3*(h2+h3) + y4*h3)/2,y表示纵坐标,即底,h为高。然后排序贪心即可。
好吧、没说清楚、画下图就知道了。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 5010 int n;
double x[N];
double y[N];
double h[N]; void solve()
{
int i,j;
sort(x+,x+n+);
for(i=;i<=n;i++)
{
if(i==) h[i]=x[i+]-x[i];
else if(i==n) h[i]=x[i]-x[i-];
else h[i]=x[i+]-x[i-];
}
sort(h+,h+n+);
sort(y+,y+n+);
double ans=;
for(i=;i<=n;i++)
{
ans+=h[i]*y[i];
}
printf("%.2f\n",ans/2.0);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lf",&x[i]);
for(int i=;i<=n;i++) scanf("%lf",&y[i]);
solve();
}
return ;
}
[Swustoj 24] Max Area的更多相关文章
- [转][Swust OJ 24]--Max Area(画图分析)
转载自:http://www.cnblogs.com/hate13/p/4160751.html Max Area 题目描述:(链接:http://acm.swust.edu.cn/problem/2 ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- LintCode 383: Max Area
LintCode 383: Max Area 题目描述 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点 ...
- [leetcode]python 695. Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...
- C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...
- LeetCode 695. Max Area of Island (岛的最大区域)
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] Max Area of Island 岛的最大面积
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [Swift]LeetCode695. 岛屿的最大面积 | Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...
- 九度OJ 朋友圈 -- 并查集
题目地址:http://ac.jobdu.com/problem.php?pid=1526 题目描述: 假如已知有n个人和m对好友关系(存于数字r).如果两个人是直接或间接的好友(好友的好友的好友.. ...
- 九度OJ 1163 素数
题目地址:http://ac.jobdu.com/problem.php?pid=1163 题目描述: 输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和 ...
- 少年Vince之遐想
本文999纯水贴,然转载仍需注明: 转载至http://www.cnblogs.com/VinceYang1994/ 昨天去姑姑家拜年,表哥房间的角落里有一架缠有蜘蛛网的遥控直升飞机. 打开飞机及遥控 ...
- 前端资源多个产品整站一键打包&包版本管理(一)
来新公司工作的第五个月.整站资源打包管理也提上了日程. 问题: 首先.什么是整站的打包管理呢? 我们公司的几个重要产品都在同一个webapp里面,但是,不同的开发部门独立开发不同的产品,长期以来,我们 ...
- python描述符descriptor(一)
Python 描述符是一种创建托管属性的方法.每当一个属性被查询时,一个动作就会发生.这个动作默认是get,set或者delete.不过,有时候某个应用可能会有 更多的需求,需要你设计一些更复杂的动作 ...
- 数位DP入门之hdu 3555 Bomb
hdu 3555 Bomb 题意: 在1~N(1<=N<=2^63-1)范围内找出含有 ‘49’的数的个数: 与hdu 2089 不要62的区别:2089是找不不含 '4'和 '62'的区 ...
- 一步步学习NHibernate(4)——多对一,一对多,懒加载(1)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们学会如何使用NHibernate对数据的简单查询,删除,更新和插入,那么如果说仅仅是这样的话,那么NHi ...
- windows8.1专业中文版一个可用的密钥分享
分享一个windows8.1专业中文版一个可用的密钥,亲测可用,联网输入密钥激活即可. PKHMN-TWQ6R-XDTH7-P4WW4-YR9T7