rankdiff题解

temp 2026-10-2 21:57:16 38 浏览 0 点赞 0 收藏

水题

不说废话了

首先,这道题用到了排序

但是

需要用到多个参数

如 id,rankid,rank 等

这时可以用sortsort函数来解决

sort(first,end,cmp);

这么一个形式

于是我想到了一个用STL的pairpair来解决这个问题

pair<type1,type2> p;

这里,我们可以用.first.first和.second.second来访问

完整代码

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
using LL = long long;
const int inf = 1e9, N2 = 1e3, mod = 998244353;
#define PII pair<int,int>
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
auto compare = [](int a, int b){ return a > b; };
PII a[N];
bool cmp(PII a,PII b){
	if(a.first != b.first)return a.first > b.first;
	return a.second < b.second;
}
void solve(){
	freopen("rankdiff.in","r",stdin);
	freopen("rankdiff.out","w",stdout);
	int n,p,q;
	cin>>n>>p>>q;
	for(int i = 0;i < n;i++){
		cin>>a[i].first;
		a[i].second = i;
	}
	sort(a,a+n,cmp);
	cout<<abs(a[p-1].second-a[q-1].second);
}
int main(){
	int _ = 1;
	while(_--){
		solve();
	}
	return 0;
}

评论

0 条
还没有评论。