[USACO20FEB]Equilateral Triangles P 题解
优雅的暴力。
设三个点为 \((i,j,k)\),则有 \(6\) 个未知数即 \(x_i,x_j,x_k,y_i,y_j,y_k\)。又因为有 \(2\) 条关于这 \(6\) 个未知数的方程 \(ij=jk,ij=ik\),所以一定能通过枚举其中的 \(4\) 个量来求解,时间复杂度 \(O(n^4)\)。
而这个 \(O(n^4)\) 的暴力是肉眼可见的跑不满(
考虑先枚举点 \(i\),则有以下四种情况:

解得 \(x=a,y=a-b\)。
其中,\(a,x>0,0\le b,y \le a\)。

解得 \(x=a,y=a-b\)。
其中,其中,\(a,x>0,0\le b,y\le a,\color{red}b\not= 0\)。

解得 \(x=2b-a,y=b-a\)。
其中,\(0\le a<b,0\le x,y\)。

解得 \(x=2b-a,y=b-a\)。
其中,\(0\le a<b,0\le x,y,\color{red}a\not=0\)。
注意,有些同时存在于两种情况的状态, 需要通过标红的判断去除。
然后就能敲出以下代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=310;
inline int read(){
int x=0;
char c=getchar();
for(;(c^'.')&&(c^'*');c=getchar());
return c=='*';
}
bool c[maxn][maxn];
int n,ans;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
c[i][j]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(!c[i][j]) continue;
for(int a=0;a<=n;a++){
for(int b=0;b<=a;b++){
if(a&&i+a<=n&&j+a<=n&&i-a+b>0&&j+a+b<=n)
ans+=(c[i+a][j+a]&c[i-a+b][j+a+b]);
if(a&&b&&i-a>0&&j+a<=n&&i+a-b<=n&&j+a+b<=n)
ans+=(c[i-a][j+a]&c[i+a-b][j+a+b]);
}
for(int b=a+1;b<=n;b++){
if(i-b-b+a>0&&j+a<=n&&i-b+a>0&&j+a+b<=n)
ans+=(c[i-b-b+a][j+a]&c[i-b+a][j+a+b]);
if(a&&i+b+b-a<=n&&j+a<=n&&i+b-a<=n&&j+a+b<=n)
ans+=(c[i+b+b-a][j+a]&c[i+b-a][j+a+b]);
}
}
}
printf("%d\n",ans);
return 0;
}
然后你会获得 \(51pt\) 的高分。
容易发现,代码中搜索到了许多冗余的状态,考虑将判断放到循环之外:
#include<bits/stdc++.h>
using namespace std;
const int maxn=310;
inline int read(){
int x=0;
char c=getchar();
for(;(c^'.')&&(c^'*');c=getchar());
return c=='*';
}
bool c[maxn][maxn];
int n,ans;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
c[i][j]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(!c[i][j]) continue;
for(int a=0;a<=n;a++){
if(a&&i+a<=n&&j+a<=n)
for(int b=max(a-i+1,0);b<=a&&j+a+b<=n;b++)
ans+=(c[i+a][j+a]&c[i-a+b][j+a+b]);
if(a&&i-a>0&&j+a<=n)
for(int b=max(i+a-n,1);b<=a&&b<=n-j-a;b++)
ans+=(c[i-a][j+a]&c[i+a-b][j+a+b]);
if(j+a<=n)
for(int b=a+1;j+a+b<=n&&b+b<i+a;b++)
ans+=(c[i-b-b+a][j+a]&c[i-b+a][j+a+b]);
if(a&&j+a<=n)
for(int b=a+1;j+a+b<=n&&b+b<=n-i+a;b++)
ans+=(c[i+b+b-a][j+a]&c[i+b-a][j+a+b]);
}
}
printf("%d\n",ans);
return 0;
}
然后就过了。
祝AC。
[USACO20FEB]Equilateral Triangles P 题解的更多相关文章
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- 《C与指针》第四章练习
本章问题 1.Is the following statement legal?If so,what does it do? (下面的语句是否合法,如果合法,它做了什么) 3 * x * x - 4 ...
- uva 11178 - Morley's Theorem
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Matlab网格划分
之前转载了一篇博客http://blog.sina.com.cn/s/blog_6163bdeb0102dvay.html,讲Matlab网格划分程序Distmesh,看了看程序,感觉程序写得有很多值 ...
- UVA_11178_Morley's_Theorem_(计算几何基础)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=23&pag ...
- uva 11178 Morley's Theorem(计算几何-点和直线)
Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states tha ...
- uva11178 Morley’s Theorem(求三角形的角三分线围成三角形的点)
Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states that that the ...
- HTML入门12
开始了解响应式图片 响应式,根据屏幕尺寸和分辨率的设备上都能良好工作以及其他特性的图片,接下来考虑怎样创建自适应得图片,专注于img元素,完成自适应. 分辨率切换,不同的尺寸 <img srcs ...
随机推荐
- CNN-卷积神经网络简单入门(2)
在上篇中,对卷积神经网络的卷积层以及池化层模块进行了简单的介绍,接下来将对卷积神经网络的整个运作流程进行分析,以便对CNN有个总体上的认知和掌握. 如下图,卷积神经网络要完成对图片数字的识别任务.网络 ...
- 面向计算机视觉的深度学习 | iBooker·ApacheCN
原文:Deep Learning for Computer Vision 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生 ...
- Lesson1——NumPy NumPy 安装
NumPy 教程目录 NumPy 安装 Python 官网上的发行版是不包含 NumPy 模块的.(pip list 测试一下) 我们可以使用以下几种方法来安装. 1 使用已有的发行版本 对于许多用户 ...
- hihoCoder挑战赛1 毁灭者问题
题目链接:http://hihocoder.com/problemset/problem/1034 数据结构题,由于每个魔法单位有着不同的回复速度和上限,所以不能根据吸收时间点进行查询和更新.但是如果 ...
- 前后端数据json交换的问题
问题1:前端发送给后端数据了,后端也接收到了,后端同时返回数据给前端了,但是前端的ajax请求中的success(data){}中的方法不执行 解决:排查了很多问题,结果都一一排除了,最后发现后端发送 ...
- list和tuple的用法区别
1.list中是可变的,tuple不可变 所以tuple没有insert, pop,append方法 2.定义只有一个元素的tuple的时候,必须加逗号,否则不会被认为是tuple,而被识别为括号 ...
- 认识Visual C++ 6.0工程结构
- NSPredicate类,指定过滤器的条件---董鑫
/* 比较和逻辑运算符 就像前面的例子中使用了==操作符,NSPredicate还支持>, >=, <, <=, !=, <>,还支持AND, OR, NOT(或写 ...
- LVS+Keepalived群集
LVS+Keepalived群集 目录 LVS+Keepalived群集 一.Keepalived实现原理 1. 单服务器的风险 2. Keepalived工具 3. Keepalived解决单点故障 ...
- MySQL5.7 库、表结构、表字段的查询、更改操作
1.查询所有数据库 SHOW DATABASES; 2.查询库中所有表 写法1: ① USE [DATABASE_NAME]; ② SHOW TABLES; 写法2: SHOW TABLES FROM ...