2023牛客多校第十场 - L

比赛地址:传送门
赛时过了 3 题,继续努力哈~
L 拓扑排序

L Grayscale Confusion

题意
给你 n 种由 \(RGB\) 表示的颜色,你需要将每个颜色都转换成由灰度值(范围 0 ~ 255)。
转化有两个限制:

  • 给定的第一种和第二种颜色转换之后的灰度值必须相同
  • 如果给定的颜色 i 和 j,满足 \(R_i < R_j, G_i < G_j, B_i < B_j\),那么转换之后的灰度值必须满足 \(h_i < h_j\)

请你输出一种合法的转换方式,无解输出 -1

思路
利用严格小于关系建图,再利用拓扑排序得出答案

因为第一种颜色和第二种颜色呈现绑定关系,所以在建图时对这两种颜色一起考虑
除了第一种颜色和第二种颜色以外的颜色,依据严格小于关系建图。将第一种颜色和第二种颜色缩成一种特殊颜色进行考虑,如果其他颜色严格小于其中一种或者大于其中一种,那么可以建边一条。
在后面的拓扑排序中也仅对 \(n - 1\) 种颜色进行考虑即可

详见代码

代码

//>>>Qiansui
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x, y, sizeof(x))
#define debug(x) cout << #x << " = " << x << '\n'
#define debug2(x,y) cout << #x << " = " << x << " " << #y << " = "<< y << '\n'
//#define int long long

using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
/*

*/
const int maxm = 1e3 + 5, inf = 0x3f3f3f3f, mod = 998244353;
struct node{
	int r, g, b, id;
	bool operator < (const node x) const{
		return r < x.r && g < x.g && b < x.b;
	}
}p[maxm];
vector<int> e[maxm], ans(maxm, 0);
int in[maxm];

void solve(){
	int n;
	cin >> n;
	for(int i = 0; i < n; ++ i){
		cin >> p[i].r >> p[i].g >> p[i].b;
		p[i].id = i;
	}
	if(p[1] < p[0] || p[0] < p[1]){
		cout << "-1\n"; return ;
	}
	for(int i = 2; i < n; ++ i){
		for(int j = 2; j < n; ++ j){
			if(i != j && p[j] < p[i]){
				e[j].push_back(i);
				++ in[i];
			}
		}
		if(p[0] < p[i] || p[1] < p[i]){
			e[1].push_back(i);
			++ in[i];
		}
		if(p[i] < p[0] || p[i] < p[1]){
			e[i].push_back(1);
			++ in[1];
		}
	}
	queue<int> q;
	for(int i = 1; i < n; ++ i){
		if(in[i] == 0) q.push(i);
	}
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for(auto v : e[u]){
			ans[v] = max(ans[v], ans[u] + 1);
			-- in[v];
			if(in[v] == 0) q.push(v);
		}
	}
	cout << ans[1] << '\n' << ans[1] << '\n';
	for(int i = 2; i < n; ++ i){
		cout << ans[i] << '\n';
	}
	return ;
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int _ = 1;
	// cin >> _;
	while(_ --){
		solve();
	}
	return 0;
}
posted on 2023-08-18 17:34  Qiansui  阅读(16)  评论(0编辑  收藏  举报