• POJ 3067 Japan


    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
    PKU
    树状数组求逆序数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct node
    {
        int x;
        int y;
    }TA;
    TA ta[1000003];
    int n,m;
    __int64 c[1003];
    int cmp(const void *a,const void *b)
    {
        if((*(TA*)a).x==(*(TA*)b).x)
           return (*(TA*)a).y-(*(TA*)b).y;
         return (*(TA*)a).x-(*(TA*)b).x;
    }
    int lb(int x)//又是这样的树状数组模板
    {
        return x&(-x);
    }
    void updata(int i)
    {
      for(;i<=m;i+=lb(i))
         c[i]+=1;
    }
    __int64 rs(int i)
    {
        __int64 sum=0;
      for(;i>0;i-=lb(i))
         sum+=c[i];
        return sum;
    }
    int main()
    {
       int i,t,k,nu=1;
       __int64 sum;//这里要注意,会超出int
       scanf("%d",&t);
       while(t--)
       {
         memset(c,0,sizeof(c));
         sum=0;
         scanf("%d%d%d",&n,&m,&k);
         for(i=0;i<k;i++)
          scanf("%d%d",&ta[i].x,&ta[i].y);
         qsort(ta,k,sizeof(ta[0]),cmp);
          for(i=0;i<k;i++)
           {
               sum+=i-rs(ta[i].y);
               updata(ta[i].y);
     
           }
         printf("Test case %d: %I64d\n",nu++,sum);
     
       }
        return 0;
    }
                                                                          ------江财小子
  • 相关阅读:
    windows开启PostgreSQL数据库远程访问
    Git使用介绍
    linux 常用工具记录及简介
    ubuntu18 安装坑点记录(华硕飞行堡垒)
    快手自动视频随机点赞脚本
    接触手机脚本编程------基于触动精灵的lua编程
    使电脑蜂鸣器发声小脚本
    tensorflow--非线性回归
    python笔记--------numpy
    python笔记--------二
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421702.html
Copyright © 2020-2023  润新知