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的更多相关文章

  1. [转][Swust OJ 24]--Max Area(画图分析)

    转载自:http://www.cnblogs.com/hate13/p/4160751.html Max Area 题目描述:(链接:http://acm.swust.edu.cn/problem/2 ...

  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 用了第一种方式, ...

  3. LintCode 383: Max Area

    LintCode 383: Max Area 题目描述 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点 ...

  4. [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 ...

  5. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  6. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 在linux环境下配置node:node + npm + forever

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3574582.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  2. OpenJudge/Poj 1125 Stockbroker Grapevine

    1.链接地址: http://poj.org/problem?id=1125 http://bailian.openjudge.cn/practice/1125 2.题目: Stockbroker G ...

  3. Java初始化理解与总结 转载

    Java的初始化可以分为两个部分: (a)类的初始化 (b)对象的创建 一.类的初始化 1.1 概念介绍: 一个类(class)要被使用必须经过装载,连接,初始化这样的过程. 在装载阶段,类装载器会把 ...

  4. 在windows下装2个mysql数据库的办法

    如下 记录下以免找不到 http://blog.chinaunix.net/uid-77311-id-3450734.html 然后接下来是 1045的解决方案 可以在随便中找

  5. CentOS 7 install LNMP

    CentOS 7 install LNMP 关于 Nginx (发音 “engine x”)这是一款免费.开源.高效的 HTTP 服务器,Nginx是以稳定著称,丰富的功能,结构简单,低资源消耗.本教 ...

  6. 浅谈MVC、MVP、MVVM架构模式的区别和联系

    MVC.MVP.MVVM这些模式是为了解决开发过程中的实际问题而提出来的,目前作为主流的几种架构模式而被广泛使用. 一.MVC(Model-View-Controller) MVC是比较直观的架构模式 ...

  7. (转载)SQL语句导入导出大全

    SQL语句导入导出大全 /******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c ...

  8. 转最简便安装python+selenium-webdriver环境方法

    最简便安装python+selenium-webdriver环境方法 from:http://www.easonhan.info/python/2013/12/07/active-python-ins ...

  9. 第四章 Web表单

    4.1 跨站请求伪造保护 安装flask-wtf app = Flask(__name__) app.config['SECRET_KEY'] = 'hard to guess string' 密钥不 ...

  10. Android分类前言

    柚子园项目搁置后,半年多时间里都在开发微信公众平台和在公司实习,用的都是python,django,bottle,已经很久没有开发android了.技术的东西,不用就容易生疏甚至忘掉.刚好现在需要写毕 ...