poj 2376 Cleaning Shifts

来源:http://www.cnblogs.com/ZefengYao/archive/2016/08/26/5810966.html
-Advertisement-
Play Games

Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18151 Accepted: 4620 Description Farmer John is assigning some of his N (1 ...


                                                                                             Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18151   Accepted: 4620

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T 

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

INPUT DETAILS: 

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

OUTPUT DETAILS: 

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

Source

USACO 2004 December Silver         貪心演算法 題解:給定T個時間區間,區間範圍[1,T],不同牛有不同的工作時間,求至少多少頭牛工作可以覆蓋這個區間。        首先以牛開始工作的時間先後順序排序,之後不斷迴圈更新起點=終點+1,在開始工作時間能覆蓋起點的牛中,每次選出一頭工作時間最晚的牛,更新終點        具體AC代碼:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int N_max = 25000;
pair<int, int>cows[N_max];
int N, T;
bool cmp(const pair<int, int>&a, const pair<int, int>&b) {
    return (a.first<b.first||(a.first==b.first&&a.second>b.second));
}
int solve() {
    int used_cows = 0;
    int end = 0, num = 0;
    while (end < T) {
        int begin = end + 1;//此時的end是上一頭牛的工作結束時間,此時的begin為當前的牛工作開始時間要在begin之前
        for (int i = num;i < N;i++) {//選出新的一頭牛,使得工作結束時間越晚越好
            if (cows[i].first <= begin) {
                if (cows[i].second >= begin)//別忘加等於,有可能牛的工作區間只有1個,譬如3-3
                    end = max(end, cows[i].second);//在能選的牛中選一條,使得工作的時間到最晚

            }
            else {
                num=i;//沒有符合要求的牛可以挑選了,換新牛
                break;
            }
        }
        //判斷這選出來的牛是否符合要求,即它的工作結束時間必須要大於begin,否則之間有區間不能被覆蓋
        if (begin > end) {//此時begin是大於等於當前挑選出來的牛的開始時間,而end是當前牛的工作結束時間
            return -1;
        }
        else used_cows++;
    }
    return used_cows;
}
int main() {
    scanf("%d%d", &N, &T);
        for (int i = 0;i < N;i++)
        scanf("%d%d", &cows[i].first, &cows[i].second);
        sort(cows, cows + N,cmp);
        cout << solve() << endl;
    return 0;
}

 

       
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 自定義模板引擎類 MyTpl.class.php 1 <?php 2 class MyTpl 3 { 4 private $tpl_vars = array(); 5 //分配 6 public function assign($key,$value){ 7 $this->tpl_vars[$key ...
  • 設計模式重點在於代碼風格的實現,使項目易於開發維護以及更新,同時,在底層代碼中存在著各種設計模式,使之易於拓展。總而言之,學會設計模式非常重要,在學習了《Head first 設計模式》之後,根據個人見解將裡面的知識與個人知識經驗結合提煉出來,方便以後自己回頭查閱複習,也同大家一起學習,如有不足,歡 ...
  • 首先如果TCP學過以後,再看UDP進行數據傳輸也是大同小異的,只是用到的類不同 UDP進行傳輸需要DataSocket和Datapacket類,Datapacket叫數據報,每一個數據報不能大於64k,都記錄著數據信息,發送端的IP、埠號, 以及要發送到的接收端的IP、埠號。 UDP進行傳輸是將 ...
  • 學Kotlin其實要看:http://kotlinlang.org/docs/kotlin docs.pdf 線上版是不完整的!!!少了一些章節,會有點難看懂後面的文檔。 我選擇了WordPress里的錯誤消息管理類wp error.php為對象,沒有依賴其他具體場景和類,所以比較適合移植和對比。 ...
  • ASA的美國總統競選 在這個大選之年,美國統計協會(ASA)將學生競賽和總統選舉放在一起,將學生預測誰是2016年總統大選的贏家準確的百分比作為比賽點。詳情見: http://thisisstatistics.org/electionprediction2016/ 獲取數據 互聯網上有很多公開的民調 ...
  • ...
  • 歐拉函數,就是歐拉發現的一個關於求素數的的公式,然後我們編個函數實現這個公式。 歐拉發現求小於等於n的正整數中有多少個數與n互質可以用這個公式: euler(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…(1-1/pn),其中p1,p2……pn為x的所有素因數,x是不為0 ...
  • 本文為flask-wtf中的幾乎所有常用API實現,併在實例中運用flask-wtf包,同時包含了作者在學習開發階段的一些心得體會。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...