CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

Help! topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 01-21-2012, 10:03 AM
Junior Member
 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
eveline is on a distinguished road
| More
Help!

Hello!

I'm new here, but for a long time following this forum. I got task for some project to do, but I really don't know how to start. If someone can help me, I would be grateful. I didn't do anything. I don't realize this task and I don't know how to start, I don't know what I need to do in this task, or anything. I need the whole code. If anyone can solve this, please help me, it's an emergency.

___________________________

You are given N open intervals on a line (the intervals do not include their endpoints). Your task is to write a program which finds the largest number of intervals that can be chosen so they don't intersect.

INPUT:
The first line of the standard input contains a number N (0<N<=5000), the number of intervals. The next N lines contain two integers li and ri (-10000< li < di <10000) which represent the left and right ends of the interval (1<=i<=N).

OUTPUT:
To the standard output in one line write the number of intervals which can be chosen so that they do not intersect.

Input:
4
-1 1
0 5
2 3
5 9

Output:
3

P.S. The program is required to implement function, which checks whether two intervals have a common point: bool check (int beg1, int end1, int beg2, int end2).I am suffering with this for a long time and I can not resolve it, the task should be solved using the iostream library.
Reply With Quote
  #2  
Old 01-27-2012, 06:49 PM
hobbyist's Avatar
Junior Member
 
Join Date: Jan 2012
Posts: 28
Thanks: 1
Thanked 7 Times in 6 Posts
hobbyist is on a distinguished road
| More
Re: Help!

c++ is a bit advanced for me, but I *think* you can use a vector

declare a 2d vector
Code:
std::vector<std::vector<int> > intervals;
add some data
Code:
loop(while input is stored) {
   intervals.push_back(std::vector<int>(2)); // prepare a set of end pts
   // request end pt 1 and end pt 2
   std::cin >> intervals[loop_counter][0] // end pt 1
   std::cin >> intervals[loop_counter][1] // end pt 2
}

I *think* the function would want to check intervals[0][0], intervals[0][1] against the other sets in the vector, so use a loop starting at index pos 1
Code:
for(int i=1; i < intervals.size(); ++i) {
   if(check(intervals[0][0], intervals[0][1], intervals[i][0], intervals[i][1]) == false)
      ++count;
}
oops; forgot the function
Code:
bool check(int s1, int e1, int s2, int e2) {
   return (s2 >= s1 && e2 <= e1);
}

maybe something like that.
HTH

Last edited by hobbyist; 01-27-2012 at 06:53 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 05:20 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board