3493. 【NOIP2013模拟联考13】三角形(triangle)
(File IO): input:triangle.in output:triangle.out
Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits
Goto ProblemSet做法:确定第一个点,枚举直线,判断不能形成三角形的情况,斜率不存在时要特判。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 3007 using namespace std; int n, h[N], z[N], tot, cnt, c[N * 2]; long long ans; bool b[N]; double K[N]; int main() { // freopen("triangle.in", "r", stdin); // freopen("triangle.out", "w", stdout); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &h[i], &z[i]); ans = (n * (n - 1)) / 2; ans *= n - 2; ans /= 3; for (int i = 1; i <= n - 1; i++) { cnt = 0; tot = 0; for (int j = i + 1; j <= n; j++) if (h[i] == h[j]) tot++; else K[++cnt] = (double)(z[i] - z[j]) / (double)(h[i] - h[j]); sort(K + 1, K + cnt + 1); ans -= (tot * (tot - 1)) / 2; tot = 1; for (int j = 2; j <= cnt; j++) { if (K[j] == K[j - 1]) tot++; else tot = 1; ans -= tot - 1; } } printf("%lld", ans); fclose(stdin); fclose(stdout); }