zoukankan      html  css  js  c++  java
  • 区间问题 codeforces 422c+hiho区间求差问

    先给出一个经典的区间处理方法 对每个区间 我们对其起点用绿色标识  终点用蓝色标识 然后把所有的点离散在一个坐标轴上 如下图
    这里写图片描述 
    这样做有什么意义呢。由于我们的区间可以离散的放在一条轴上面那么我们在枚举区间的时候 0(n)的复杂度就可以了 具体的操作 我们看两道题目

    http://codeforces.com/contest/822/problem/C

    C. Hacker, pack your bags!
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

    So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly xdays. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers liricosti — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.

    At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.

    Help Leha to choose the necessary vouchers!

    Input

    The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

    Each of the next n lines contains three integers liri and costi(1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.

    Output

    Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.

    Examples
    input
    4 5
    1 3 4
    1 2 5
    5 6 1
    1 2 4
    output
    5
    input
    3 2
    4 6 3
    2 4 1
    3 5 4
    output
    -1
    Note

    In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.

    In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.

     题目的意思是取两段不相交的区间 它们的长度等于x 求花费最小的取法 。

     借助上面区间离散的思想 我们把区间放在一条轴上,然后开始枚举 。怎么枚举呢?当我们遇到是一个区间的起点的时候,我们算出选取当前区间的最小花费,这里又有一个技巧,由于我们取第二个区间是之前遍历过且已经结束(遍历到终点)区间(题目要求不相交),我们可以用一个mark数组来记录遍历过的状态,然后在0(1)的复杂就可以算出当前情况的最小值(这里最好结合代码理解);当遇到的点是区间的终点的时候,我们更新一下mark数组;

    有一个trick要注意 对于断电重合的区间 在离散化到轴上的时候 由于题意要求不想交 我们在处理的时候 把起点放在终点之前。

    上代码

    #include <cstdio>
    #include <iostream>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int inf=2e9 + 5;//注意最大值。。
    struct node //还是用结构体吧
    {
        int pos;
        int flag;
        int days;
        int cost;
        node(int a,int b,int c,int d)
        {
            pos=a;
            flag=b;
            days=c;
            cost=d;
        }
    };
    int cmp(node a,node b)
    {
        if(a.pos==b.pos)
        {
            return a.flag < b.flag;
        }
        return a.pos < b.pos;
    }
    int main()
    {
        int n,k;
        cin>>n>>k;
        vector< node >vec;
        vec.clear();
        for(int i=1;i<=n;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            int temp_days=b-a+1;
            int temp_cost=c;
            vec.push_back(node(a,0,temp_days,temp_cost));
            vec.push_back(node(b,1,temp_days,temp_cost));
        }
        int mark[200011];
        memset(mark,0,sizeof(mark));
        sort(vec.begin(),vec.end(),cmp);
        int minn=inf;
        for(int i=0;i<vec.size();i++)
        {
            node temp=vec[i];
            if(temp.flag==0)// 起点的情况
            {
                int key=k-temp.days;
                if(key<=0) continue;
                if(mark[key]==0) continue;
                minn=min(minn,mark[key]+temp.cost);
            }
            else// 终点的情况
            {
                int key=temp.days;
                if(mark[key]==0 || mark[key] > temp.cost) mark[key]=temp.cost;// 更新mark数组
            }
        }
        if(minn==inf) cout<<-1<<endl;
        else cout<<minn<<endl;
        return 0;
    }

    http://hihocoder.com/problemset/problem/1305

    接下来是这道区间求差 我们要求区间是在A集合而不在B集合中 怎么处理呢?

    我们还是把所有的点离散的放在一条坐标轴上 然后用一个conta表示A contb标识B 每当遇到一个区间的起点的时候cont++ 遇到终点的时候cont-- 所以对于一个区间 当cont>0的时候 说明这个区间被覆盖

    那么对一段区间 当conta>0 &&coutb==0 就可以表示区间属于A而部署于B

    上代码

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    using namespace std;
    int cmp(pair<int,int>a,pair<int,int>b)
    {
        if(a.second==b.second) return a.first<b.first;
        return a.second < b.second;
    }
    int main()
    {
        int n,m;
        int a[300001],b[300001];
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cin>>n>>m;
        for(int i=1;i<=2*n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=2*m;i++) scanf("%d",&b[i]);
        vector<pair <int,int> > v;
        for(int i=1;i<=2*n;i+=2)
        {
            v.push_back(make_pair(1,a[i]));
            v.push_back(make_pair(2,a[i+1]));
        }
        for(int i=1;i<=2*m;i+=2)
        {
            v.push_back(make_pair(3,b[i]));
            v.push_back(make_pair(4,b[i+1]));
        }
        sort(v.begin(),v.end(),cmp);
        int conta,contb,sum;
        conta=contb=sum=0;
        for(int i=0;i<v.size();i++)
        {
            pair<int,int>temp=v[i];
            switch(temp.first)
            {
                case 1:conta++;break;
                case 2:conta--;break;
                case 3:contb++;break;
                case 4:contb--;break;
            }
            if(conta>0&&contb==0) sum+=v[i+1].second-temp.second;
        }
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    img与特殊布局下对浏览器渲染的剖析
    css属性image-redering详解
    nodejs与Promise的思想碰撞
    跨域抓取博客园RSS
    WebGL中图片多级处理(FrameBuffer)
    WebGL中的OpenGL着色器语言
    三角剖分算法(delaunay)
    用原生Canvas写贪吃蛇及问题解决
    那些在BAE上部署node.js碰到的坑
    js实现jquery的offset()
  • 原文地址:https://www.cnblogs.com/z1141000271/p/7151546.html
Copyright © 2011-2022 走看看