背景

前几天打了比赛,崩麻了,所以来水一篇题解。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. 原生Django出现同源策略跨域的解决方式

    解决方式: 在返回数据的时候,添加响应头信息: 例如:

  2. CSS——选择器的优先级

    所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序.样式表中的特殊性描述了不同规则的相对权重. !important > 行内样式>ID选择器 > 类选择器 > 标签 ...

  3. Python实现五子棋人机对战的二次开发

    Python实现人机对战的二次开发     在网上找到了一个使用python实现五子棋游戏,其中通过加入一个简单的AI算法实现了人机对战的功能,我觉得这个人机对战还是蛮有意思的,下面我分析一下五子棋游 ...

  4. 鸿蒙HarmonyOS实战-ArkTS语言基础类库(XML)

    前言 数据传输的数据格式有以下几种常见的格式: JSON(JavaScript Object Notation):JSON是一种轻量级的数据交换格式,可读性高且易于解析.它使用键值对的方式表示数据,并 ...

  5. 抖音abogus逆向分析

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 目标网站 aHR0cHM6 ...

  6. css3颜色模式 圆角的实现 width的属性值 触发怪异盒模型

    Css颜色模式: rgb(255,0,0) rgba(255,0,0,0.5)(0.5是透明度) hsl(58%,56%)色彩饱和度 hala() border-image   url(路径) 向内偏 ...

  7. Spring扩展——BeanFactoryPostProcessor(BFPP)

    引言 在Spring中BeanFactoryPostProcessor(后面使用简写BFPP),作为容器启动过程的对容器进行修改操作的Bean对象,是Spring框架对外提供的核心扩展点之一,Spri ...

  8. json字符串忽略null,忽略字段,首字母大写等gson,jackson,fastJson实现demo,T data JSON.parseObject json转换

    json字符串忽略null,忽略字段,首字母大写等gson,jackson,fastJson实现demo package com.example.core.mydemo.json.vo; import ...

  9. Ajax分析与爬取实战

    Ajax 分析与爬取实战 准备工作 安装好 Python3 了解 Python HTTP 请求库 requests 的基本用法 了解 Ajax 基础知识和分析 Ajax 的基本方法 爬取目标 以一个示 ...

  10. WebUI自动化测试中关于图片验证码的解决方法

    关于怎么识别图片中的文字,传送门:https://www.cnblogs.com/teangtang/p/16157880.html 实现代码如下: #! /usr/bin/env python # ...