zoukankan      html  css  js  c++  java
  • 贪心:zoj3953 Intervals

    Description

    Chiaki has n intervals and the i-th of them is [liri]. She wants to delete some intervals so that there does not exist three intervals ab and c such that a intersects with bb intersects with c and c intersects with a.

    Chiaki is interested in the minimum number of intervals which need to be deleted.

    Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.

    Input

    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.

    Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ nli≠ lj or ri ≠ rj.

    It is guaranteed that the sum of all n does not exceed 500000.

    Output

    For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.

    Sample Input

    1
    11
    2 5
    4 7
    3 9
    6 11
    1 12
    10 15
    8 17
    13 18
    16 20
    14 21
    19 22
    

    Sample Output

    4
    3 5 7 10

    题意:

      给你n个区间的左右端点,让你取走一些区间,使得任何三个区间都不两两相交。

    思路:

      贪心,首先将所有区间以左端点从小到大排序,然后每次判断当前的三个区间是否两两相交。
      如果两两相交则删除右端点值最大的那个区间,因为这样对后面的影响会最小。
      如果不相交则删除最左侧的区间然后继续添加区间,每三个判断一次。

    代码:

    #include <bits/stdc++.h>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    
    #define IO ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    const long long INF = 0x3f3f3f3f;
    const long long mod = 1e9+7;
    const double PI = acos(-1.0);
    const double wyth=(sqrt(5)+1)/2.0;
    const char week[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    const char month[12][10]= {"Janurary","February","March","April","May","June","July",
                               "August","September","October","November","December"
                              };
    const int daym[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
    using namespace std;
    const int maxn = 50005;
    int ans[maxn],cnt;
    struct node
    {
        int l,r;
        int id;
    } a[maxn], tmp[5];
    bool cmp1(node a, node b)//按起点从小到大排序
    {
        if(a.l == b.l)
            return a.r < b.r;
        return a.l < b.l;
    }
    bool cmp2(node a, node b)//按终点从大到小排列
    {
        return a.r > b.r;
    }
    bool f(node x, node y, node z)//判断是否相交
    {
        return y.l <= x.r && z.l <= y.r && z.l <= x.r;
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cnt = 0;
            int n;
            cin>>n;
            for(int i = 1; i <= n; i++)
            {
                cin>>a[i].l>>a[i].r;
                a[i].id = i;
            }
            sort(a+1, a+n+1, cmp1);
            tmp[1] = a[1];
            tmp[2] = a[2];
            for(int i = 3; i <= n; i++)
            {
                tmp[3] = a[i];
                sort(tmp+1, tmp+4, cmp1);
                //如果两两相交,记录下来,然后将最右侧的区间交换到tmp[3]
                if(f(tmp[1], tmp[2], tmp[3]))
                {
                    sort(tmp+1, tmp+4, cmp2);
                    ans[cnt++] = tmp[1].id;
                    swap(tmp[1], tmp[3]);
                }
                //如果不相交,那么将最左侧的区间交换到tmp[3];
                else
                    sort(tmp+1, tmp+4, cmp2);
            }
            sort(ans, ans+cnt);
            cout<<cnt<<endl;
            for(int i = 0; i < cnt; i++)
                cout<<ans[i]<<" ";
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Spring boot unable to determine jdbc url from datasouce
    Unable to create initial connections of pool. spring boot mysql
    spring boot MySQL Public Key Retrieval is not allowed
    spring boot no identifier specified for entity
    Establishing SSL connection without server's identity verification is not recommended
    eclipse unable to start within 45 seconds
    Oracle 数据库,远程访问 ora-12541:TNS:无监听程序
    macOS 下安装tomcat
    在macOS 上添加 JAVA_HOME 环境变量
    Maven2: Missing artifact but jars are in place
  • 原文地址:https://www.cnblogs.com/aiguona/p/8872389.html
Copyright © 2011-2022 走看看