Section 1.2 Milking Cows

Section 1.2第一节的阅读材料主要介绍了全局搜索的一些知识,这一section问题的解决方法基本都与此有关;

题目原文:

Milking Cows

Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

  • The longest time interval at least one cow was milked.
  • The longest time interval (after milking starts) during which no cows were being milked.

PROGRAM NAME: milk2

INPUT FORMAT

Line 1:The single integer
Lines 2..N+1:Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in)

3
300 1000
700 1200
1500 2100

OUTPUT FORMAT

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

SAMPLE OUTPUT (file milk2.out)

900 300

分析

本题分别给出若干个挤奶工开始工作的时间和结束工作的时间,要求求出持续最长的挤奶时间和最长的空档时间。由于输入的挤奶工开始工作和结束工作的时间并不是按时间顺序排列的,所以需要对输入的数据按照开始时间先进行排序,然后依次对比当前结束工作时间和下一个开始时间即可得到最长持续时间和空档时间。 

提交代码如下

/*
ID: 
PROG: milk2
LANG: C++
*/
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

struct milktime 
{
	int start;
	int end;
};

bool compare(milktime mk1,milktime mk2)
{
	return mk1.start < mk2.start;
}

bool isIn(milktime t1,milktime t2)
{
	return t1.start >= t2.start && t1.end <= t2.end;
}

bool isCross(milktime t1,milktime t2)
{
	return t1.start >= t2.start &&t1.start <= t2.end && t1.end > t2.end;
}

bool isIdle(milktime t1, milktime t2)
{
	return t1.start > t2.end;
}


int main()
{
	ifstream fin("milk2.in");
	int n;fin >> n;
	vector<milktime> mktime(n);
	for (int i=0;i!=n;i++)
	{
		fin >> mktime[i].start >> mktime[i].end;
	}

	sort(mktime.begin(),mktime.end(),compare);
	int cont = 0;
	int idle = 0;

	if(n==1)
	{
		cont = mktime[0].end - mktime[0].start;
	}

	milktime cur = mktime[0];
	for (int i=1;i!=n;i++)
	{
		if(mktime[i].start <= cur.end && mktime[i].end > cur.end)
		{
			cur.end = mktime[i].end;
		}
		if(mktime[i].start > cur.end)
		{
			if(cur.end - cur.start > cont)
			{
				cont = cur.end - cur.start;
			}
			if(mktime[i].start - cur.end > idle)
			{
				idle = mktime[i].start - cur.end;
			}
			cur.start = mktime[i].start;
			cur.end = mktime[i].end;
		}
	}
	if(cur.end - cur.start > cont)
	{
		cont = cur.end - cur.start;
	}
	ofstream fout("milk2.out");
	fout << cont<<" " << idle << endl;

}

提交结果

TASK: milk2
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.008 secs, 3496 KB]
   Test 2: TEST OK [0.005 secs, 3496 KB]
   Test 3: TEST OK [0.005 secs, 3496 KB]
   Test 4: TEST OK [0.005 secs, 3496 KB]
   Test 5: TEST OK [0.008 secs, 3496 KB]
   Test 6: TEST OK [0.003 secs, 3496 KB]
   Test 7: TEST OK [0.005 secs, 3496 KB]
   Test 8: TEST OK [0.008 secs, 3496 KB]

All tests OK.

官方参考答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXMILKING 5000

typedef struct Milking	Milking;
struct Milking {
    int begin;
    int end;
};

Milking milking[MAXMILKING];
int nmilking;

int
milkcmp(const void *va, const void *vb)
{
    Milking *a, *b;

    a = (Milking*)va;
    b = (Milking*)vb;

    if(a->begin > b->begin)
	return 1;
    if(a->begin < b->begin)
	return -1;
    return 0;
}

void
main(void)
{
    FILE *fin, *fout;
    int i, j, t, tmilk, tnomilk;
    Milking cur;

    fin = fopen("milk2.in", "r");
    fout = fopen("milk2.out", "w");
    assert(fin != NULL && fout != NULL);

    /* read input, sort */
    fscanf(fin, "%d", &nmilking);
    for(i=0; i<nmilking; i++)
	fscanf(fin, "%d %d", &milking[i].begin, &milking[i].end);

    qsort(milking, nmilking, sizeof(Milking), milkcmp);

    /* walk over list, looking for long periods of time */
    /* tmilk = longest milking time */
    /* tnomilk = longest non-milking time */
    /* cur = current span of milking time being considered */

    tmilk = 0;
    tnomilk = 0;
    cur = milking[0];
    for(i=1; i<nmilking; i++) {
	if(milking[i].begin > cur.end) {	/* a down time */
	    t = milking[i].begin - cur.end;
	    if(t > tnomilk)
		tnomilk = t;

	    t = cur.end - cur.begin;
	    if(t > tmilk)
		tmilk = t;

	    cur = milking[i];
	} else {	
	    if(milking[i].end > cur.end)
		cur.end = milking[i].end;
	}
    }

    /* check final milking period */
    t = cur.end - cur.begin;
    if(t > tmilk)
	tmilk = t;

    fprintf(fout, "%d %d\n", tmilk, tnomilk);
    exit(0);
}

此为,官方还给出另一种方法,不同于之前只对开始时间进行排序,这种方法将开始时间和结束时间一起排序,这种方法在处理较大的输入数据时有优势;
代码如下:

/* sort the starting and ending times, then go through them from
 start to finish, keeping track of how many farmers are milking
 between each "event" (a single farmer starting and stopping). */

#include <fstream.h>
#include <stdlib.h>

struct event
{
 long seconds;   /* seconds since 5 am */
 signed char ss; /* start = 1, stop = -1 (delta number of farmers milking)
*/
};

int eventcmp (const event *a, const event *b)
{
 if (a->seconds != b->seconds)
  return (a->seconds - b->seconds); /* 300 before 500 */

 return (b->ss - a->ss); /* 1 (start) before -1 (stop) */
}

int main ()
{
 ifstream in;
 ofstream out;

 in.open("milk2.in");
 out.open("milk2.out");

 int num_intervals, num_events, i;
 event events[5000 * 2];

 in >> num_intervals;
 num_events = num_intervals * 2;
 for (i = 0; i < num_intervals; ++i)
 {
  in >> events[2*i  ].seconds; events[2*i  ].ss = 1;
  in >> events[2*i+1].seconds; events[2*i+1].ss = -1;
 }

 qsort(events, num_events, sizeof(event),
  (int(*)(const void*, const void*)) eventcmp);

/* for (i = 0; i < num_events; ++i)
  out << events[i].seconds
    << (events[i].ss == 1 ? " start" : " stop") << endl; */

 int num_milkers = 0, was_none = 1;
 int longest_nomilk = 0, longest_milk = 0;
 int istart, ilength;

 for (i = 0; i < num_events; ++i)
 {
  num_milkers += events[i].ss;

  if (!num_milkers && !was_none)
  {
   /* there are suddenly no milkers. */
   ilength = (events[i].seconds - istart);
   if (ilength > longest_milk)
    longest_milk = ilength;
   istart = events[i].seconds;
  }
  else if (num_milkers && was_none)
  {
   /* there are suddenly milkers. */
   if (i != 0)
   {
    ilength = (events[i].seconds - istart);
    if (ilength > longest_nomilk)
     longest_nomilk = ilength;
   }
   istart = events[i].seconds;
  }

  was_none = (num_milkers == 0);
 }

 out << longest_milk << " " << longest_nomilk << endl;

 return 0;
}


The end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值