POJ 3067 Japan
来源:互联网 发布:聊天室源码 编辑:程序博客网 时间:2024/05/21 13:56
Japan
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16738 Accepted: 4489
Description
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.
Input
The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.
Output
For each test case write one line on the standard output:
Test case (case number): (number of crossings)
Test case (case number): (number of crossings)
Sample Input
13 4 41 42 33 23 1
Sample Output
Test case 1: 5
题目大意:
就是东西两边各有n,m个城市,而且城市的序号都是从上到下标记好的(注意输入的时候不一定是按照由小到大的顺序输入的),他们之间通过高速路连接,问高速路有多少个交叉口。
解析:
把一侧的城市排序后,就变成了求另一侧的逆序数的问题了,因为有多少个逆序就有多少个交叉点。鉴于直接求逆序数的话必然超时,所以可以用树状数组来求解。(树状数组不会的童鞋自己看下大牛的链接吧http://www.cppblog.com/Ylemzy/articles/98322.html,然后可以再看下http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html,这里详细解释了为什么树状数组能求逆序),然后就是树状数组的注意点了,会在程序中注释
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int maxn;long long c[1005];struct edget{int a,b;}e[1000100];int lowbit(int x)//求得是x的二进制的最后一位{return x&(-x);}void update(int x){int i = x;while(i <= maxn)//因为要更新的话就需要把结点上层的包含此节点的c[i]全部更新,所以就是i+=lowbit(i){c[i] += 1; //此处因为求逆序数,所以更新为加1i += lowbit(i);}}long long sum(int x){long long ans = 0;for(int i = x ; i > 0 ; i -= lowbit(i))ans += c[i];//ans就是i前面的数的和return ans;}int cmp(const edget x , const edget y){if(x.a == y.a )return x.b < y.b ;elsereturn x.a < y.a;}int main()//先排序左边的数字,则问题转化成求右边的逆序数{int t,temp;scanf("%d",&t);temp = t;while(t--){memset(c,0,sizeof(c));int n,m,k;long long ans = 0 ;scanf("%d%d%d",&n,&m,&k);maxn = m;int i;for(i = 0 ; i < k ; i ++)scanf("%d %d",&e[i].a ,&e[i].b );sort(e,e+k,cmp);for(i = 0 ; i < k ; i ++ ){update(e[i].b);ans += i + 1- sum(e[i].b) ;//i + 1- sum(e[i].b)就是逆序数,因为是从i = 0开始的, //此处是i+1-sum,若是从1开始就是i-sum(e[i].b)}printf("Test case %d: %lld\n",temp-t,ans);}return 0;}
- POJ 3067 Japan
- poj 3067 Japan
- POJ 3067 - Japan
- poj 3067 Japan
- Poj 3067 Japan
- poj 3067 Japan
- POJ 3067 Japan
- Poj 3067 Japan
- POJ 3067 Japan
- POJ 3067 Japan
- POJ-3067-Japan
- POJ 3067 Japan
- POJ 3067 Japan
- poj 3067 Japan
- POJ 3067 Japan
- POJ 3067 Japan
- poj 3067 Japan
- POJ 3067 Japan
- Its not about the outage
- hdu 1372 Knight Moves 骑士的移动 bfs--马走日
- 1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- HDU 3605 Escape (二分图的多重匹配)
- CursorWindow can not be created due to cursor [memory] leak
- POJ 3067 Japan
- 编程之美系列之寻找最大的K个数
- 传智播客学习感受
- jax-ws webservice部署到tomcat 遇到的问题
- 基于AT89S52单片机的可控点阵
- 问题一百三十:字符矩阵排序
- hdu 1867 (KMP)
- Allegro最新资料大全
- Android-sqlite数据库存取图片信息