#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
struct Student
{
Student() :sober(0), sleep(0), isSleep(0) {}
Student(int a,int b,int c) :sober(a), sleep(b), isSleep(c) {}
int sober;
int sleep;
int isSleep;
};
Student stu[20];
int state[20],cnt = 0;
const int num = 13;
unordered_set<int> mset;
//当前清醒人的数量
int GetSoberCnt() {
int ret = 0;
for (int i = 0; i < cnt; ++i) {
if (stu[i].isSleep) {
//下一次要醒了
if (state[i] % (stu[i].sleep + stu[i].sober) <= stu[i].sober) {
stu[i].isSleep = 0;
state[i] = 0;
}
}
else ++ret;
}
return ret;
}
int CanSleep() {
int t = GetSoberCnt();
//睡觉的人 大于 清醒的人
if (cnt - t > t) {
//可以睡觉了
for (int i = 0; i < cnt; ++i) {
//还没睡的 想睡就睡
if (!stu[i].isSleep && state[i] >= stu[i].sober) { stu[i].isSleep = 1; --t; }
}
}
else {
//不能睡觉
for (int i = 0; i < cnt; ++i) {
//重新坚持
if (!stu[i].isSleep && state[i] >= stu[i].sober) state[i] = 0;
}
}
return cnt - t;
}
bool Check() {
//当前学生状态信息 哈希映射为一个整数 检测是否进入了循环 num 为一个质数
unsigned int hash = 0;
for (int i = 0; i < cnt; ++i) {
hash = hash * num + state[i];
}
if (mset.count(hash)) return false;
mset.insert(hash);
return true;
}
int main()
{
int id = 0;
while (cin >> cnt && cnt) {
mset.clear();
int ntime = 1,scnt = 0;
for (int i = 0; i < cnt; ++i) {
cin >> stu[i].sober >> stu[i].sleep >> state[i];
stu[i].isSleep = state[i] > stu[i].sober;
}
while (true)
{
//当前睡觉的人
scnt = CanSleep();
if(scnt == 0 || Check() == false) break;
for (int i = 0; i < cnt; ++i) {
state[i]++;
}
++ntime;
}
if (scnt == 0) cout << "Case " << ++id << ": " << ntime << endl;
else cout << "Case " << ++id << ": -1"<< endl;
}
return 0;
}
习题4-8(uva-12108)
最新推荐文章于 2021-04-05 14:15:13 发布