UVa Problem 10191 Longest Nap (最长打盹时间)

// Longest Nap (最长打盹时间)
// PC/UVa IDs: 110404/10191, Popularity: B, Success rate: average Level: 1
// Verdict: Accepted
// Submission Date: 2014-07-28
// UVa Run Time: 0.018s
//
// 版权所有(C)2014,邱秋。metaphysis # yeah dot net
//
// 前一次的算法有漏洞,经网友指出后,现在使用填充查找法来获取最长打盹时间,即将10:00
// 到18:00按分钟间隔用一个数组来表示,占用的时间标志为1,未占用的时间标志为0,将事件
// 的开始时间和结束时间之间的分钟对应的数组元素置为1,查找连续0的最大长度即为最长打
// 盹时间。

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

#define MAX_MINUTES 481
int minutes[MAX_MINUTES];   // 10:00到18:00共480分钟

struct longestNapTime
{
	int startAt;	// 打盹的开始时间,以从00:00开始的分钟数计算
	int duration;	// 打盹的持续时间,以分钟数计算
};

void reset()
{
    memset(minutes, 0, sizeof(minutes));
    minutes[0] = 0; minutes[480] = 1;
}

int getStartTime(string line)
{
	int startTime = atoi(line.substr(0, 2).data()) * 60;
	startTime += atoi(line.substr(3, 2).data());
	return (startTime - 600);
}

int getEndTime(string line)
{
    int endTime = atoi(line.substr(6, 2).data()) * 60;
	endTime += atoi(line.substr(9, 2).data());
	return (endTime - 600);
}

void fill(int counterEvents)
{
	while (counterEvents > 0)
	{
		string line;
		getline(cin, line);
		for (int i = getStartTime(line); i < getEndTime(line); i++)
		{
		   minutes[i] = 1;
		}
		counterEvents--;
	}
}

longestNapTime find()
{
    int startAt = 0, napTime = 0;
    bool startTimeSetted = false;
    longestNapTime theLongest = { startAt, 0 };

    for (int index = 0; index < MAX_MINUTES; index++)
    {
        if (minutes[index] == 0)
        {
            if (startTimeSetted == false)
            {
                startAt = index;
                startTimeSetted = true;
            }
            napTime++;
        }
        else
        {
            if (napTime > theLongest.duration)
            {
                theLongest.startAt = startAt + 600;
                theLongest.duration = napTime;
            }
            napTime = 0;
            startTimeSetted = false;
        }
    }

    return theLongest;
}

void print(int counterCases, longestNapTime aNapTime)
{
	cout << "Day #" << counterCases << ": the longest nap starts at ";
	cout << (aNapTime.startAt / 60) << ":";
	cout << (aNapTime.startAt % 60 < 10 ? "0" : "") << (aNapTime.startAt % 60);
	cout << " and will last for ";
	if (aNapTime.duration < 60)
	{
		cout << aNapTime.duration;
	}
	else
	{
		cout << (aNapTime.duration / 60) << " hours and " << (aNapTime.duration % 60);
	}
	cout << " minutes.\n";
}

int main(int ac, char *av[])
{
	int counterCases = 0, counterEvents;
	while (counterCases++, cin >> counterEvents)
	{
	    cin.ignore();
	    reset();
        fill(counterEvents);
        print(counterCases, find());
	}

	return 0;
}

UVa Problem 10191 Longest Nap (最长打盹时间)

时间: 2024-08-04 18:45:29

UVa Problem 10191 Longest Nap (最长打盹时间)的相关文章

UVa Problem 10051

这题有点类似LIS,由于颜色最多100种,所以只需建立一个100的数组,按对立面的关系以某种颜色为向上面的最大值就可以了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int cube[505][7]; 7 int max_col[105]; 8 int tmp_col[105]; 9 struct{ 10 int p; 11 int

UVA Problem D: Hartals

题目如下: Problem D: Hartals  A social research organization has determined a simple set of parameters to simulate the behavior of the political parties of our country. One of the parameters is a positive integerh (called the hartal parameter) that denot

成功也不需要太长的时间

有两个年轻人都很喜欢画画,一个年轻人很有绘画天赋,家境也很富裕,而另外的一个年轻人则资质差一些,家庭贫困.在二十多岁的时候两个入都默默无闻,很有天赋的年轻人开始不耐烦了,开始抱怨成功遥遥无期. 另一个年轻人则因为生活所迫不得不去跟人学做木匠,可是他对绘画的热爱从来没有减弱,每天无论回来得多晚.多累,他都要点亮油灯,伏案画一个小时.就是在走村串户为别人做木工活的时候,他的工具箱里也时刻装着笔墨纸砚,在休息的短暂时间里他都会找一个地方练习画画. 这个年轻人为了画画,特地花钱买了几只虾,每天早晨的时候

USACO s1.2.Milking Cows(求最长连续时间和最长间断时间)

题意:输入多个时间段,表示喂牛的时间,问喂牛的最长的持续时间和不喂牛的最长的时间. key:注意输入的时间没有先后顺序.有两种方法.一是对时间段进行排序,比较每段时间的的末尾就行了,记得求得最大连续时间的时候要更新最后的时间,更新最大间断时间的时候要更新最前面的时间.方法二,用数组标记喂牛时间,,for一遍大概就可以找到这两个时间了.下面的是方法一. /* TASK:milk2 LANG:C++ ID:huibaochen */ #include <iostream> #include <

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

[CareerCup] 18.7 Longest Word 最长的单词

5.7 Given a list of words, write a program to find the longest word made of other words in the list. 这道题给了我们一个字符串数组,让我们找到最长的那个单词是由字符串数组中的其他单词组成的,LeetCode上跟类似的题目有Word Break和Word Break II.那么我们首先来想如果是拆分两个单词怎么做,那我们要首先把所有的单词存到哈希表里,然后遍历每个单词,每个位置上都拆分成左右两个字符

UVA 10635 Prince and Princess 最长公共子序列(nlongn)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19051 题目意思是给你两串数字(计为 a,b 数组),让你求他们的最长公共子序列.数字长度是 n * n (n <= 250)所以 O(n^2) 肯定过不了了.所以想要找 O(nlongn)的. 因为题目给出了数字在 1 ~ (n^2)内并且数字不会重复.因此,对于a数组中的每个数字,如果我们都知道他在b数组中出先得位置的话(位置计为 c 数组),我们只需要在c数组里面求一