首页 > 专题系列 > Java解POJ > POJ 2376 Cleaning Shifts [解题报告] Java
2013
11-11

POJ 2376 Cleaning Shifts [解题报告] Java

Cleaning Shifts

问题描述 :

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.

输入:

* 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.

输出:

* 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.

样例输入:

3 10
1 7
3 6
6 10

样例输出:

2

温馨提示:

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.

解题代码:

//* @author 
/*贪心算法
 *思想就是先按排序,从前向后一点一点找,如果第一个牛不能到1则返回-1,中间出了断档也返回-1,最后不能到t也要返回-1.
 */

import java.util.Arrays;
import java.util.Scanner;

//贪心加排序,用到了comparable接口

public class Main {
Scanner cin = new Scanner(System.in);
int n;
int t;
Cows[] cows;

public void inPut() {
   n = cin.nextInt();
   t = cin.nextInt();
   cows = new Cows[n];
   for (int i = 0; i < n; i++) {
    cows[i] = new Cows();
    cows[i].a = cin.nextInt();
    cows[i].b = cin.nextInt();
   }
   calculate();
}

private void calculate() {
   int result = 0;
   Arrays.sort(cows);
   // System.out.println(Arrays.toString(cows));
   if(cows[0].a > 1) {
    System.out.println(-1);
    return;
   }
   int temp = cows[0].b;
   int i = 0;
  
   while (temp < t && i < n) {
    int temp2 = 0;
    int flag = 0;
    while (i < n && cows[i].a <= temp + 1) {
     flag = 1;
     if (cows[i].b > temp2) {
      temp2 = cows[i].b;
     }
     i++;
    }

    if (flag == 0) {
     System.out.println("-1");
     return; 
    }
    temp = temp2;
    result++;
   }
  
   if(temp < t) {
    System.out.println("-1");
    return;
   }
   System.out.println(result + 1);
}

public static void main(String[] args) {
   new Main().inPut();
}
}

class Cows implements Comparable< Cows> {
int a;
int b;

public int compareTo(Cows o) {
   return this.a - o.a;
}

public String toString() {
   return this.a + " " + this.b;
}
}

  1. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;

  2. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  3. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  4. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n