zoukankan      html  css  js  c++  java
  • UVa 1617 Laptop (贪心)

    题意:有n个长度为1的线段,确定它们的起点,使得第i个线段在[ri,di]之间,输出空隙数目的最小值。

    析:很明显的贪心题,贪心策略是这样的,先把所有的区间排序,原则是按右端点进行排序,如果相等再按左端点排,然后再扫一遍,如果第一个区间的右端点和第二个右端点一样,

    一定可以相邻,如果不相等,再看是不是与左端点大小关系,如果小于左端点,那么就一定会产生空隙,如果不是小于,就可以,那么端点要向右移动一个单位,其他的也样判断。

    代码如下:

    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    using namespace std ;
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f3f;
    const double eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct node{
        int x, y;
        bool operator < (const node &p) const{
            return y < p.y || (y == p.y && x < p.x);
        }
    };
    node a[maxn];
    
    int main(){
        int T;  cin >> T;
        while(T--){
            scanf("%d", &n);
            for(int i = 0; i < n; ++i)  scanf("%d %d", &a[i].x, &a[i].y);
            sort(a, a+n);
            int ans = 0;
            int r = a[0].y;
            for(int i = 1; i < n; ++i){
                if(r == a[i].y)  continue;
                if(r < a[i].x){
                    ++ans;
                    r = a[i].y;
                }
                else ++r;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    [HDU]1086You can Solve a Geometry Problem too
    [HDU]2161Primes
    [HDU]2098分拆素数和
    [HDU]1431素数回文
    [HDU]1527取石子游戏
    [HDU]2092整数解
    [HDU]1405The Last Practice
    [HDU]2565放大的X
    [HDU]1723Distribute Message
    [HDU]1208Pascal's Travels
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5724226.html
Copyright © 2011-2022 走看看