您的位置:首页 > 其它

cdojR - Japan

2016-05-03 18:47 316 查看

地址: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 InputSample 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[2000000+5];
long long  temp[2000000+5];
long long  cnt=0;//逆序对的个数
void merge(int left,int mid,int right)
{
int i=left,j=mid+1,k=0;
while (( i<=mid )&& (j<=right))
if (a[i]<=a[j]) temp[k++]=a[i++];
else
{
cnt+=mid+1-i;//关键步骤
temp[k++]=a[j++];
}
while (i<=mid) temp[k++]=a[i++];
while (j<=right) temp[k++]=a[j++];
for (i=0,k=left; k<=right;) a[k++]=temp[i++];
}
void mergeSort(int left,int right)
{
if (left<right)
{
int mid=(left+right)/2;
mergeSort(left, mid);
mergeSort(mid+1, right);
merge(left, mid, right);
}
}
int main (void)
{
int n,u,v,t;
cin>>t;
for(int i=1;i<=t;i++)
{
p.clear();
cnt=0;
cin>>u>>v>>n;
for(int i=0; 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=0; i<n; i++)
a[i]=p[i].second;
mergeSort(0,n-1);
printf("Test case %d: %lld\n",i,cnt);
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: