简体   繁体   English

如何检查两个或更多个时间段在c ++中是否相互重叠?

[英]How to check if two or more time periods overlap each other in c++?

I am using std::pair to get start time and end time of each time periods and I put all those time periods in an array (say classes[no_of_time_periods]). 我正在使用std :: pair获取每个时间段的开始时间和结束时间,并将所有这些时间段放入数组中(例如,classes [no_of_time_periods])。 Code for it: 代码:

int getClassTimes(int N)
{
   int subsets, startTime, endTime;
   std::pair<int,int> classes[N];
   for(int i=0;i<N;i++)
   {
       printf("Please Enter Class %d Start Time and End Time:",(i+1));
       scanf("%d",&startTime);
       scanf("%d",&endTime);
       classes[i] = std::make_pair(startTime,endTime);
   }
   subsets = compute(classes,N);
   return subsets;
}

I know I can check if two time periods overlap or not using following condition: 我知道我可以使用以下条件检查两个时间段是否重叠:

if(!(classes[i].first<classes[j].second && classes[i].second>classes[j].first))

But I want to check for more than two time periods. 但是我想检查两个以上的时间段。 Example: 例:

Input:
No_Of_Time_Periods = 5
Time_Period 1      = 1 to 3
Time_Period 2      = 3 to 5
Time_Period 3      = 5 to 7
Time_Period 4      = 2 to 4
Time_Period 5      = 4 to 6

Calculation(Number of subsets of non-overlapping Time Periods):
**Note: If end time of one class is start time of other, they are non-overlapping.
Ex((1 to 3) and (3 to 5) are non-overlapping.)**

(1 to 3)(3 to 5)
(1 to 3)(3 to 5)(5 to 7)
(1 to 3)(4 to 6)
(1 to 3)(5 to 7)
(2 to 4)(4 to 6)
(2 to 4)(5 to 7)
(3 to 5)(5 to 7)

Output:
Total Number of subsets of non-overlapping classes:7

I found the logic and I wrote the code.But while submitting in Online Judge(SPOJ), it says "Time Limit Exceeded".So, Obviously my code was not well optimized. 我找到了逻辑,然后编写了代码。但是在Online Judge(SPOJ)中提交时,它显示“ Time Limited Exceeded”。因此,显然我的代码没有得到很好的优化。 How to achieve this using c++ with better performance? 如何使用具有更好性能的c ++实现此目标? Any Help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance! 提前致谢!

This is so confusing! 真是令人困惑! The overlap test should be like so: 重叠测试应如下所示:

if ((classes[i].second < classes[j].first) || (classes[i].first > classes[j].second))
  printf("no overlap");

if (!((classes[i].second < classes[j].first) || (classes[i].first > classes[j].second)))
  printf("overlap");

assuming classes[n].first is always less than classes[n].second 假设classes[n].first classes[n].second总是小于classes[n].second

check out interval trees. 检出间隔树。 You will get logarithmic complexity: http://en.wikipedia.org/wiki/Interval_tree 您将获得对数复杂度: http : //en.wikipedia.org/wiki/Interval_tree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 opencv:在c / c ++中检查两个iplimages是否彼此相等? - opencv: check if two iplimages is equal to each other in c/c++? 调整窗口大小时,如何防止两个以上的组件相互重叠? - How to prevent two for more components to overlap each other when resizing window? 我如何制作一个 function 来检查一个单词是否在一个向量中重复超过两次或更多次,以及 output 它重复的次数? 在 C++ - How could I craft a function that check if a word is repeated more than two time or more in a vector and output the number of time it repeated? in C++ C ++:如何在同一个.cpp上声明两个类在编译时“看到”? - C++: How can I make two classes declared on the same .cpp “see” each other at compile time? 如何在C ++中显示彼此相邻的两个函数? - How to display two functions next to each other in C++? C ++如何删除引用彼此的两个类指针 - C++ how to delete two pointers of classes referencing each other 如何连接或链接两个 c++ 程序? - How to connect or link two c++ programs with each other? 如何使用c ++将两台计算机的时钟相互同步 - how to sync the clock of two computer with each other using c++ 如何有两个相互调用C ++的函数 - How to have two functions that call each other C++ C ++:两个需要彼此的类 - C++: Two classes needing each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM