zoukankan      html  css  js  c++  java
  • 2018 徐州网络赛 G


    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of former wave in its range and remain its own trace of ( xx , 00 ) -> ( xx , yy ) and ( 00 , yy ) -> ( xx , yy ). Now the toad on the coast wants to know the total length of trace on the coast after n waves. It's guaranteed that a wave will not cover the other completely.

    Input

    The first line is the number of waves n(n le 50000)n(n50000).

    The next nn lines,each contains two numbers xx yy ,( 0 < x0<x , y le 10000000y10000000 ),the ii-th line means the ii-th second there comes a wave of ( xx , yy ), it's guaranteed that when 1 le i1i , j le njn ,x_i le x_jxixj and y_i le y_jyiyj don't set up at the same time.

    Output

    An Integer stands for the answer.

    Hint:

    As for the sample input, the answer is 3+3+1+1+1+1=103+3+1+1+1+1=10

    样例输入

    3
    1 4
    4 1
    3 3

    样例输出

    10

    题目来源

    ACM-ICPC 2018 徐州赛区网络预赛

     

    思路:

    既然新加的点会覆盖之前比他短的点,那么我们可以对平行于x轴与y轴的线按倒序维护。

    复杂度O( nlogn )

    #include <bits/stdc++.h>
    #include <bits/extc++.h>
    using namespace std;
    typedef long long ll;
    ll height(std::vector<int> v){
        int sz=v.size();
        set<int>st;
        ll ans=0;
        for (int i=sz-1; i>=0; --i){
            set<int>::iterator it = st.lower_bound(v[i]);
            if(it==st.begin())  ans+=v[i];
            else {
                it--;
                ans+=v[i]-(*it);
            }
            st.insert(v[i]);
        }
        return ans;
    }
    int main(){
        // freopen("in.txt","r",stdin);
        int x,y,n;
        std::vector<int> xx,yy;
        while(scanf("%d",&n)==1){
            xx.clear(),yy.clear();
            while(n--){
                scanf("%d%d",&x,&y);
                xx.push_back(x);
                yy.push_back(y);
            }
            printf("%lld
    ",height(xx)+height(yy));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    图解机器学习读书笔记-CH3
    塑造职场影响力的五大法宝
    怎样培养独挡一面的能力
    数据结构
    [分享]恼人的设计模式
    Git使用总结
    设计师整理的系统开发流程-简洁又有重点
    JavaScript中的String对象
    python高效解析日志入库
    如何让js不产生冲突,避免全局变量的泛滥,合理运用命名空间
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9622450.html
Copyright © 2011-2022 走看看