zoukankan      html  css  js  c++  java
  • POJ3067:Japan(树状数组求逆序对)

    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)
    Sample Input

    1
    3 4 4
    1 4
    2 3
    3 2
    3 1
    Sample Output

    Test case 1: 5
    Source

    Southeastern Europe 2006

    题意:
    两边分别有n,m个点,每边点的编号从上到下位1,2,3。。。
    现在有K条边连接两边的点,最多只有两条边相交于一点而不会出现三条边以上交于一点的状况,问一共有几个交点

    思路:
    相交的情况必然是在在Li<Lj的情况下,Ri>Rj
    所以在对L,R排序之后,只需要看R里面逆序数的多少即可

    #include <stdio.h>
    #include <algorithm>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <string.h>
    #include <math.h>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    
    
    #define INF 0x3f3f3f3f
    #define LL long long
    using namespace std;
    const int  MAXN = 1e6+5;
    
    
    struct Node{
        int l;
        int r;
    
    };
    
    Node a[MAXN];
    int b[MAXN];
    int c[MAXN];
    int T,N,M,K;
    
    bool cmp(Node a,Node b)
    {
        if (a.l!=b.l)
            return a.l < b.l;
        else
            return a.r < b.r;
    }
    
    int lowbit(int x)
    {
        return x & (-x);
    }
    
    void updata(int p)
    {
        while (p<=M)
        {
            c[p] += 1;
            p += lowbit(p);
        }
    }
    
    
    int getsum(int p)
    {
        int res = 0;
        while (p)
        {
            res += c[p];
            p -= lowbit(p);
        }
        return res;
    }
    
    
    
    
    int main()
    {
        //freopen("../in.txt","r",stdin);
        scanf("%d",&T);
        int t = 1;
        while (T--)
        {
            scanf("%d%d%d",&N,&M,&K);
            memset(c,0, sizeof(c));
            for (int i=1;i<=K;i++)
            {
                scanf("%d%d",&a[i].l,&a[i].r);
            }
            sort(a+1,a+1+K,cmp);
            LL ans = 0;
            for (int i=1;i<=K;i++)
            {
                updata(a[i].r);
                ans += i-getsum(a[i].r);
            }
            printf("Test case %d: %lld
    ",t++,ans);
        }
        return 0;
    }
  • 相关阅读:
    Effective C# Item6:明辨值类型和引用类型的使用场合
    Effective C# Item15:利用using和try/finally语句来清理资源
    Effective C# Item12:变量初始化器优于赋值语句
    Effective C# Item19:定义并实现接口优于继承类型
    Effective C# Item14:利用构造器链
    Effective C# Item18:实现标准Dispose模式
    Effective C# Item17:尽量减少装箱和拆箱
    Effective C# Item7:将值类型尽可能实现为具有常量性和原子性的类型
    Effective C# Item10:理解GetHashCode()方法的缺陷
    Effective C# Item20:明辨接口实现和虚方法重写
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11267231.html
Copyright © 2011-2022 走看看