zoukankan      html  css  js  c++  java
  • POJ 3067 Japan

    Japan
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25489   Accepted: 6907

    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

     
     
     
    解析:树状数组(单点更新,区间查询)。把道路进行排序(按照w从小到大排序,w相同则按e从小到大排序),这样,对于道路ri,若ej>ei(0≤j<i),则ri、rj两条道路存在交点。通过树状数组,我们可以知道道路ri前面有多少条道路的e小于等于ei,再用下标减去它即可得到ri前面道路的e大于ei的条数,总和就是结果。
     
     
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define lowbit(x) (x)&(-x)
    #define ll long long
    using namespace std;
    
    struct Road{
        int w, e;
        bool operator < (const Road& b)const
        {
            if(w != b.w) return w<b.w;
            return e<b.e;
        }
    }r[1000005];
    
    int c[1005];
    int n, m, k;
    
    void add(int x, int val)
    {
        for(int i = x; i <= 1000; i += lowbit(i))
            c[i] += val;
    }
    
    int sum(int x)
    {
        int ret = 0;
        for(int i = x; i > 0; i -= lowbit(i))
                ret += c[i];
        return ret;
    }
    
    void solve()
    {
        memset(c, 0, sizeof(c));
        sort(r, r+k);
        ll res = 0;
        for(int i = 0; i < k; ++i){
            res += i-sum(r[i].e);
            add(r[i].e, 1);
        }
        printf("%I64d
    ", res);
    }
    
    int main()
    {
        int t, cn = 0;
        scanf("%d", &t);
        while(t--){
            scanf("%d%d%d", &n, &m, &k);
            for(int i = 0; i < k; ++i)
                scanf("%d%d", &r[i].w, &r[i].e);
            printf("Test case %d: ", ++cn);
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    leetcode刷题四<寻找两个有序数组的中位数>
    leetcode刷题第三天<无重复字符的最长子串>
    leetcode刷题第二天<两数相加>
    leetcode刷题第一日<两数和问题>
    sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError)
    flask微电影系统开发中上下文处理器
    gdb解决字符串打印果断措施
    邻接表
    Jarvis OJ 一些简单的re刷题记录和脚本
    windows控件理论学习
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5726482.html
Copyright © 2011-2022 走看看