题意:给定n根木棍,不允许拼接或折断,选择四根组成矩形,求所有合法矩形中周长平方与面积比最小的一个,输出拼成这个矩形的四根木棍

n<=1e6

思路:猜结论:答案必定从相邻的4根中产生

证明见https://www.luogu.org/blog/78371/so1ution-cf1027c

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 1100000
#define MOD 1000000007
#define eps 1e-8
#define pi acos(-1)
#define oo 110000000000000 int a[N],b[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void swap(int &x,int &y)
{
int t=x;x=y;y=t;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas;
scanf("%d",&cas);
while(cas--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int m=;
int i=;
while(i<n)
{
i++;
if(i+<=n&&a[i]==a[i+]) {b[++m]=a[i]; i++;}
}
ll c=*(b[]+b[]);
ll s=b[]*b[];
ll ans1=c;
ll ans2=s;
int k=;
for(int i=;i<=m-;i++)
{
c=*(b[i]+b[i+]);
s=b[i]*b[i+];
if(s*ans1*ans1>ans2*c*c) {ans1=c; ans2=s; k=i;}
}
printf("%d %d %d %d\n",b[k],b[k],b[k+],b[k+]);
}
return ;
}

【CF1027C】Minimum Value Rectangle(贪心,数学)的更多相关文章

  1. CF1027C Minimum Value Rectangle 贪心 数学

    Minimum Value Rectangle time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. CF1027C Minimum Value Rectangle【贪心/公式化简】

    https://www.luogu.org/problemnew/show/CF1027C #include<cstdio> #include<string> #include ...

  3. CF1027C Minimum Value Rectangle

    之前做的时候没想出来...现在来数学推导一波. 题意:从n个木棒中选出4个拼成一个矩形,使得 (周长)2/面积 最小. 解:设矩形宽a长b.我们要最小化下面这个式子: 去掉常数,不妨设b = a + ...

  4. [Swift]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

  5. 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

    题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...

  6. LC 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

  7. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  8. 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

  9. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

随机推荐

  1. JavaScript无提示关闭当前页面窗口,兼容IE/Firefox/Chrome

    <script type="text/javascript" language="javascript"> function fc(){ var b ...

  2. iOS重绘机制drawRect

    iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动 ...

  3. Codevs3324 新斯诺克

    题目描述 Description 斯诺克又称英式台球,是一种流行的台球运动.在球桌上,台面四角以及两长边中心位置各有一个球洞,使用的球分别为1 个白球,15 个红球和6 个彩球(黄.绿.棕.蓝.粉红. ...

  4. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  5. cephfs 挂载 卸载

    #挂载 sudo ceph-fuse -m 10.1.xx.231:6789,10.1.xx.232:6789,10.1.xx.233:6789 -r /MySQL-BK /data/backup # ...

  6. chrome浏览器 配置开机启动全屏(看板app模式设置)

        chrome浏览器 配置开机启动全屏(看板app模式设置) 1.下载安装chrome浏览器. 2.建立一个chrome浏览器的快键方式,右键打开属性,如下图: 3.将目标选项卡的值修改为:&q ...

  7. C#基础-数组-冒泡排序

    冒泡排序基础 冒泡排序原理图分析 tmp在算法中起到数据交换的作用 int[] intNums = { 12,6,9,3,8,7 }; int tmp = intNums[0]; // 一共5次冒泡, ...

  8. 【nginx】nginx.sh nginx 安装脚本

    #! /bin/shcd /usr/local/srcwget http://nginx.org/download/nginx-1.10.1.tar.gzecho 'download success' ...

  9. 使用 Python 编写登陆接口

    # 使用 Python 编写登陆接口# Create Date: 2017.10.31 Tuesday# Author: Eric Zhao# -*- coding:utf-8 -*-'''编写登陆接 ...

  10. NO_PUBKEY

    * 现象:$ sudo apt-get update时警告如下: W: GPG error: http://ppa.launchpad.net precise Release: The followi ...