joj 1015: Peter Paul and Mary

本文介绍了一个用于安排三人会议的算法。该算法首先计算出一周内(周一至周五)三人共同的空闲时间段,若最大的共同空闲时间不足三小时,则无法安排会议;反之,则能在三周内完成会议安排。具体实现通过计算每天的公共空闲时间,并找出最适合的三天进行会议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

先求出周一到周五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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值