原题链接:https://vjudge.net/problem/UVA-12108
分类:函数
备注:简单模拟
注意:刚要入睡的时候观察的是上一分钟的情况。
代码如下
#include<stdio.h>
#include<string.h>
const int maxn = 10 + 5;
int n, a[maxn], b[maxn], pos[maxn], sleep[maxn], kase;
void simulate()
{
int ans = -1, flag = 1, judge = 0;//judge判断上一分钟是否睡觉的严格多余醒着的
for (int i = 0; i < n; i++)if (sleep[i])flag = 0;
if (flag) { printf("Case %d: 1\n", ++kase); return; }
for (int i = 0, cnt = 0; i < n; i++)
{
if (sleep[i])cnt++;
if (cnt > n - cnt)judge = 1;
}
for (int i = 2; i <= 1000; i++)
{
int cnt = 0, now[maxn] = { 0 }, flag = 1;
for (int j = 0; j < n; j++)
{
pos[j] = pos[j] % (a[j] + b[j]) + 1;//更新时间
if (pos[j] == a[j] + 1)//正好要入睡的时刻
{
if (judge)now[j] = 1;
else pos[j] = 1;
}
else if (pos[j] > a[j] + 1)//继续睡
now[j] = 1;
}
judge = 0;
for (int i = 0; i < n; i++)sleep[i] = now[i];
for (int i = 0; i < n; i++)
{
if (sleep[i])cnt++, flag = 0;
if (cnt > n - cnt) { judge = 1; break; }
}
if (flag) { ans = i; break; }
}
printf("Case %d: %d\n", ++kase, ans);
}
int main(void)
{
while (~scanf("%d", &n) && n)
{
memset(sleep, 0, sizeof(0));
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &a[i], &b[i], &pos[i]);
if (pos[i] > a[i])sleep[i] = 1;
}
simulate();
}
return 0;
}
Barsaker
发布了40 篇原创文章 · 获赞 42 · 访问量 1333
私信
关注