题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2932
3 2 4 1 1 5 2 1 4 3 3 1 2 1 1 2 2 1 2 3 0
Case 1: 18 Case 2: -1
题意:
给出每个学生在一个周期内不睡觉和睡觉的分钟数,如果在某一天,学生本来在该睡觉的时间段但是他看见不睡觉的人数大于总人数的一半,那么他就不会睡觉,反之睡觉;
求所有的学生都不睡觉的时间!
PS:
模拟学生的睡觉和不睡觉过程,暴力!
代码如下:
#include<cstdio>
#include<cstring>
const int maxn = 17;
const int MAXN = 100017;
int main()
{
int n;
int cas = 0;
int a[maxn], b[maxn], c[maxn];
while(scanf("%d",&n)&&n)
{
for(int i = 1; i <= n; i++)
scanf("%d%d%d", &a[i], &b[i], &c[i]);
int ans;
int cont = 0;
for(ans = 1; ans < MAXN; ans++)
{
cont = 0;
for(int i = 1; i <= n; i++)
{
if(c[i] <= a[i])//检查位置是否在开始睡觉的时间之前
cont++;//没有睡觉的人数
}
if(cont == n) //每个位置都在所给的开始睡觉的时间之前
break;
for(int i = 1; i <= n; i++)
{
if(c[i] == a[i]+b[i] || (c[i] == a[i] && cont > n-cont))
{
//新的一个循环开始 || 本来应该睡觉但是此时没有睡觉的人数大于一半
c[i] = 0;//重新计数
}
c[i]++;
}
}
if(ans == MAXN)
ans = -1;
printf("Case %d: %d\n", ++cas, ans);
}
return 0;
}
HDU 2932 Extraordinarily Tired Students(数学 & 模拟)
原文:http://blog.csdn.net/u012860063/article/details/39971559