跳到正文

题解

题解:P9525 [JOISC2022] 团队竞技

Ambrosia约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年7月26日
收录
2025年12月12日
更新
2026年9月24日
篇幅
约 3 分钟
题目
P9525

版本

  1. v1当前

题目思路

贪心,每次选择 a,b,ca,b,c 最大的三个数,如果某一个数满足两个属性都最大,就直接删除。使用堆或者平衡树维护。

AC Code

C++
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
#define lnf 0x3f3f3f3f3f3f3f3f

const bool Debug = false;

//#define Debug true

using namespace std;

namespace WTH {
	const int N = 150000;
	
	int n;
	int a[N + 5], b[N + 5], c[N + 5];
	int x, y, z;
	bool _Flg[N + 5];
	priority_queue <pair <int, int>, vector <pair <int, int>>, less <pair <int, int>>> _A, _B, _C;
	
	void init () {
		for (register int i = 0; i <= N; ++ i) {
			_Flg[i] = 1;
		}
		
		ios::sync_with_stdio (Debug);
		cin.tie (0);
		cout.tie (0);
	}
	
	void main () {
		cin >> n;
		
		for (register int i = 1; i <= n; ++ i) {
			cin >> a[i] >> b[i] >> c[i];
			_A.push ({a[i], i});
			_B.push ({b[i], i});
			_C.push ({c[i], i});
		}
		
		while (_A.size () && _B.size () && _C.size ()) {
			x = _A.top ().second;
			y = _B.top ().second;
			z = _C.top ().second;
			
			if (b[x] >= b[y] || c[x] >= c[z]) {
				_Flg[x] = 0;
			}
			
			if (a[y] >= a[x] || c[y] >= c[z]) {
				_Flg[y] = 0;
			}
			
			if (a[z] >= a[x] || b[z] >= b[y]) {
				_Flg[z] = 0;
			}
			
			if (_Flg[x] * _Flg[y] * _Flg[z]) {
				cout << a[x] + b[y] + c[z] << endl;
				return ;
			}
			
			if (Debug) {
				puts ("HHH");
			}
			
			while (_A.size () && !_Flg[_A.top ().second]) {
				_A.pop ();
			}
			
			while (_B.size () && !_Flg[_B.top ().second]) {
				_B.pop ();
			}
			
			while (_C.size () && !_Flg[_C.top ().second]) {
				_C.pop ();
			}
		}
		
		cout << -1 << endl;
	}
}

signed main () {
	WTH::init ();
	WTH::main ();
	return 0;
}
原文