家庭房产(PTA)

复制代码
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

//10
//6666 5551 5552 1 7777 1 100
//1234 5678 9012 1 0002 2 300
//8888 - 1 - 1 0 1 1000
//2468 0001 0004 1 2222 1 500
//7777 6666 - 1 0 2 300
//3721 - 1 - 1 1 2333 2 150
//9012 - 1 - 1 3 1236 1235 1234 1 100
//1235 5678 9012 0 1 50
//2222 1236 2468 2 6661 6662 1 300
//2333 - 1 3721 3 6661 6662 6663 1 100

set<int> s[1000];
double arr1[1000] = { 0 };
double arr2[1000] = { 0 };

int main() {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		int a;
		for (int j = 0; j < 3; j++) {
			cin >> a;
			if (a != -1) s[i].insert(a);
		}
		cin >> a;
		for (int j = 0, b; j < a; j++) {
			cin >> b;
			s[i].insert(b);
		}
		cin >> arr1[i] >> arr2[i];
		set<int> r;
		for (auto x : s[i]) {
			for (int k = 0; k <= i; k++) {
				if (s[k].count(x)) {
					r.insert(k);
				}
			}
		}
		int t = *r.begin();
		r.erase(r.begin());
		if (r.size()) {
			for (auto l : r) {
				for (auto p : s[l]) {
					s[t].insert(p);
				}
				arr1[t] += arr1[l];
				arr2[t] += arr2[l];
				s[l].clear();
			}
		}
		
	}

	//创建一个下标数组,用它排序
	vector<int> ind;
	for (int i = 0; i < N; i++) {
		if (!s[i].empty()) {
			ind.push_back(i);
			arr1[i] /= s[i].size();
			arr2[i] /= s[i].size();
		}
	}
	sort(ind.begin(), ind.end(), [&](int i, int j) -> bool {
			if (arr2[i] != arr2[j]) return arr2[i] > arr2[j];
			return (*s[i].begin()) < (*s[j].begin());
	});
	cout << ind.size() << endl;
	for (int i = 0, I = ind.size(); i < I; i++) {
		printf("%04d %zd %.3lf %.3lf\n", *s[ind[i]].begin(), s[ind[i]].size(),                 
               arr1[ind[i]], arr2[ind[i]]);
	}
	return 0;
}
相关推荐
爱喝热水的呀哈喽2 小时前
cpp二分库函数 lambda捕获
算法
longlongzihan2 小时前
LeetCode 76. 最小覆盖子串:滑动窗口算法详解
算法·leetcode·滑动窗口
写后端的胖头鱼10 小时前
一文详解B+树原理(附场景+示例)
数据结构·b树
怕浪猫11 小时前
RAG 面试 6 连问,从原理到优化全部覆盖
python·算法·面试
无线通信科研笔记12 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
mmmmath_313 小时前
二叉树の递归遍历
算法
All for pursuit.13 小时前
【位运算-1】338.比特位计数
数据结构·c++·算法·leetcode
小师兄吃牛肉14 小时前
C语言中的nullptr有必要吗
c语言·c++·算法
WL学习笔记14 小时前
日期类(C++)
开发语言·c++·日期类实现
H.莓飛15 小时前
【C++】命名空间、缺省参数、函数重载与引用
linux·开发语言·c++·visual studio