【CF1027C】Minimum Value Rectangle(贪心,数学)
题意:给定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(贪心,数学)的更多相关文章
- CF1027C Minimum Value Rectangle 贪心 数学
Minimum Value Rectangle time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CF1027C Minimum Value Rectangle【贪心/公式化简】
https://www.luogu.org/problemnew/show/CF1027C #include<cstdio> #include<string> #include ...
- CF1027C Minimum Value Rectangle
之前做的时候没想出来...现在来数学推导一波. 题意:从n个木棒中选出4个拼成一个矩形,使得 (周长)2/面积 最小. 解:设矩形宽a长b.我们要最小化下面这个式子: 去掉常数,不妨设b = a + ...
- [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 ...
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...
- 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 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...
随机推荐
- LINQ与反射
string file = @"C:\Windows\winsxs\x86_netfx35linq-system.core_31bf3856ad364e35_6.1.7601.17514_n ...
- Virt-install用法:
#一般选项:指定虚拟机的名称.内存大小.VCPU个数及特性等 -n NAME, --name=NAME:虚拟机名称,需全局惟一: -r MEMORY, --ram=MEMORY:虚拟机内 ...
- Missing letters-freecodecamp算法题目
Missing letters 1.要求 从传递进来的字母序列中找到缺失的字母并返回它. 如果所有字母都在序列中,返回 undefined. 2.思路 设定缺失变量miss 在for循环遍历字符串的各 ...
- C/C++ 数组与指针
#include <iostream>using namespace std;int main(){ char *a[]={"ab","ccs",& ...
- python入门:用户登录,三次错误机会
#!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次机会重试 #主要分为两个部分,一部分是写三次循环,一部分写用户输入 #用户登录的实现,循环3次 ...
- 【php】instanceof
instanceof 的使用还有一些陷阱必须了解.在 PHP 5.1.0 之前,如果要检查的类名称不存在,instanceof 会调用__autoload().另外,如果该类没有被装载则会产生一个致命 ...
- 【nginx】 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: "Primary script unknown" w ...
- 图解Disruptor框架(二):核心概念
图解Disruptor框架(二):核心概念 概述 上一个章节简单的介绍了了下Disruptor,这节就是要好好的理清楚Disruptor中的核心的概念.并且会给出个HelloWorld的小例子. 在正 ...
- SQL Server ALwayson 正在解析
原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...
- 面向对象之元类(metaclass)
一.前言: 要搞懂元类必须要搞清楚下面几件事: 类创建的时候,内部过程是什么样的,也就是我们定义类class 类名()的过程底层都干了些啥 类的调用即类的实例化过程的了解与分析 我们已经知道元类存在的 ...