joj 1015: Peter Paul and Mary

http://acm.jlu.edu.cn/joj/showproblem.php?pid=1015

先求出周一到周五Peter,Paul和Mary公共的free time,如果他们的公共free time最大值小于三

则无法安排会议,否则三周之内必可以安排好会议。


#include
using namespace std;

struct FreeTime{
  int begin;
  int end;
};

string days[] = {
 "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
};

int calCommonFreeTime(FreeTime dayFreeTime[],int n){
 int max = dayFreeTime[0].begin;
 int min = dayFreeTime[0].end;
 int i = 1;
 for(; i < n; i++){
  if(max < dayFreeTime[i].begin)
   max = dayFreeTime[i].begin;
  if(min > dayFreeTime[i].end)
   min = dayFreeTime[i].end;
 }
 int commonFreeTime = min - max;
 return commonFreeTime > 0 ? commonFreeTime : 0;
}

void schedule(int commonFreeTimes[], int n,int icase){
 int maxFreeTime = commonFreeTimes[0];
 int i = 1;
 for(; i < n; i++){
  if( maxFreeTime < commonFreeTimes[i] ){
   maxFreeTime = commonFreeTimes[i];
  }
 }
 if(maxFreeTime < 3)//can't schedule
  cout << "Case " << icase << ": The meetings cannot be scheduled." << endl;
 else{//can schedule
  int j;
  int time = 1;
  string meetingDays[3];
  for(i = 0; i < 3; i++)//at most schedule 3 weeks
   for(j = 0; j < n; j++){
    if(commonFreeTimes[j] >= time){
     meetingDays[time-1] = days[j];
     time++;
    }
   }
    cout << "Case " << icase << ": " << meetingDays[0] 
      << " for 1 hour, then " << meetingDays[1]
   << " for 2 hours, then " << meetingDays[2]
   << " for 3 hours." << endl;
 }
}

int main(){
 int ncase;
 FreeTime freeTimes[3][5];
 int commonFreeTimes[5];
 FreeTime dayFreeTime[3];
 cin >> ncase;
 int i,j,k;

 for(i = 1; i <= ncase; i++){
  for(j = 0; j < 3; j++)
   for(k = 0; k < 5; k++){
    cin >> freeTimes[j][k].begin >> freeTimes[j][k].end;
    if(freeTimes[j][k].end < freeTimes[j][k].begin)
     freeTimes[j][k].end += 12;//change the end hour according to 24 hours a day
   }

  for(j = 0; j < 5; j++){
   for(k = 0; k < 3; k++){
    dayFreeTime[k] = freeTimes[k][j];
   }
   commonFreeTimes[j] = calCommonFreeTime(dayFreeTime,3);
  }

  schedule(commonFreeTimes,5,i);
 }
 return 0;
}


你可能感兴趣的:(PHP,J#)