UVa 11638 - Temperature Monitoring

contents

  1. 1. Problem
  2. 2. Input
  3. 3. Output
  4. 4. Sample Input
  5. 5. Output for Sample Input
  6. 6. Solution

Problem

We have a device to monitor temperature. After configuring it, we place it inside the chamber whose temperature we wish to monitor. The device is equipped with four alarms each of which can be configured differently. The Alarms are identified by the numbers 1, 2, 3 and 4. The device takes a reading of its surrounding at a regular interval.

In this problem you will be simulating the behavior of such a device.

我們有一個監測溫度裝置,在安裝配置後,將其放入所要監控溫度的室內。這個裝置共有四個警報器,分別編號為 1, 2, 3, 4,這些警報器將會每隔固定一段時間進行溫度檢測。
在這個問題中,請模擬這裝置的行為。

Input

The first line of input contains a positive integer T<101, which denotes the number of test cases. Each case consists of several lines which are described below.

輸入第一行為測資筆數 T (T < 101)

The first line contains a positive integer M, which denotes the measurement interval. That is, the device takes a reading every M seconds.

每一組測資第一行為一個正整數 M,表示裝置每隔 M 秒會進行偵測。

The second line contains a non-negative integer S, which denotes the ‘Startup delay’. This is the amount of time that the device will remain idle after placing inside the chamber before it takes its first reading. The device will instantly take a reading when the ‘Startup delay’ ends.

第二行為一個非負整數 S,表示裝置在前 S 秒正在安裝,這段時間機器將不會進行偵測,在安裝完後,將會立即地進行檢測。

The third line contains four integers. The integers give the threshold temperature for alarm 1,2,3,4 respectively. Here threshold temperature means, when a recorded temperature crosses this temperature the corresponding alarm will be triggered.

第三行會有 4 個正整數,分別表示警報器 1, 2, 3, 4 的警報閥值 (根據閥值進行溫度比較)

The fourth line contains a non-negative integer C. The least significant 8 bits of this integer represents the configurations of the four alarms. The rightmost 4 bits (bit 0 to 3) determine if the alarms are enabled(bit value 1) or disabled(bit value 0). For example, if the bits are set as 0011, this means Alarm 1 and 2 are enabled and Alarm 3 and 4 are disabled. If an alarm is disabled, it will never be triggered.

第四行將會有一個非負整數 C,用最低的 8 個 bits 表示警報器的設定,最右邊的 4 個 bits (bit 0 to bit 3) 表示警報器是否有啟動 (1 表示有啟動,反之則否),例如 0011 表示警報器 1, 2 被啟動,3, 4 則沒有被啟動。如果警報器沒有被啟動,則將不會被觸發。

The next 4 bits(bits 4 to 7) determine the triggering type of each alarm. A value of 0 means, it’s a low trigger and a value of 1 means it’s a high trigger. For example, if the bits are set as 1100, this means Alarm 1 and 2 are set for low trigger and Alarm 3 and 4 for high trigger. Here high trigger means if a recorded temperature is above the set threshold temperature for an alarm, it will be triggered. Similarly, a Low trigger means if a recorded temperature is below the set threshold temperature for an alarm, that alarm will be triggered.

接下來的 4 個 bits 將會決定警報器的類型,亦即表示 1 高觸發 (大於閥值觸發) 0 表示低觸發 (低於閥值觸發)。例如 1100,警報器 1, 2 將會是低觸發,3, 4 則會是高觸發。

The fifth line contains a positive integer K<=100. The following K lines contain a pair of integers each in the format time temp. Here time represents the duration of a period with constant temperature and temp indicates the temperature of the environment in that period. Note that, time is always positive. The time value of first pair indicates the period immediately following the placement of the device inside the environment to be monitored. Successive time values indicate the duration of periods in the order they occur. The temperature at the border regions is considered to be that of the period just ending. Also, the temperature at the very beginning is that of the first period. Every value in the input will fit in 32 bit signed integer.

第五行將會有一個正整數 K,表示接下來依序發生的時間情況。將會從時間 0 開始,依序播報 (duration, temp) 表示新的溫度持續多久 (duration)。假設所有數據皆可以在 32 bits 內。

Output

For each case of input, there will be one line of output. It will first contain the case number, followed by the triggering status of each of the four alarms. The triggering status will contain four strings of either yes or no, separated by a space. The first string will be yes if alarm 1 was triggered at least once and no otherwise. The second string will be the status of alarm 2 and so on. Look at the sample output for exact formatting.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
5
5
5 10 15 20
15
1
5 15
1
0
5 10 15 20
7
2
10 15
10 20

Output for Sample Input

1
2
Case 1: no no no yes
Case 2: no no no no

Solution

題目相當令人討厭,因為區間涵蓋沒有講得很清楚。

但是最後可以明白的是

[ti, tj][tj, tk] 其中 ti < tj < tk,我們在討論觸發的時候,端點是需要被考慮的。

詳細操作,詳見代碼。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
int main() {
int testcase, cases = 0;
int M; /* the device takes a reading every M seconds. */
int S; /* The device will instantly take a reading when the ‘Startup delay’ ends. */
int threshold[4]; /* */
int C; /* */
int enable[4], trigger[4]; /* trigger 0 <, 1 > */
int K, time, temp;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &M);
scanf("%d", &S);
for(int i = 0; i < 4; i++)
scanf("%d", &threshold[i]);
scanf("%d", &C);
for(int i = 0; i < 4; i++)
enable[i] = (C>>i)&1;
for(int i = 4; i < 8; i++)
trigger[i - 4] = (C>>i)&1;
int result[4] = {};
int now_time = 0, L, R;
scanf("%d", &K);
while(K--) {
scanf("%d %d", &time, &temp);
L = now_time;
R = now_time + time;
if(R < S) {
} else {
int last_read = R / M * M;
if(L <= last_read && last_read <= R && last_read >= S) {
for(int i = 0; i < 4; i++) {
if(trigger[i] == 0) {
result[i] |= (temp < threshold[i])&enable[i];
} else {
result[i] |= (temp > threshold[i])&enable[i];
}
}
}
}
now_time = R;
}
printf("Case %d:", ++cases);
for(int i = 0; i < 4; i++)
printf(" %s", result[i] ? "yes" : "no");
puts("");
}
return 0;
}