题解:P9525 [JOISC2022] 团队竞技

涉及知识点:堆

解题思路

利用堆来维护每个能力的最大值。

如果当前的最大值都不属于同一只海狸,就输出。

否则把当重复的元素弹出堆。

如果堆空了还没有找到,说明没有,输出无解。

代码

#include <bits/stdc++.h>
#define int long long
#define ll __int128
#define db double
#define ldb long double
#define vo void
#define endl '\n'
#define il inline
#define re register
#define ve vector
#define p_q priority_queue
#define PII pair<int, int>
#define u_m unordered_map
#define bt bitset

using namespace std;

//#define O2 1
#ifdef O2
	#pragma GCC optimize(1)
	#pragma GCC optimize(2)
	#pragma GCC optimize(3, "Ofast", "inline")
#endif

struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
	char buf[MAXSIZE], *p1, *p2;
	char pbuf[MAXSIZE], *pp;
	IO() : p1(buf), p2(buf), pp(pbuf) {}

	~IO() {
		fwrite(pbuf, 1, pp - pbuf, stdout);
	}
	char gc() {
		if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
		return p1 == p2 ? ' ' : *p1++;
	}

	bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}

	template <class T>
	void read(T &x) {
		double tmp = 1;
		bool sign = 0;
		x = 0;
		char ch = gc();
		while (!isdigit(ch)) {
			if (ch == '-') sign = 1;
			ch = gc();
		}
		while (isdigit(ch)) {
			x = x * 10 + (ch - '0');
			ch = gc();
		}
		if (ch == '.') {
			ch = gc();
			while (isdigit(ch)) {
				tmp /= 10.0, x += tmp * (ch - '0');
				ch = gc();
			}
		}
		if (sign) x = -x;
	}

	void read(char *s) {
		char ch = gc();
		for (; blank(ch); ch = gc());
		for (; !blank(ch); ch = gc()) * s++ = ch;
		*s = 0;
	}

	void read(char &c) {
		for (c = gc(); blank(c); c = gc());
	}

	void push(const char &c) {
		if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
		*pp++ = c;
	}

	template <class T>
	void write(T x) {
		if (x < 0) x = -x, push('-');
		static T sta[35];
		T top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while (x);
		while (top) push(sta[--top] + '0');
	}

	template <class T>
	void write(T x, char lastChar) {
		write(x), push(lastChar);
	}
} io;

namespace COMB {
	int fact[200000];
	int Triangle[1010][1010];
	void Fact(int n, int mod) {
		fact[0] = 1;
		for (int i = 1; i <= n; i ++ ) fact[i] = ((fact[i - 1]) % mod * (i % mod)) % mod;
	}
	void Pascal_s_triangle(int n, int mod) {
		for (int i = 0; i <= n; i ++ ) Triangle[i][0] = 1;
		for (int i = 1; i <= n; i ++ )
			for (int j = 1; j <= i; j ++ )
				Triangle[i][j] = (Triangle[i - 1][j] + Triangle[i - 1][j - 1]) % mod;
	}
	int pw(int x, int y, int mod) {
		int res = 1;
		while (y) {
			if (y & 1) res = ((res % mod) * (x % mod)) % mod;
			x = (x % mod) * (x % mod) % mod;
			y >>= 1;
		}
		return res;
	}
	int pw(int x, int y) {
		int res = 1;
		while (y) {
			if (y & 1) res *= x;
			x *= x;
			y >>= 1;
		}
		return res;
	}
	int GCD(int x, int y, int mod) {
		return __gcd(x, y) % mod;
	}
	int LCM(int x, int y, int mod) {
		return (((x % mod) * (y % mod)) % mod / (GCD(x, y, mod) % mod)) % mod;
	}
	int C(int n, int m, int mod) {
		if (m > n || m < 0) return 0;
		return fact[n] * pw(fact[m], mod - 2, mod) % mod * pw(fact[n - m], mod - 2, mod) % mod;
	}
	int Ask_triangle(int x, int y) {
		return Triangle[x][y];
	}
}
using namespace COMB;

//#define fre 1
#define IOS 1
//#define multitest 1

const int N = 4e6 + 10;
const int M = 4e5 + 10;
const int inf = 1e12;
const int Mod = 1e9 + 9;

namespace zla {
	int n;
	struct node {
		int a, b, c;
	} A[N];
	int vis[N];
	p_q<PII> a, b, c;

	il void Init() {
		cin >> n;
		for (int i = 1; i <= n; i ++ ) {
			cin >> A[i].a >> A[i].b >> A[i].c;
			a.push(make_pair(A[i].a, i));
			b.push(make_pair(A[i].b, i));
			c.push(make_pair(A[i].c, i));
		}
	}

	il void Solve() {
		for (int i = 1; i <= n; i ++ ) {
			int x = a.top().second;
			int y = b.top().second;
			int z = c.top().second;
			if (A[x].b == A[y].b || A[x].c == A[z].c) vis[x] = 1;
			if (A[y].c == A[z].c || A[y].a == A[x].a) vis[y] = 1;
			if (A[z].a == A[x].a || A[z].b == A[y].b) vis[z] = 1;
			if (!vis[x] && !vis[y] && !vis[z]) {
				cout << A[x].a + A[y].b + A[z].c;
				return ;
			}
			while (!a.empty() && vis[a.top().second]) a.pop();
			while (!b.empty() && vis[b.top().second]) b.pop();
			while (!c.empty() && vis[c.top().second]) c.pop();
		}
		cout << -1;
	}

	il void main() {
		Init();
		Solve();
	}
}

signed main() {
	int T;
#ifdef IOS
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
	freopen("team.in", "r", stdin);
	freopen("team.out", "w", stdout);
#endif
#ifdef multitest
	cin >> T;
#else
	T = 1;
#endif
	while (T--) zla::main();
	return 0;
}

posted @ 2024-10-25 14:41  zla_2012  阅读(4)  评论(0编辑  收藏  举报