[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 ...
随机推荐
- 用最笨的方法实现java控制台日历打印
如果想用户自定义输入日期查询,可以通过Calendar的set方法和Scanner方法设置 Calendar类简单使用:https://blog.csdn.net/weixin_43670802/ar ...
- Python中set集合常用操作
功能 Python符号 Python方法 备注 交集 & intersection, intersection_update &:取两者交集>>> set3 = se ...
- JIRA 测试循环的创建和使用
3.测试循环 3.1测试循环的创建 测试人员编写完测试用例,并评审通过后:测试负责人可以计划测试循环. 点击JIRA 选择列表栏中的"测试".点击"计划循环测试 ...
- B快速导航
GETTING STARTED If you are new to Selenium, we have a few resources that can help you get up to spee ...
- [源码解析] 模型并行分布式训练Megatron (5) --Pipedream Flush
[源码解析] 模型并行分布式训练Megatron (5) --Pipedream Flush 目录 [源码解析] 模型并行分布式训练Megatron (5) --Pipedream Flush 0x0 ...
- k8s 通过helm发布应用
什么是helm? Helm 是 Kubernetes 的包管理器.Helm 是查找.分享和使用软件构建 Kubernetes 的最优方式. 在红帽系的Linux中我们使用yum来管理RPM包,类似的, ...
- HTML笔记整理--上节
一.认识WEB 「网页」主要是由文字.图像和超链接等元素构成,当然除了这些元素,网页中还可以包括音频.视频以及Flash等. 「浏览器」是网页显示.运行的平台. 「浏览器内核」(排版引擎.解释引擎.渲 ...
- Solution -「OurOJ 46544」漏斗计算
\(\mathcal{Description}\) Link. 定义一个运算结点 \(u\) 有两个属性:当前容量 \(x_u\).最大容量 \(V_u\).提供以下单元操作: I 读入一个整 ...
- Solution -「AT 3913」XOR Tree
\(\mathcal{Description}\) Link. 给定一棵树,边 \((u,v)\) 有边权 \(w(u,v)\).每次操作可以使一条简单路径上的边权异或任意非负整数.求最少的操 ...
- Solution -「CF 802C」Heidi and Library (hard)
\(\mathcal{Descriptoin}\) Link. 你有一个容量为 \(k\) 的空书架,现在共有 \(n\) 个请求,每个请求给定一本书 \(a_i\).如果你的书架里没有这本书 ...