• 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 }
  • 相关阅读:
    图像梯度计算
    图像轮廓检测
    元组()
    SwiftUI 概览
    TCL 语言概览
    列表 []
    Numpy 矩阵计算
    图像平滑(滤波)
    language="JavaScript"与type="text/javascript"
    调用接口, Test.java(不用任何包, MD5大写32位加密,HttpURLConnection)
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6589110.html
Copyright © 2020-2023  润新知