POJ 3067 Japan(树状数组求逆序对)
来源:互联网 发布:上海数据交易中心 地址 编辑:程序博客网 时间:2024/06/08 14:04
Japan
http://poj.org/problem?id=3067
Time Limit: 1000MS
Memory Limit: 65536K
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
题意抽象:
设二分图(偶图)G<V,E>的二部划分为(V1,V2),|V1|=N,|V2|=M,|E|=K。已知V1和V2上的点分别在两条平行线上,点与点之间径直相连且每个交点最多在两条直线上,求交点个数。
解法:
复杂度:
1. 对两点集的元素排序,优先级V1>V2
const int MAXN = 1002;struct Node {int a, b;} node[MAXN * MAXN];bool cmp(Node a, Node b) {return a.a != b.a ? a.a < b.a : a.b < b.b;}sort(node + 1, node + 1 + K, cmp);
int lowbit(int x) {return -x & x;}void add(int i, int val) {while (i <= M) {c[i] += val;i += lowbit(i);}}int sum(int i) {int s = 0;while (i) {s += c[i];i -= lowbit(i);}return s;}long long ans = 0;add(node[1].b, 1);for (int i = 2; i <= K; i++) {add(node[i].b, 1);ans += sum(M) - sum(node[i].b);//统计比node[i].b大的点个数,O(log M)时间}
完整代码:
/*485ms,2760KB*/#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>using namespace std;const int MAXN = 1002;int c[MAXN];int N, M, K;struct Node {int a, b;} node[MAXN * MAXN];bool cmp(Node a, Node b) {return a.a != b.a ? a.a < b.a : a.b < b.b;}int lowbit(int x) {return -x & x;}void add(int i, int val) {while (i <= M) {c[i] += val;i += lowbit(i);}}//前i项和int sum(int i) {int s = 0;while (i) {s += c[i];i -= lowbit(i);}return s;}int main() {int T;scanf("%d", &T);for (int iCase = 1; iCase <= T; iCase++) {scanf("%d%d%d", &N, &M, &K);for (int i = 1; i <= K; i++) {scanf("%d%d", &node[i].a, &node[i].b);}///////////////////////sort(node + 1, node + 1 + K, cmp); //排序memset(c, 0, sizeof(c));long long ans = 0;add(node[1].b, 1);for (int i = 2; i <= K; i++) {add(node[i].b, 1);ans += sum(M) - sum(node[i].b);//统计比node[i].b大的点个数,O(log M)时间}printf("Test case %d: %I64d\n", iCase, 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(树状数组求逆序数)
- POJ3067:Japan(树状数组求逆序对)
- poj 3067 Japan 树状数组求逆序数
- poj--3067 Japan(树状数组+逆序数)
- POJ 3067 Japan (树状数组 + 逆序数)
- POJ 3067 Japan(树状数组+逆序数)
- POJ 3067-Japan(树状数组-逆序数)
- poj 3067(树状数组求逆序对)
- jquery 节点操作.删除和取值
- 转战到CSDN
- 第六节 CSS学习——类选择器
- 【黑马程序员】MyEclipse慢的解决
- static_cast, dynamic_cast, const_cast探讨
- POJ 3067 Japan(树状数组求逆序对)
- Adobe 公司市场专向社交化
- 【黑马程序员】this关键字
- 第七节 CSS学习——属性选择器
- Spring学习之——依赖关系和配置细节(三)
- php中global和$GLOBALS[]的分析之一
- [原创 - 尚学堂科技 - 马士兵老师]
- 【黑马程序员】HashMap遍历的两种方式
- 闪回技术11g