UVa11926 - Multitasking(排序问题)

Calendars control our daily lives. For people like me, who are bad at multitasking, it is important to have at most one task planned for any minute of my life. Your job is to make sure that my calendar is free of conflicts for the next one million minutes (just over 99 weeks) of my life. To keep things simple, all times are expressed in minutes from a fixed time 0 representing ``now".

\epsfbox{p11926.eps}

In this calendar, there are two types of tasks: one-time tasks and repeating tasks. One-time tasks have a start time and an end time. Repeating tasks have a start time and an end time for their first occurrence, and a repetition interval. Repeating tasks are assumed to keep repeating forever without end. For example, a repeating task with start time 5, end time 8 and repetition interval 100 would be occurring at time intervals [5..8], [105..108], [205..208], ...

Tasks are considered to be in conflict if and only if their time intervals overlap, for example [2..5] and [4..6] overlap. ``Touching" is OK, for example [2..5] and [5..6] do not overlap.

Input 

There are approximately 30 test cases. The first line of each test case contains two numbers n and m. n is the number of one-time tasks and m the number of repeating tasks. The following n lines contain two numbers each, the start and end times respectively of a one-time task. Afterward, m more lines similarly describe the repeating tasks by giving their start times, end times, and repetition intervals. Both n and m are at most 100.

All numbers are integers in the range [0..1000000]. For each task, the end time is guaranteed to be larger than the start time, and the repetition interval is larger than 0.

Input terminates with a line containing `0 0' which should not be processed.

Output 

For each test case, print a single line containing either the words ` NO CONFLICT' if there are no overlaps between any tasks for minutes 0..1000000, or ` CONFLICT' if there is at least one overlap.

Sample Input 

2 0
10 20
20 30
2 0
10 30
20 21
1 1
1000 2000
0 10 1000
0 0

Sample Output 

NO CONFLICT
CONFLICT
CONFLICT
题意:有两种任务,一种是单一任务,给定起始时间、结束时间,另一种是循环任务,给定第一个的起始时间、结束时间,时间间隔,求给出的任务是否有时间冲突

思路:1、对于单一任务,根据起始时间从小到大排列,然后看是否有重。2、对于循环任务,需要结束时间与起始时间的差应该小于时间间隔。3、循环任务与单一任务是否有冲突 4、循环任务之间是否有冲突

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 110;

struct OneTimeTask
{
	int start, end;

	bool operator < (const OneTimeTask &other) const
	{
		if (start != other.start) return start < other.start;

		return end < other.end;
	}
};

struct RepeatTask
{
	int start, end, interval;
};

int n, m;
OneTimeTask onetask[MAXN];
RepeatTask repeattask[MAXN];

bool input()
{
	scanf("%d%d", &n, &m);

	if (n == 0 && m == 0) return false;

	for (int i = 0; i < n; i++) {
		scanf("%d%d", &onetask[i].start, &onetask[i].end);
	}

	for (int i = 0; i < m; i++) {
		scanf("%d%d%d", &repeattask[i].start, &repeattask[i].end, &repeattask[i].interval);
	}
	return true;
}


void solve()
{
	bool flag = true;

	sort(onetask, onetask + n);
	for (int i = 1; i < n; i++) {
		if (onetask[i].start >= onetask[i - 1].start && onetask[i].start < onetask[i - 1].end) {
			flag = false;
			break;
		}
	}
	if (flag) {
		for (int i = 0; i < m; i++) {
			if (repeattask[i].end - repeattask[i].start > repeattask[i].interval) {
				flag = false;
				break;
			}
		}
	}
	

	if (flag) {
		for (int i = 0; i < m && flag; i++){
			int start = repeattask[i].start;
			int end = repeattask[i].end;
			int interval = repeattask[i].interval;

			while (flag) {
				if (start > 1000000 || start > onetask[n - 1].end) break;
				for (int j = 0; j < n && flag; j++) {
					if ((start >= onetask[j].start && start < onetask[j].end) || (end > onetask[j].start && end <= onetask[j].end)) flag = false;
				}
				
				start += interval;
				end += interval;
			
			}
		}
	}

	if (flag && m >= 2) {
		vector<OneTimeTask> tasks;
		for (int i = 0; i < m; i++) {
			int start = repeattask[i].start;
			int end = repeattask[i].end;
			int interval = repeattask[i].interval;

			while (1) {
				OneTimeTask tmp;
				tmp.start = start, tmp.end = end;
				tasks.push_back(tmp);
				start += interval;
				end += interval;

				if (start > 1000000) break;
				if (end > 1000000) end = 1000000;
			}
		}

		sort(tasks.begin(), tasks.end());

		for (int i = 1, size = tasks.size(); i < size; i++) {
			if (tasks[i].start >= tasks[i - 1].start && tasks[i].start < tasks[i - 1].end) {
				flag = false;
				break;
			}
		}
	}

	if (flag) printf("NO CONFLICT\n");
	else printf("CONFLICT\n");
}

int main(int argc, char **argv)
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	while (input()) {
		solve();
	}

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值