51nod 1451:合法三角形 枚举斜率
2015-12-14 22:32
204 查看
1451 合法三角形
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
收藏
关注
有n个不同的点,问有多少组三元组能构成面积非0的三角形。
Input
Output
Input示例
Output示例
完全是水过的,感觉斜率在精度上还可以再卡我几次。。。
一开始按照x点坐标来暴力,T掉了,想也是,一个6层循环。。。
然后考虑枚举斜率,判重。但因为斜率是double型,搞了一个map<double,int>,结果精度上还是不行。
把斜率扩大很多很多倍,开了一个很大很大的数组,一开始memset又把我给T了,最后记录了一下数值,终于算是水过了。
A了之后看了别的人的做法,用double数组记录,排一下序,再去扫一遍判重是一个更好的做法。
代码:
重新写了一下上面那个思路的,这个比之前的靠谱多了,相对来说时间会长一些。
代码:
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
收藏
关注
有n个不同的点,问有多少组三元组能构成面积非0的三角形。
Input
单组测试数据。 第一行一个整数n (1 ≤ n ≤ 2000),表示点的数目。 接下来n行,每行包含两个整数 xi, yi ( -100 ≤ xi, yi ≤ 100),表示第i个点的坐标。输入保证点是两两不同的。
Output
输出合法的三角形数目。
Input示例
4 0 0 1 1 2 0 2 2
Output示例
3
完全是水过的,感觉斜率在精度上还可以再卡我几次。。。
一开始按照x点坐标来暴力,T掉了,想也是,一个6层循环。。。
然后考虑枚举斜率,判重。但因为斜率是double型,搞了一个map<double,int>,结果精度上还是不行。
把斜率扩大很多很多倍,开了一个很大很大的数组,一开始memset又把我给T了,最后记录了一下数值,终于算是水过了。
A了之后看了别的人的做法,用double数组记录,排一下序,再去扫一遍判重是一个更好的做法。
代码:
#pragma warning(disable:4996) #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <map> using namespace std; typedef long long ll; ll n; ll x[2005], y[2005]; ll appear[8000005]; int vis[8000005]; void input() { int i; scanf("%lld", &n); for (i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &y[i]); } void solve() { int i, j, k, h; ll repeat = 0; for (i = 1; i <= n; i++) { k = 0; for (j = i + 1; j <= n; j++) { double xx = x[j] - x[i]; double yy = y[j] - y[i]; double res; if (xx == 0) { res = 8000004; } else { res = yy / xx * 20000 + 4000000; } int r = res; repeat += appear[r]; if (appear[r] == 0) { vis[k] = r; k++; } appear[r]++; } for (h = 0; h < k; h++) { appear[vis[h]] = 0; } } printf("%lld\n", n*(n - 1)*(n - 2) / 6 - repeat); } int main() { //freopen("i.txt", "r", stdin); //freopen("o.txt", "w", stdout); input(); solve(); return 0; }
重新写了一下上面那个思路的,这个比之前的靠谱多了,相对来说时间会长一些。
代码:
#pragma warning(disable:4996) #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <map> using namespace std; typedef long long ll; #define eps 1e-6 ll n; ll x[2005], y[2005]; double xy[4000005]; void input() { int i; scanf("%lld", &n); for (i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &y[i]); } void solve() { ll i, j, k, h; ll repeat = 0; for (i = 1; i <= n; i++) { k = 0; for (j = i + 1; j <= n; j++) { double xx = x[j] - x[i]; double yy = y[j] - y[i]; double res; if (xx == 0) { res = 4000002; } else { res = yy / xx + 2000000; } xy[k++] = res; } sort(xy, xy + k); j = 0; for (h = 1; h < k; h++) { if (abs(xy[h] - xy[j]) > eps) { repeat += (h - j)*(h - j - 1) / 2; j = h; } } repeat += (h - j)*(h - j - 1) / 2; } printf("%lld\n", n*(n - 1)*(n - 2) / 6 - repeat); } int main() { //freopen("i.txt", "r", stdin); //freopen("o.txt", "w", stdout); input(); solve(); return 0; }
相关文章推荐
- VS2010中配置OPencv
- 程序员的职业状态
- Linux文件系统
- 动态添加表格(一)
- Windows、Linux系统开机密码破解
- JavaScript 数组双重for循环
- 对于opencv使用cvLoad加载分类器报错的解决
- ms sqlserver clr应用(一)
- A+B for Input-Output Practice (VIII)(没啥用,来凑数)
- 与调试器共舞 - LLDB 的华尔兹
- 我的大学
- JavaScript常用语句表达式
- iOS开发之上架AppStore
- 4-6 求单链表结点的阶乘和
- 第二百五十六天 how can I 坚持
- 【html】【21】高级篇--搜索框
- 逆向破解_iOS_学习笔记_1
- AnimTextView
- could not resolve the connect identifier specified
- LeetCode题解——Remove Invalid Parentheses