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. OpenJudge 2979 陪审团的人选 / Poj 1015 Jury Compromise

    1.链接地址: http://bailian.openjudge.cn/practice/2979 http://poj.org/problem?id=1015 2.题目: 总Time Limit: ...

  2. php configure help

    `configure' configures this package to adapt to many kinds of systems. Usage: ./configure [OPTION].. ...

  3. 《APUE》第四章笔记(1)

    1.引言 本章介绍文件系统的特征和文件的性质.从stat函数开始,逐个说明stat结构的每一个成员以了解文件的所有属性.在此过程中,还将会说明修改这些属性的各个函数,并更详细地查看UNIX文件系统的结 ...

  4. ie6下 gif动画不动

    ie6下 gif动画不动 如果有onclick事件:在IE6中,点击a标签,onclick事件会先执行,其次是href下的动作,href执行后,默认会执行跳转动作(尽管href属性不一定是一个地址), ...

  5. sscanf用法简析

    1. 常见用法. char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n" ...

  6. oracle常用SQL语句(汇总版)

    Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...

  7. MaskedTextBox控件实现输入验证

    Mask属性可以验证用户在文本中输入数据的格式 this.maskedTextBox1.Mask = "000000-00000000-000A";//身份证号码18位 this. ...

  8. 【Scala 】Akka库

    简介编辑 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用.它已经成功运用在电信行业.系统几乎不会宕机(高可用性 99.9 ...

  9. 如何组织css,写出高质量的css代码

    !如何组织css一:css的API 属于基础部分,这部分的能力用“对”和“错”来评判. 比如说把文字设置为红色,只能用color:red:这种写法是对的,其他任何写法都是错的. 二:css框架 不能用 ...

  10. [CC150] 八皇后问题

    Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of ...