跳到正文

题解

题解:P16889 [GKS 2022 #F] Sort the Fabrics

wu_song_yu约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年6月13日
收录
2026年8月24日
更新
2026年11月10日
篇幅
约 2 分钟
题目
P16889

版本

  1. v1当前

题目传送门

思路

定义两个数组,用快速排序分别按 Ada 和 Charles 的方式排序,然后看两个数组中相同下标的物品是否相同。

代码

C++
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+10;
struct cdu{//定义结构体
	string c;
	int d,u;
}s1[N],s2[N];
bool ada(cdu x,cdu y){
	if(x.c==y.c){
		return x.u<y.u;
	}
	return x.c<y.c;
}
bool charles(cdu x,cdu y){
	if(x.d==y.d){
		return x.u<y.u;
	}
	return x.d<y.d;
}
int t,n,ans;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
	for(int i=1;i<=t;i++){
		ans=0;
		cin>>n;
		for(int j=1;j<=n;j++){
			cin>>s1[j].c>>s1[j].d>>s1[j].u;
			s2[j].c=s1[j].c,s2[j].d=s1[j].d,s2[j].u=s1[j].u;
		}
		sort(s1+1,s1+1+n,ada);
		sort(s2+1,s2+1+n,charles);
		for(int i=1;i<=n;i++){
			if(s1[i].u==s2[i].u){
				ans++;
			}
		}
		cout<<"Case #"<<i<<": "<<ans<<endl;
	}
    return 0;
}
原文