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. Vsftpd -- 验证方式

    vsftpd程序提供的FTP服务可选认证方式,分别为匿名访问.本地用户和虚拟用户: 匿名访问:任何人无需验证口令即可登入FTP服务端. 本地用户:使用FTP服务器中的用户.密码信息. 虚拟用户:创建独 ...

  2. zoj 3471 Most Powerful

    题目链接:zoj 3471 Most Powerful  作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...

  3. Putty终端 模拟 远程登录 虚拟机Linux

    1.虚拟机设置 虚拟机设置->网络适配器->选择Host-only:与主机共享一个私有网络 桥接.NAT.Host-only三种网络模式的说明: (1)桥接:表示在局域网内是一台真实的系统 ...

  4. “微信应用号对行业影响”之一,app开发速来围观

    昨天,微信张小龙的一个讲话刷爆朋友圈,除了4大价值观,最后顺便提到:要推出微信应用号! 其实,价值观也就说说听听,最后顺便提到的微信应用号,才是真正的巨型炸弹. 腾讯挟6亿高粘度用户之重,号令天下,阿 ...

  5. 使用PHP导出Word文档的原理和实例

    PHP操作Word文档的方法有很多,这里再为大家提供一种方法. 原理   一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方 ...

  6. wpf单实例运行

    默认情况下我们可以打开一个应用程序多个实例,例如你双击一个exe多次.当然有些时候这么做会带来很多好处,但是有时我们又不希望这么做,要避免这个问题其实很简单,同WinForm中单实例运行一个应用是一样 ...

  7. python 脚本

    mag3.py 1,import import sys from org.eclipse.jface.dialogs import MessageDialogfrom org.eclipse.core ...

  8. Sybase数据库异常紧急恢复

    现象:Error 926  Severity Level 14  Error Message Text  Database 'xx' cannot be opened - it has been ma ...

  9. MVC-简单验证码制作

    1.制作验证码: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing. ...

  10. 微软职位内部推荐-ATG Engineer II

    微软近期Open的职位: ATG Engineer - GeneralistReady to work on some of the most advanced hardware on the pla ...