Cinema CodeForces - 670C(对sort自定义排序的进一步理解)

以前一直误以为在自定义cmp函数时,只能利用结构体内的变量去定义排序规则,今天才知道原来只要cmp里面含有结构体变量,利用该变量在其他任意数组,容器的关系都是可以自定义排序规则的,真神奇,sort()函数真是强大!

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int a[maxn];
map<ll,int>mp;
struct node{
	int b,c,id;
}x[maxn];
bool cmp(node x,node y){
	if(mp[x.b]==mp[y.b])return mp[x.c]>mp[y.c];
	return mp[x.b]>mp[y.b];
} 
int main(){
	int n,m;
	int maxx=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		mp[a[i]]++;
		maxx=max(maxx,mp[a[i]]); 
	}
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>x[i].b;
		x[i].id=i;
	}
	for(int i=1;i<=m;i++)cin>>x[i].c;
	sort(x+1,x+1+m,cmp);
	cout<<x[1].id<<endl;
}
原创文章 38 获赞 7 访问量 1732

猜你喜欢

转载自blog.csdn.net/Alanrookie/article/details/106151487