cdojR - Japan

时间:2023-02-12 15:45:55

地址:http://acm.uestc.edu.cn/#/contest/show/95

题目:

R - Japan

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status
N

Input

T

Output

For each test case write one line on the standard output: Test case (case number): (number of crossings)

Sample input and output

Sample Input Sample Output
1
3 4 4
1 4
2 3
3 2
3 1
Test case 1: 5

Hint

The data used in this problem is unofficial data prepared by pfctgeorge. So any mistake here does not imply mistake in the offcial judge data.

思路:

又是逆序对,具体的不多说了,和前面的某题一样,,,

归并求逆序对数,,

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cstdlib>
#include <string> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; vector<pair<long long,long long > >p;
long long a[+];
long long temp[+];
long long cnt=;//逆序对的个数
void merge(int left,int mid,int right)
{
int i=left,j=mid+,k=;
while (( i<=mid )&& (j<=right))
if (a[i]<=a[j]) temp[k++]=a[i++];
else
{
cnt+=mid+-i;//关键步骤
temp[k++]=a[j++];
}
while (i<=mid) temp[k++]=a[i++];
while (j<=right) temp[k++]=a[j++];
for (i=,k=left; k<=right;) a[k++]=temp[i++];
}
void mergeSort(int left,int right)
{
if (left<right)
{
int mid=(left+right)/;
mergeSort(left, mid);
mergeSort(mid+, right);
merge(left, mid, right);
}
}
int main (void)
{
int n,u,v,t;
cin>>t;
for(int i=;i<=t;i++)
{
p.clear();
cnt=;
cin>>u>>v>>n;
for(int i=; i<n; i++)
{
long long k,b;
scanf("%lld%lld",&k,&b);
p.push_back(make_pair(k,b));
}
sort(p.begin(),p.end());
for(int i=; i<n; i++)
a[i]=p[i].second;
mergeSort(,n-);
printf("Test case %d: %lld\n",i,cnt);
}
return ;
}