背景

前几天打了比赛,崩麻了,所以来水一篇题解。LC真睿智

题意

给你 \(n\) 个点,问最多能组成几个三角形。

分析

听说可以随机化。这道题就是一个简单贪心。

我们考虑,如果没有共线的点,那么答案显然就是 \(\frac{n}{3}\) 了。

如果有共线,我们容易想到一个贪心思路:既然同一直线上的点不能组成三角形,那么应该尽可能让多的不在这条直线上的点消耗这条直线上的点,即设直线上点的集合为 \(C\),那么对于任意 \(\{x,y\}\notin C\),让它和 \(C\) 中的两个元素组成三角形即可。这种情况下答案显然是 \(n-card(C)\),限制条件是直线上的点能够消耗完其余点,即 $\frac{card(C)}{2}> n-card(C) $。

由于 $1\leq n \leq 300 $ 的奇妙范围,直线的寻找可以直接暴力三重循环。对于是否共线的判断,可以用相似来证,具体在代码里面。

然后就可以上代码了。

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int w=1,s=0;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
while(isdigit(ch)){s=s*10+(ch-'0');ch=getchar();}
return w*s;
}
const int maxn=1e6+10;
int n;
struct no
{
int x,y;
}a[500];
int ans=-maxn;//记录最长直线
bool ch(no x,no y)//判断是否共线
{
return x.x*y.y==x.y*y.x;
}
no operator -(no x,no y)
{
return (no){x.x-y.x,x.y-y.y};
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int x=read(),y=read();
a[i]={x,y};
}
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
int sum=2;
for(int k=1;k<=n;k++)
{
if(k==i||k==j)continue;
if(ch(a[k]-a[i],a[k]-a[j]))sum++;
}
ans=max(ans,sum);//更新最大值
}
}
if(ans/2>n-ans)cout<<n-ans;//直线可以消耗所有点
else cout<<n/3;//不能
return 0;
}

题解:AT_arc173_b [ARC173B] Make Many Triangles的更多相关文章

  1. codechef Chef and The Right Triangles 题解

    Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by ...

  2. [USACO20FEB]Equilateral Triangles P 题解

    优雅的暴力. 设三个点为 \((i,j,k)\),则有 \(6\) 个未知数即 \(x_i,x_j,x_k,y_i,y_j,y_k\).又因为有 \(2\) 条关于这 \(6\) 个未知数的方程 \( ...

  3. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  4. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  5. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  6. Project Euler 91:Right triangles with integer coordinates 格点直角三角形

    Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...

  7. CodeForces 682E Alyona and Triangles (计算几何)

    Alyona and Triangles 题目连接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/J Description You ar ...

  8. Little Zu Chongzhi's Triangles

    Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 ...

  9. POJ1569 Myacm Triangles

    Description There has been considerable archeological work on the ancient Myacm culture. Many artifa ...

  10. hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形

    How Many Triangles 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5784 Description Alice has n poin ...

随机推荐

  1. wpf 无法从流中加载光标

    使用wpf 加载图片光标时,无论是用光标文件的绝对路径还是使用uri资源的方式都不得行,及报无法从文件中加载光标或无法从流中加载光标.这中情况,就很有可能是光标cur文件不是标准的光标文件.比如你是通 ...

  2. web开发遇到的坑之360浏览器缓存问题

    再使用360浏览器,浏览我自己开发的一个配置的web管理后台时,发现,使用ctrl+F5都不能刷新表格的数据,还有,我添加字段时,明明是添加成功的,用sql再数据库查都能查出来.但表格里就是不显示.我 ...

  3. RDP 端口转发 多窗口运行

    需要设置本机的默认端口进行修改 优点:(1)部署简单.Windows自带,支持IPv4和IPv6(2)不用重启机器,还长记性.命令即时生效,重启系统后配置仍然存在.缺点:(1)不支持UDP(2)XP/ ...

  4. Kubernetes操作图

  5. 「C++」深度分析C++中i++与++i的区别

    大家好,我是Charzie.在C++编程中,i++和++i是两个常见的自增运算符,用于将变量的值增加1(有时与i+=1效果一样).然而,虽然它们的功能看似相似,但在实际使用中却存在显著的区别.本博客将 ...

  6. wordpress博客系统

    wordpress博客系统 LNMP:Linux+nginx+mysql+php 一个操作系统+web网站+一个数据库存放数据+后端编程语言 基于红帽操作系统来搭建 1.需要一个本地yum仓库 [ro ...

  7. fs.1.10 ON rockylinux8 docker镜像制作

    概述 freeswitch是一款简单好用的VOIP开源软交换平台. rockylinux docker上编译安装fs1.10版本的流程记录. 环境 docker engine:Version 24.0 ...

  8. LeetCode 208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)(C++/Java)

    题目: Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); t ...

  9. 网络问题排查必备利器:Pingmesh

    背景 当今的数字化世界离不开无处不在的网络连接.无论是日常生活中的社交媒体.电子商务,还是企业级应用程序和云服务,我们对网络的依赖程度越来越高.然而,网络的可靠性和性能往往是一个复杂的问题,尤其是在具 ...

  10. sql的删除语句

    好久没用过sql的删除语句了,今天写删除语句的时候报错了,应该是:   DELETE FROM 表名称 WHERE 列名称 = 值 我写成了: DELETE FROM 表名称 别名 WHERE 别名. ...