Codeforces 849B. Tell Your World 计算几何
来源:互联网 发布:tsp优化问题 编辑:程序博客网 时间:2024/05/29 15:12
题目大意
给出n(3-1000)和n个数ai(±1e9),ai代表点(i,ai),判断这n个点是否在两条平行但不重合的直线上.
解法分析
直线的斜率只有一个,只要先得到斜率再检查即可.
从a1,a2,a3上必定可以得到斜率(最多3个),然后分别检查每个斜率是否正确.
AC代码
#include <bits/stdc++.h>using namespace std;#define eps 1e-6int n;double a[1010];int check(double k){ int point = 0; //表示除了1所在直线外第二条直线的点 for(int i = 2; i <= n; i++) { if(fabs((a[i] - a[1]) - k * (i - 1)) < eps) continue; if(point == 0) point = i; else if(fabs((a[i] - a[point]) - k * (i - point)) >= eps) return 0; } return point;}int main(void){ cin >>n; for(int i = 1; i <= n; i++) cin >> a[i]; if(check(a[2] - a[1]) || check(a[3] - a[2]) || check((a[3] - a[1]) / 2)) printf("yes\n"); else printf("no\n"); return 0;}
总结
关于逻辑结构
我的第一版程序写了80+行,因为逻辑结构太复杂,隐藏了一个小bug导致fst.
第二版是参考了网上的解法,自己写的代码,与ac代码的结构大致相同,但是check部分就写了20行.
第三版(ac代码)是参考了网上的结构,真的服.
关于优化逻辑结构的总结
注意一整个if中如果出现了两次相同的判断条件,就说明该优化了
关于else,如果在循环中,可以不用else直接走循环,或者使用continue
在函数中,检查到不符合条件可以直接return 不用设置控制变量.
阅读全文
1 0
- codeforces 849B Tell Your World(计算几何)
- Codeforces 849 B. Tell Your World(计算几何)
- Codeforces 849B. Tell Your World 计算几何
- Codeforces 849 B Tell Your World
- CodeForces 849B Tell Your World 点线
- codeforces 849B. Tell Your World
- codeforces 849B Tell Your World
- Codeforces-849B Tell Your World
- CodeForces #431Div. 2 849B Tell your world 几何 暴力 枚举
- Codeforces 894B. Tell Your World
- Codeforces Tell Your World
- Codeforces Round #431 (Div. 2) B. Tell Your World
- Codeforces Round #431 (Div. 2)B Tell Your World
- Codeforces Round #431 (Div. 2) 849B Tell Your World(暴力+枚举)
- CodeFroces 849B. Tell Your World (鸽巢原理)
- Codeforces Round #431 (Div. 2) B. Tell Your World(向量积)
- Codeforces Round #431 (Div. 2) B. Tell Your World(技巧性模拟+精度坑死人系列)
- B. Tell Your World Codeforces Round #431 (Div. 2)(简单模拟)
- android 获取当前屏幕显示的Activity
- Hdu 5396 Expression 区间DP+组合数
- 基本雷达测高工具箱BRAT(Basic Radar Altimetry Toolbox)的API
- Mac 下android studio 中ndk 环境配置
- 深入理解sql注入(01)
- Codeforces 849B. Tell Your World 计算几何
- 初探-Vector方法
- 2. Add Two Numbers
- Python机器学习教程——逻辑回归
- 老电视
- crontab使用
- Java对象内存分析
- 朴素贝叶斯法--简单总结
- 微信小程序----组件之progress