[蓝桥杯]真题讲解:数三角(枚举+STL)

[蓝桥杯]真题讲解:数三角(枚举+STL)

一、视频讲解

[蓝桥杯]真题讲解:数三角(枚举+STL)

在这里插入图片描述

二、正解代码

1、C++

#include<bits/stdc++.h>
#define int long long 
using namespace std;

signed main(){
	int n; cin >> n;
	vector<array<int,2>>a(n);

	map<pair<int,int>, int>node;

	for(int i = 0; i < n; i ++) {
		cin >> a[i][0] >> a[i][1];
		node[{a[i][0], a[i][1]}] ++;
	}
	int ans = 0;

	for(int i = 0; i < n; i ++) {
		map<int,vector<int>>st;

		for(int j = 0; j < n; j ++) {
			int dis = (a[i][0] - a[j][0]) * (a[i][0] - a[j][0]) + (a[i][1] - a[j][1]) * (a[i][1] - a[j][1]); 	
			if(dis)st[dis].push_back(j);
		}

		for(auto x: st) {
			vector<int>&no = x.second;
			int sum = no.size();
			ans += sum * (sum - 1) / 2;
			
			int del = 0;
			for(int j = 0; j < no.size(); j ++) {
				int x1 = a[i][0], y1 = a[i][1];
				int x2 = a[no[j]][0], y2 = a[no[j]][1];
				int x3 = x1 * 2 - x2, y3 = y1 * 2 - y2;
				del += (node[{x3, y3}]);
			}
			ans -= (del / 2);
		}
	}
	cout << ans << endl;
	return 0;
}

2、python3

n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
node = {}
for x, y in a:
    key = (x, y)
    if key not in node:
        node[key] = 1
    else:
        node[key] += 1
ans = 0
dis = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(i + 1, n):
        x1, y1 = a[i]
        x2, y2 = a[j]
        d = (x1 - x2) ** 2 + (y1 - y2) ** 2
        dis[i][j] = d
        dis[j][i] = d

st = {}
for i in range(n):
    st.clear()
    x1, y1 = a[i]
    for j in range(n):
        if dis[i][j]:
            if dis[i][j] not in st:
                st[dis[i][j]] = [j]
            else:
                st[dis[i][j]].append(j)

    for d,no in st.items():
        sum = len(no)
        ans += sum * (sum - 1) // 2
        dell = 0
        for j in no:
            x2, y2 = a[j]
            x3 = x1 * 2 - x2
            y3 = y1 * 2 - y2
            if (x3, y3) in node:
                dell += node[(x3, y3)]
        ans -= (dell // 2)
print(ans)

3、Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<int[]> a = new ArrayList<>();
        Map<String, Integer> node = new HashMap<>();
        for(int i = 0; i < n; i ++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            a.add(new int[]{x, y});
            String no = x + "#" + y;
            node.put(no, node.getOrDefault(no, 0) + 1);
        }
        long ans = 0;
        for(int i = 0; i < n; i ++) {
            Map<Long, List<Integer>> st = new HashMap<>();
            for(int j = 0; j < n; j ++) {
                int x1 = a.get(i)[0], y1 = a.get(i)[1];
                int x2 = a.get(j)[0], y2 = a.get(j)[1];
                long dis = (long)Math.pow(x1 - x2, 2) + (long)Math.pow(y1 - y2, 2);
                if(dis == 0)continue;
                if(st.get(dis) == null)
                    st.put(dis, new ArrayList<Integer>());
                st.get(dis).add(j);
            }
            for(Map.Entry<Long, List<Integer>> x: st.entrySet()){
                List<Integer>no = x.getValue();
                long sum = no.size();
                ans += sum * (sum - 1) / 2;
                long del = 0;
                for(int j = 0; j < no.size(); j ++) {
                    int x1 = a.get(i)[0], y1 = a.get(i)[1];
                    int x2 = a.get(no.get((j)))[0], y2 = a.get(no.get((j)))[1];
                    int x3 = x1 * 2 - x2, y3 = y1 * 2 - y2;
                    if(node.get(x3 + "#" + y3) != null)
                        del += node.get(x3 + "#" + y3);
                }
                ans -= del / 2;
            }
        }
        System.out.println(ans);
    }
}   }
}

相关推荐

  1. 2023省赛分糖果 |+DFS

    2024-05-13 12:36:07       28 阅读
  2. 2024】好

    2024-05-13 12:36:07       12 阅读
  3. [ 2016]回文日期

    2024-05-13 12:36:07       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 12:36:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 12:36:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 12:36:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 12:36:07       20 阅读

热门阅读

  1. python的错误处理和调试

    2024-05-13 12:36:07       11 阅读
  2. 【代码随想录】day52

    2024-05-13 12:36:07       10 阅读
  3. [力扣题解]45. 跳跃游戏 II

    2024-05-13 12:36:07       11 阅读
  4. Redis——Redis的数据库结构、删除策略及淘汰策略

    2024-05-13 12:36:07       12 阅读
  5. Tauri框架:使用Rust构建轻量级桌面应用

    2024-05-13 12:36:07       14 阅读
  6. C语言和BASH SHELL中条件表达式的真假与0和1的关系

    2024-05-13 12:36:07       11 阅读
  7. 运维:CentOS常见命令详解

    2024-05-13 12:36:07       12 阅读
  8. 蓝桥杯-错误票据(两种写法stringstream和扣字符)

    2024-05-13 12:36:07       13 阅读