1. 程式人生 > >PAT--1006 Sign In and Sign Out (25 分)

PAT--1006 Sign In and Sign Out (25 分)

1006 Sign In and Sign Out (25 分)

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133

譯文:

在每天的開始,第一個登入計算機房的人將開啟門,最後一個登出的人將鎖門。鑑於登入和退出的記錄,你應該找到那天解鎖並鎖上門的人。

輸入規格:
每個輸入檔案包含一個測試用例。每個案例包含一天的記錄。案例以正整數開頭M,即記錄總數,後跟M行,每行格式如下:
ID_number Sign_in_time Sign_out_time
其中時間以格式給出HH:MM:SS,並且ID_number是一個不超過15個字元的字串。

輸出規格:
對於每個測試用例,在一行中輸出當天解鎖並鎖上門的人的ID號。兩個ID號必須用一個空格分隔。

注意: 保證記錄一致。也就是說,登入時間必須早於每個人的退出時間,並且在同一時刻沒有兩個人登入或退出。

樣本輸入:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
樣本輸出:
SC3021234 CS301133

筆記: 其實這題真的很水,將sign in時間排序一下,再將sign out時間排序一下就OK了,完全沒有什麼難度。使用STL容器map後,他會自動key排序,這樣就減少了我們很多時間。
滿分程式碼:

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
	int n;
	scanf("%d", &n);
	map<string, string>signIn;
	map<string, string>signOut;
	//資料讀入
	for (int i = 0; i < n; i++)
	{
		string name, in, out;
		cin >> name >> in >> out;
		signIn[in] = name;
		signOut[out] = name;
	}
	//資料輸出
	map<string, string>::iterator it;
	it = signIn.begin();
	cout << it->second<<" ";
	it = signOut.end();
	it--;
	cout << it->second << endl;
	return 0;
}