• POJ 3067 Japan(树状数组求逆序对)


    Japan
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14795   Accepted: 3968

    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

     
     
     
    题意:日本东部和西部沿海各有n,m座城市,想通过修路来连同。给出城市之间的联线,求共有多少的交点;
     
    先排序,然后求逆序对。
    可以用归并排序,也可以用树状数组。
    这里用树状数组实现。
    注意边的范围比较大,1000*1000.
    结果超int,用long long
     
    /*
    POJ 3067 Japan
    按照第一个数从小到大排序,第一个数相同则按照第二个数从小到大排序。
    然后用树状数组求逆序对。
    
    边比较大,结果要用long long
    
    
    */
    
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    using namespace std;
    const int MAXN=1010;
    int c[MAXN];
    struct Node
    {
        int a,b;
    }node[1000*1000];
    int N,M,K;
    bool cmp(Node a,Node b)
    {
        if(a.a==b.a)return a.b<b.b;
        return a.a<b.a;
    }
    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>0)
        {
            s+=c[i];
            i-=lowbit(i);
        }
        return s;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int iCase=0;
        int T;
        scanf("%d",&T);
        while(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;
            for(int i=1;i<=K;i++)
            {
                ans+=sum(M)-sum(node[i].b);
                add(node[i].b,1);
            }
            printf("Test case %d: %I64d\n",iCase,ans);
        }
        return 0;
    }
  • 相关阅读:
    java中Map遍历的四种方式
    Java从网络读取图片并保存至本地
    spring整合问题分析之Write operations are not allowed in readonly mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    element 中table表格多选,翻页或者切换不同数据后保存之前选择的数据并返显选中的状态
    nvm的安装与使用(Mac 版)
    常用正则表达式
    uniapp开发微信小程序
    关于elementui select组件change事件只要数据变化就会触发的解决办法
    elementui的表单重置方法resetFields()的坑
    Element中vfor动态数据循环表单验证的处理
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2630594.html
Copyright © 2020-2023  润新知