The arrangement of venue in algorithm design

Source: Internet
Author: User
#include <iostream>
using namespace Std;


using namespace Std;
Class Arrange
{


Public


Start time to initialize the venue
void Initstart (int start[],int N)
{


for (int i=0; i<n; i++)
{
cin>>start[i];
}


}
The end time used to initialize the venue
void initfinish (int finish[],int N)
{


for (int i=0; i<n; i++)
{
cin>>finish[i];
}




}
Number used to initialize the venue
void Initnum (int num[],int N)
{
for (int i=0;i<n;i++)
{
cin>>num[i];
}
}


The key to the success of the algorithm is to sort the nth meeting by the end time.
The ranking of this place is the key to the success of this algorithm
void Sort (int n,int start[],int finish[],int num[])
{
int temp;
for (int i=0;i<n;i++)
{
for (int j=i;j<n-1;j++)
{
if (finish[j]>finish[j+1])
{
Swap end Time
TEMP=FINISH[J];
FINISH[J]=FINISH[J+1];
Finish[j+1]=temp;




For the meeting start time and end time correspondence, the Exchange start time


TEMP=START[J];
START[J]=START[J+1];
Start[j+1]=temp;


Each venue actually has a number, for the subsequent venue and the original number corresponding, so here
The number that should be adjusted for their corresponding


temp = Num[j];
NUM[J] = num[j+1];
NUM[J+1] = temp;


}
}
}


}




void Greedyselector (int n,int start[],int finish[],bool a[])
{
For venues that have been ordered by end time, the first array element
Represents the earliest end, which is what we are greedy to
A[0] =true;


int j=0;


for (int i=1;i<n;i++)
{
For a venue where the start time is greater than the end time, then we determine that he is compatible with the previous meeting
if (Start[i]>=finish[j])
{
A[i]=true;
J=i;
}else
{
A[i]=false;
}
}


}




};




int main ()
{
int n;
Used to record that the meeting was selected, if selected, then his record is only true
Back to facilitate the output of the venue number, the need to follow his number to find the corresponding venue
BOOL A[50];
The start time to save the meeting
int START[50];


The end time to save the meeting
int FINISH[50];
The number used to save the venue, because each meeting has a number, so this place can be an integer
Types can also be other data types
int NUM[50];


Arrange A;
cout<< "Please enter the total number of meetings to be arranged n!\n";
cin>>n;
Initialize Venue number
cout<< "Please enter the venue number!\n";
A.initnum (Num,n);
cout<< "Please enter the start time of each session!\n";
Initialize the end time of the venue
A.initstart (Start,n);


cout<< "Please enter" <<n<< "session end time E Note the corresponding relationship between the start time and the end time!\n";


A.initfinish (Finish,n);


A.sort (N,start,finish,num);


A.greedyselector (N,start,finish,a);


cout<< "The number of the selected venue is: \ n";
for (int i=0;i<n;i++)
{
if (A[i])
{
cout<<num[i]<< "";
}
}



return 0;

}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.