zoukankan      html  css  js  c++  java
  • poj2376 Cleaning Shifts

    思路:

    关于区间的贪心。注意特判。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 pair<int, int> p[25005];
     7 int n, t;
     8 
     9 int main()
    10 {
    11     cin >> n >> t;
    12     for (int i = 0; i < n; i++)
    13     {
    14         scanf("%d %d", &p[i].first, &p[i].second);
    15     }
    16     sort(p, p + n);
    17     bool flag = true;
    18     int now_end = 0;
    19     int cnt = 0;
    20     for (int i = 0; i < n; i++)
    21     {
    22         if (p[i].second <= now_end)
    23             continue;
    24         if (p[i].first - now_end > 1)
    25         {
    26             flag = false;
    27             break;
    28         }
    29         int j = i, maxn = p[j].second;
    30         while (j < n && (p[j].first <= now_end || p[j].first - now_end <= 1))
    31         {
    32             maxn = max(maxn, p[j].second);
    33             j++;
    34         }
    35         now_end = maxn;
    36         cnt++;
    37         if (now_end >= t)
    38             break;
    39         i = j - 1;
    40     }
    41     if (now_end < t)
    42     {
    43         flag = false;
    44     }
    45     cout << (flag ? cnt : -1) << endl;
    46     return 0;
    47 }
  • 相关阅读:
    将已排序的数组乱序
    Roadmap!!
    测试
    最大对称字串
    约瑟夫环问题
    大家好
    MYSQL数据库导入SQL文件出现乱码如何解决
    Hibernate缓存
    Spring备忘四(涵盖Spring2.5)
    Struts2 Hello,Wold
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6589110.html
Copyright © 2011-2022 走看看