20分,实在没招了,求大佬帮忙
查看原帖
20分,实在没招了,求大佬帮忙
1560932
chipichapachipichapa楼主2025/7/26 14:42
#include<bits/stdc++.h>
using namespace std;
map<int,int>h_w;
int n,h,w;
int cnt;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>h>>w;
		h_w[h]=w;
		h_w[h_w[h]]=h;
	}
//	for(int i=1;i<=n;i++)
//	{
//		cout<<h_w[h_w[i]]<<" ";
//	}
	for(int i=0;i<n;i++)
	{
		if(h_w[h_w[i]]<h_w[h_w[i+1]])
		{
			swap(h_w[h_w[i]],h_w[h_w[i+1]]);
			cnt++;
		}
		else
		{
			if(h_w[i]<h_w[i+1])
			{
				swap(h_w[i],h_w[i+1]);
				cnt++;
			}
		}
	}
//	for(int i=0;i<n;i++)
//	{
//		cout<<h_w[h_w[i]]<<" ";
//	}
//	cout<<endl;
	cout<<cnt<<endl;
	return 0;
}

2025/7/26 14:42
加载中...