这份代码在UVa上通过不了
Description
一个班,有学生,这些学生,要么睡觉,要么醒着,他们遵循一个周期。
醒 a 分钟,睡 b 分钟。
但在将要睡的那一分钟,即第a分钟,他会审视一下全班同学。
如果,醒着的人比睡着的人多,那么,他就不睡了,从头还是那个周期,否则,就睡,睡了再醒。
然后,他们的初始状态是c,即第1分钟,将是他们周期的第C分钟。
问题是,那一分钟,他们都醒着,不睡,这个就是ans
Algorithm
先对每一个人开一个bool 数组来表示他们的周期,即代码里面的b数组
然后再用个a数组来记录每个人的指针,
接下来就是模拟的过程了
当然了,如果找不到怎么办?
我也不知道怎么办。。。依照队友的建议给ans设置一个最大值,如果这么久都找不到,那就没有。
当然了,这肯定是不严谨的,如果那天我能找到更好的算法,那就再改吧
Hint
输出的case 1, case 2 看清楚
Code
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 10;
const int maxl = 20;
const int maxans = 1e6;
int n;
int a[maxn]; // 指针
bool b[maxn][maxl] = {false};
int c[maxn]; // 记录周期
int e[maxn]; // 复制a
void solve()
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(e, 0, sizeof(e));
for (int i = 0; i < n; i++)
{
int aa, bb, cc;
cin >> aa >> bb >> cc;
for (int j = 0; j < aa; j++)
b[i][j] = true;
a[i] = cc - 1;
c[i] = aa + bb;
}
int ans;
for (ans = 1; ans < maxans; ans++)
{
// 检验是否全部都醒着
bool flag = true;
for (int i = 0; i < n; i++)
if (!b[i][a[i]])
{
flag = false;
break;
}
if (flag) break;
for (int i = 0; i < n; i++)
e[i] = a[i];
for (int i = 0; i < n; i++)
{
if (b[i][a[i]] && !b[i][a[i]+1])
{
int t = 0;
for (int j = 0; j < n; j++)
if (b[j][a[j]]) t++;
if (t > n / 2) e[i] = 0; else e[i] = (e[i] + 1) % c[i];
}else
e[i] = (e[i] + 1) % c[i];
}
for (int i = 0; i < n; i++)
a[i] = e[i];
}
if (ans == maxans) cout << "-1" << endl; else
cout << ans << endl;
}
int main()
{
for (int i = 1; ;i++)
{
cin >> n;
if (n == 0) break;
cout << "Case " << i << ": ";
solve();
}
}
本文介绍了一个UVa在线编程题目解决方案,该题目涉及学生周期性醒来或睡觉的模拟问题。通过使用布尔数组记录每个学生的状态周期,并利用辅助数组进行状态更新,最终找出所有学生在同一分钟都处于清醒状态的时间点。
2016

被折叠的 条评论
为什么被折叠?



