1064 - Network

发布时间:2016-12-8 2:33:31 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"1064 - Network",主要涉及到1064 - Network方面的内容,对于1064 - Network感兴趣的同学可以参考一下。

A packet-switching network handles information in small units, breaking long messages into multiple packets before routing. Although each packet may travel along a different path, and the packets comprising a message may arrive at different times or out of order, the receiving computer reassembles the original message correctly. The receiving computer uses a buffer memory to hold packets that arrive out of order. You must write a program that calculates the minimum buffer size in bytes needed to reassemble the incoming messages when the number of messages (N ), the number of packets (M ), the part of the messages in each packet, the size of each message, and the order of the incoming packets are given. When each packet arrives, it may be placed into the buffer or moved directly to the output area. All packets that are held in the buffer are available to be moved to the output area at any time. A packet is said to ``pass the buffer" when it is moved to the output area. A message is said to ``pass the buffer" when all of its packets have passed the buffer. The packets of any message must be ordered so the data in the sequence of packets that pass the buffer is in order. For example, the packet containing bytes 3 through 5 of a message must pass the buffer before the packet containing bytes 6 through 10 of the same message. Messages can pass the buffer in any order, but all packets from a single message must pass the buffer consecutively and in order (but not necessarily at the same time). Note that unlike actual buffering systems, the process for this problem can look ahead at all incoming packets to make its decisions. The packets consist of data and header. The header contains three numbers: the message number, the starting byte number of data in the packet, and the ending byte number of data in the packet respectively. The first byte number in any message is 1. For example, the figure below shows three messages (with sizes of 10, 20, and 5 bytes) and five packets. The minimum buffer size for this example is 10 bytes. As they arrive, packet #1 and packet #2 are stored in the buffer. They occupy 10 bytes. Then packet #3 passes the buffer directly. Packet #4 passes the buffer directly and then packet #2 exits the buffer. Finally, packet #5 passes the buffer directly and packet #1 exits the buffer. Input  The input file contains several test cases. The first line of each test case contains two integers N (1N5) and M (1M1000) . The second line contains N integers that are the sizes of messages in bytes; the first number denotes the size of message #1, the second number denotes the size of message #2, and so on. Each of the following M lines describes a packet with three integers: the message number and the starting and ending byte numbers of data in the packet. No packet contains more 64 bytes of data. The last test case is followed by a line containing two zeroes. Output  For each test case, print a line containing the test case number (beginning with 1) followed by the minimum buffer size in bytes required to reassemble the original messages. Put a blank line after the output for each test case. Use the format of the sample output. Sample Input  3 3 5 5 5 1 1 5 2 1 5 3 1 5 3 5 10 20 5 2 16 20 1 6 10 3 1 5 1 1 5 2 1 15 0 0 Sample Output  Case 1: 0 Case 2: 10 #include<stdio.h> #include<string.h> #include<stdlib.h> char data[5][64200]; int n,m,cases,i,ans,size[5],selected[5],packet[1009],start[1009],end[1009],queue[5]; void work() { int i,cur,head,len,maxl,used[5]; memset(data,0,sizeof(data)); memset(used,0,sizeof(used)); cur=0; head=0; maxl=0; for(i=0;i<m;i++) { len=end[i]-start[i]+1; used[packet[i]]+=len; memset(&data[packet[i]][start[i]],1,len); while(data[queue[cur]][head+1]&&head<size[queue[cur]]) { head++; used[queue[cur]]--; } if(head>=size[queue[cur]]) { cur++; head=0; } len=used[0]+used[1]+used[2]+used[3]+used[4]; if(len>maxl) maxl=len; } if(maxl<ans) ans=maxl; } void go(int i) { int j; if(i>=n) work(); for(j=0;j<n;j++) if(!selected[j]) { selected[j]=1; queue[i]=j; go(i+1); selected[j]=0; } } int main() { while(scanf("%d%d",&n,&m)&&n) { for(i=0;i<n;i++) scanf("%d",&size[i]); for(i=0;i<m;i++) { scanf("%d%d%d",&packet[i],&start[i],&end[i]); packet[i]--; } ans=1<<30; go(0); printf("Case %d: %d\n\n",++cases,ans); } return 0; }

上一篇:UVA - 10827 Maximum sum on a torus
下一篇:1065 - Raising the Roof

相关文章

关键词: 1064 - Network

相关评论