题意:n个给定点,q个询问点,每次询问给出一个坐标A,问从n中选定两个点B,C,有多少种方案使得ABC是个直角三角形。
思路:直角三角形能想的就那几个,枚举边,枚举顶点,这个题都行,写的枚举顶点的,A点分两种情况,1是直角,2是非直角。防止误差,用分数表示斜率,然后用了map<pair<int,int> int> 发现t了,改成unordered_map发现这个unordered_map只能映射一个,即unordered_map<ll, int>,所以得用到hash,把维的点hash成一个数。
说下那两种情况,
#include<bits/stdc++.h> typedef long long ll; using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } int ans[2200]; unordered_map<ll,int>mp; struct node{ int x, y; }p[2200], q[2200]; ll Hash(node v){ //hash ll A = 2333, B = 5279, C = 998244353; return A*v.x + B*v.y + C; } int main(){ int n,w; cin >> n >> w; for(int i = 1;i <= n;i++) scanf("%d%d",&p[i].x, &p[i].y); for(int i = 1;i <= w;i++) scanf("%d%d",&q[i].x,&q[i].y); //第一种,A为直角顶点 for(int i = 1;i <= w;i++){ mp.clear(); for(int j = 1;j <= n;j++){ int x = q[i].x - p[j].x; int y = q[i].y - p[j].y; int g = gcd(x,y); x /= g; y /= g; mp[Hash((node){x, y})]++; } for(int j = 1; j <= n; j++){ int nowx=q[i].x - p[j].x; int nowy=q[i].y - p[j].y; int g=gcd(nowx,nowy); nowx /= g; nowy /= g; swap(nowx, nowy); ans[i] += (mp[Hash((node){-nowx, nowy})] + mp[Hash((node){nowx, -nowy})]); } ans[i] /= 2; } //第二种,A为非直角顶点 for(int i = 1; i <= n; i++){ mp.clear(); for(int j = 1; j <= n; j++){ if(i == j) continue; int x = p[i].x - p[j].x; int y = p[i].y - p[j].y; int g = gcd(x,y); x /= g; y /= g; mp[Hash((node){x, y})]++; } for(int j = 1; j <= w; j++){ int nowx=p[i].x - q[j].x; int nowy=p[i].y - q[j].y; int g=gcd(nowx,nowy); nowx /= g; nowy /= g; swap(nowx, nowy); ans[j] += (mp[Hash((node){-nowx, nowy})] + mp[Hash((node){nowx, -nowy})]); } } for(int i = 1; i <= w; i++) printf("%d\n",ans[i]); return 0; }
原文地址:https://www.cnblogs.com/philo-zhou/p/11671124.html
时间: 2024-11-03 06:32:32