UVA12108--Extraordinarily Tired Students

本文介绍了一个有趣的问题:如何计算在特定条件下所有学生都保持清醒所需的最短时间。通过模拟每个学生的清醒-睡眠周期并更新其状态,算法最终确定了整个班级达到完全清醒状态的时间。

题意:
课堂上有n个学生(n<=10)。每个学生都有一个“清醒-睡眠”周期。其中第i个学生醒 ai 分钟后睡bi 分钟,然后重复(1<=ai,bi<=5),初始时第i个学生处在他的周期的第 ci 分钟。每个学生在临睡前会察看全班睡觉人数是否严格大于清醒人数,只有这个条件满足时才睡觉,否则就坚持听课 ai分钟后再次检查这个条件。问经过多长时间后全班都清醒。

思路:
遍历每个学生,修改状态,如果都醒着,停止遍历。

代码:

#include<stdio.h>
#define maxn 1000000
typedef struct Stu {
    int a, b, c;
} Stu;

int main() {
    int n, flag = 0;
    while (scanf("%d", &n) && n) {
        Stu stu[n];
        for (int i = 0; i < n; i++)
            scanf("%d%d%d", &stu[i].a, &stu[i].b, &stu[i].c);
        bool ok = false;
        int x = 1;
        for (; x < maxn; x++) {
            int awake = 0;//醒着的人数
            for (int i = 0; i < n; i++)
                if (stu[i].c <= stu[i].a)
                    awake++;
            if (awake == n) {
                ok = true;
                break;
            }
            for (int i = 0; i < n; i++) {
                if (stu[i].c == stu[i].a + stu[i].b
                        || (stu[i].c == stu[i].a && awake >= n - awake))
                    stu[i].c = 0;
                stu[i].c++;
            }
        }
        printf("Case %d: %d\n", ++flag, ok ? x : -1);
    }
    return 0;
}
TCUTTER - Tin Cutter In a Tin Cutting factory there is a machine for cutting parts from tin plates. It has an extraordinarily sharp knife able to make horizontal or vertical segment cuts in the tin plates. Each cutting process consists of a sequence of such cuts. Each segment cut is given by its endpoints that are always located inside the tin plate. During the cutting process some parts of tin plate can fall out and so some holes in the plate can emerge. Factory management needs to predict the number of holes in the plate at the end of the given sequence of cuts. Write a program that answers this question. Single segment cuts are not considered to be holes. Here there are examples of some situations that can arise after cutting: two holes two holes one hole one hole Input The input file consists of blocks of lines. Each block except the last describes one cutting process. In the first line of the block there is a number N <= 100. indicating the number of segment cuts in the cutting process. These cuts are defined by the following N lines. The line defining one segment cut has the form X1 Y1 X2 Y2 where X1 Y1 and X2 Y2 are the co-ordinates of the end points of the segment cut. They are separated by one space. The co-ordinates are integers and always define horizontal or vertical segment (i.e. segment parallel with x or y axis). The last block consists of just one line containing 0. Output The output file contains the lines corresponding to the blocks in the input file. Each such line contains the number of holes that remain in the tin plate after the execution of the corresponding cuts. There is no line in the output file corresponding to the last "null" block of the input file. Example Input: 4 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 2 0 1 2 1 1 2 1 0 0 Output: 1 0
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值