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 NInput
TOutput
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[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
相关文章推荐
- list集合需要深入了解的问题
- 数据库创建emp和dept练习表
- 链表-1 链表中数据的删除
- 常用正则表达式
- Python pass 语句
- 安卓自定义画布 图片,文字相结合成图片 手势放大缩小,文字颜色大小自定义
- jQuery学习之jQuery Ajax用法详解
- 【NGUI】Rpg对话框
- HDU 2222 Keywords Search
- OC面向对象—继承
- linux下php redis扩展安装
- 线程通信
- 一定要看的。深入理解Objective-C:Category
- NLP's score meaning
- 淘宝的ruby镜像已无人维护,使用ruby-china的RubyGems镜像
- 关于委托的一个简单小练习
- [GDOI2016旅游(QwQ~)记]
- 大数据应用
- R语言学习四 图像的绘制一
- cdojQ - 昊昊爱运动 II