优雅的暴力。

设三个点为 \((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 题解的更多相关文章

  1. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  2. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  3. 《C与指针》第四章练习

    本章问题 1.Is the following statement legal?If so,what does it do? (下面的语句是否合法,如果合法,它做了什么) 3 * x * x - 4 ...

  4. uva 11178 - Morley's Theorem

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. Matlab网格划分

    之前转载了一篇博客http://blog.sina.com.cn/s/blog_6163bdeb0102dvay.html,讲Matlab网格划分程序Distmesh,看了看程序,感觉程序写得有很多值 ...

  6. UVA_11178_Morley's_Theorem_(计算几何基础)

    描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=23&pag ...

  7. uva 11178 Morley&#39;s Theorem(计算几何-点和直线)

    Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states tha ...

  8. uva11178 Morley’s Theorem(求三角形的角三分线围成三角形的点)

    Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states that that the ...

  9. HTML入门12

    开始了解响应式图片 响应式,根据屏幕尺寸和分辨率的设备上都能良好工作以及其他特性的图片,接下来考虑怎样创建自适应得图片,专注于img元素,完成自适应. 分辨率切换,不同的尺寸 <img srcs ...

随机推荐

  1. HCNP Routing&Switching之组播技术-IGMP-Snooping

    前文我们了解了组播协议IGMP相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15700550.html:今天我们来聊一聊二层交换机处理组播报文所面临的 ...

  2. CKKS Part2: CKKS的编码和解码

    该文章翻译自CKKS EXPLAINED, PART 2: FULL ENCODING AND DECODING,主要介绍CKKS方案中是如何编码和解码的(如何将复数向量转成整数多项式,以及如何求逆运 ...

  3. 通过json动态创建控制器

    通过字符串来创建控制器 如果通过字符串来创建控制器 不可以直接通过类型来获取对应的类 因为Swift有命名空间,类前需要加上命名空间的名称 获取命名空间的名称 let executable = NSB ...

  4. webpack引入css文件

    需要配置 postcss  详见 官网 https://www.postcss.com.cn/

  5. MySQL数据类型的最优选择

    MySQL数据类型的最优选择   慎重选择数据类型很重要.为啥哩?可以提高性能.原理如下:            ● 存储(内存.磁盘).从而节省I/O(检索相同数据情况下)      ● 计算.进而 ...

  6. having筛选结果集

    题目要求:让你输出有两科及其以上挂科(60分及格)的学生的名单? name subject score 错误的做法: mysql> select name, count(scoure<60 ...

  7. 用Java模拟实现对系统文件以目录的拷贝功能

    要用Java对单个文件拷贝的话,其实思路很简单,循环读取被拷贝文件,放入byte数组,然后写入目标文件.当然我们也可以借助现有的类去完成,如InputStream中的transferTo()方法就可以 ...

  8. Solution -「Gym 102979L」 Lights On The Road

    \(\mathcal{Description}\)   Link.   给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...

  9. CentOS7搭建ntp时钟服务器

    文章目录 服务器配置 远程客户端配置 服务器配置 # 关闭防火墙,selinux=disabled 1.# 服务器部署 [root@localhost ~]# yum -y install ntp n ...

  10. Spring AOP基础概念及自定义注解式AOP初体验

    对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...