1017-1018

来源:互联网 发布:马龙职业生涯数据 编辑:程序博客网 时间:2024/05/02 04:21

1017. Queueing at Bank (25)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) - the total number of customers, and K (<=100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:
7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10
Sample Output:
8.2

代码:

#@author cxluaimport datetimeline1 = input().split()n = int(line1[0])k = int(line1[1])record = []for i in range(n):    record.append(input().split())    # print(record)    record[i][0] = datetime.datetime.strptime(record[i][0], '%H:%M:%S')    record[i][1] = datetime.timedelta(minutes = int(record[i][1]))# print(record)record = sorted(record, key = lambda x: x[0])opentime = datetime.datetime.strptime('08:00', '%H:%M')closetime = datetime.datetime.strptime('17:00', '%H:%M')waittime = datetime.timedelta(0, 0) # in secondwindow = []processing = 0for i in range(k):      if processing >= n or record[processing][0] > closetime:        break    if record[i][0] < opentime:        waittime += opentime - record[i][0]        window.append(opentime + record[i][1])      else:        window.append(record[i][0] + record[i][1])    processing += 1    # print(i, window, waittime)# print(window)while processing < n and record[processing][0] <= closetime:    fastwin = window.index(min(window))    if record[processing][0] < window[fastwin]:        waittime += window[fastwin] - record[processing][0]        window[fastwin] += record[processing][1]    else:        window[fastwin] = record[processing][0] + record[processing][1]    processing += 1    # print(window, waittime)print('%.01f'%(waittime.total_seconds()/60./processing + 0.001))
  1. datetime.datetime对象和datetime.timedelta对象相加减;
  2. 读入数据时,直接将字符串转换为datetime.datetime对象,sorted()函数直接对datetime.datetim对象排序;
  3. 用while做循环时,注意找到循环变量以及更新;
  4. 排队题目注意时间界线和时间空闲,以及窗口的时间属性(结束时间)的更新,例如窗口结束时间,对于上班时间开始的,直接加用户的服务时间,而对于中间时间开始的,若顾客有断层(该窗口有空闲),则应从顾客到达时间算起(测试点4, 5);对于顾客的服务与否和等待时间,只要顾客在17: 00前到达,那么等待时间即参与计算,按照题意,如果在该顾客前面的顾客17:00之后才结束服务,那个该顾客仍然被服务,所以等待时间不需要以下班时间划分界线(测试点6);

1018. Public Bike Management (30)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

figure 1

Figure 1 illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:

  1. PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.

  2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci (i=1,…N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0->S1->…->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge’s data guarantee that such a path is unique.

Sample Input:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
Sample Output:
3 0->2->3 0

代码:

#@author cxlua# 10 4 4 5# 6 7 5 0# 0 1 1# 0 2 1# 1 3 1# 2 3 1# 3 4 1line1 = [int(i) for i in input().split()]capacity = line1[0]stationNum = line1[1]proStaIndex = line1[2]roadNum = line1[3]curBikeNum = [0]for i in input().split():    curBikeNum.append(int(i)) perfectNum = int(capacity / 2)graph = [([float('inf')] * (stationNum + 1)) for i in range(stationNum + 1)]for i in range(roadNum):    road = [int(i) for i in input().split()]    v = road[0]    w = road[1]    graph[v][w] = graph[w][v] = road[2]marked = [False] * (stationNum + 1)mindis = float('inf')minsend = float('inf')minTakeBack = float('inf')path = [0]def calredundant(num):    global perfectNum    if num > perfectNum:        return int(num - perfectNum)    else:        return 0def calshort(num):    global perfectNum    if num < perfectNum:        return int(perfectNum - num)    else:        return 0def caltake(num):    if num >= 0:        return num    else:        return 0def dfs(dis, node, send, take, temppath):    global marked, graph, mindis, perfectNum, curBikeNum, minsend, minTakeBack, path    if node == proStaIndex:        # print(temppath)        if dis < mindis:            mindis = dis            minsend = send            minTakeBack = take            path = temppath[:]        elif dis == mindis:            if send < minsend:                minsend = send                minTakeBack = take                path = temppath[:]            elif send == minsend:                if take < minTakeBack:                    minTakeBack = take                    path = temppath[:]        # print(path)        return     if dis >= mindis:        return    marked[node] = True    for i in range(stationNum + 1):        if graph[node][i] < float('inf') and (not marked[i]):            temppath.append(i)            dfs(dis + graph[node][i], i, send + calshort(curBikeNum[i] + take), \                caltake(take + calredundant(curBikeNum[i]) - calshort(curBikeNum[i])), temppath)            temppath.pop()            marked[i] = False    returndfs(0, 0, 0, 0, path)print(minsend, '->'.join(str(node) for node in path), minTakeBack)
  1. dfs的路径记录:
    • 在dfs函数中包含一个temppath参数,是一个List, 每次调用dfs之前,在list中append调用dfs的点(路径上的点)(注意不能在递归调用时append,否则调用时temppath会变成nonetype),return以后pop该点(在List末尾删除);
    • 在全局维持一个path变量,初始化为只包含起点的list, 当到达目标点时,path**深拷贝**temppath(否则函数返回后,temppath被释放,path又指回初始化的值);
      (本来是没有pop, 而是维持一个step全局变量,每次到达终点时,取temppath的后step个点,但这样是错误的,因为后step个点并不是整个路径(见代码开头测试用例))
  2. str.join(list)时,list元素要求类型是str;
  3. send和takeback的计算问题:不能到最后的时候直接累加,因为后面的站点多出来的车不能补给前面的站点(测试点3,7)。
原创粉丝点击