Problem Description
Peer-to-peer(P2P) computing technology has been widely used on the Internet to exchange data. A lot of P2P file sharing systems exist and gain much popularity in nowadays.
Let's consider a simplified model of P2P file sharing system: There are many computers in the system, and they can receive data from or send data to others. To simplify the problem, let's assume that there is just ONE large file which is of our concern in the system. Some computers already have the whole file (we call them "servers" and some don't(we call them "clients". Every client needs to download the file from servers. When a client gets the WHOLE file, it becomes a server.
These computers are not always online. An online client will down load the file from all online servers. Different servers send data of different parts of the file to a client, so the client can download the file faster.
Now given the transfer speed between each pair of computers, what time is every computer online or offline, and which computers are servers at the beginning, please analyze the running of the system in a period of time.
Input
The first line contains an integer indicating the number of test cases.
For each test case:
Line 1: It contains two positive integers: n and T(n<=20, T<=1000) meaning that there are n computers in the system numbered from 1 to n, and you task is to figure out that how many percentage of the file does every computer gets after T seconds from the beginning.
Line 2: It contains two positive integers: k and S (k<=n, S<=220) meaning that at the beginning there are k servers, and the size of the file of our concern is S (KB).
Line 3: It contains k integers. It's a list of all servers'No.
Line 4 ~ n+3: These n lines form a matrix of size n*n. The j-th integer in the i-th row of the matrix represents the transfer speed (in KB/s, no more than 210) between computer i and computer j (i and j start from 1). The matrix is symmetrical and the integers on the diagonal are meaningless.
Line n+4 ~ 2n+3: Each line contains an online/offline pattern for a computer in the following format( These lines are in the ascending order of computer No.) :
t online_time
1 offline_time
1 online_time
1 offline_time
2 ... online_time
t offline_time
t
t is an integer no more than 10 and the time given are all non-negative integers and in ascending order. During the time between online_timei and offline_timei, the computer is online and can download data from other computers or send data to other computers.
Line 2n+4: It contains one positive integer m, representing the number of download actions in the system.
Line 2n+5 ~ 2n+m+4: Each line contains two integers representing a download action in the following format:
download_time
i computer_id
i
At time download_time
i, the computer computer_idi starts to download the file. 0<= download_time
i <=T, 1<= computer_id
i <=n. These lines are given in non-descending order of time. It's guaranteed that servers never try to download the file. It's ensured that at time download_time
i the computer computer_idi is online (Though it's possible that it instantly go offline after issuing a download command).
When a client starts to download, it will try to connect to all servers and download data simultaneously from online servers. The client's download speed is the sum of all connections. We assume the construction of a connection to be instant and cost no time. Only data transfer is time consuming.
When a client goes offline, unfinished download task will be saved and continued when it's online next time. If a server goes online, all computers that are currently downloading will connect to it and download data from it. What's more, when a client becomes a server, it begins to send data to clients immediately. NOTE: To simplify the problem, time used to download a file should be rounded up to integer(If the file size is 6KB and download speed is 5KB/s, the download task will cost 2 seconds instead of 1.2 seconds ----- 5KB for the first second and 1KB for the next second).
Please note that all times given above are in seconds.
Output
For each test case, the output should contain n lines, each for a computer.
The i-th line contains a percentage indicating the amount of data the i-th computer gets after T seconds from the beginning, in the format: "percentage%". The percentage should be rounded down to integer.
题目大意:模拟一个文件的P2P发送和接受。
思路:模拟题,不是很难,就是题目略长。
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6
7 const int MAXT = 1010;
8 const int MAXN = 22;
9
10 int mat[MAXN][MAXN], state[MAXN][MAXT];
11 int download[MAXN], beg[MAXN];
12 int n, m, k, T, size;
13
14 void init() {
15 memset(download, 0, sizeof(download));
16 memset(state, 0, sizeof(state));
17 scanf("%d%d", &n, &T);
18 scanf("%d%d", &k, &size);
19 while(k--) {
20 int x;
21 scanf("%d", &x);
22 download[x] = size;
23 }
24 for(int i = 1; i <= n; ++i)
25 for(int j = 1; j <= n; ++j) scanf("%d", &mat[i][j]);
26 for(int i = 1; i <= n; ++i) {
27 int t, st, ed;
28 scanf("%d", &t);
29 while(t--) {
30 scanf("%d%d", &st, &ed);
31 for(int j = st; j < ed; ++j) state[i][j] = 1;
32 }
33 }
34 scanf("%d", &m);
35 memset(beg, 0x3f, sizeof(beg));
36 while(m--) {
37 int t, id;
38 scanf("%d%d", &t, &id);
39 beg[id] = t;
40 }
41 }
42
43 void solve() {
44 for(int t = 0; t < T; ++t) {
45 for(int i = 1; i <= n; ++i) {
46 if(download[i] == size || beg[i] > t) continue;
47 if(!state[i][t]) continue;
48 int sum = 0;
49 for(int j = 1; j <= n; ++j) {
50 if(download[j] != size) continue;
51 if(!state[j][t]) continue;
52 sum += mat[i][j];
53 }
54 download[i] += sum;
55 if(download[i] >= size) download[i] = size + 1;
56 }
57 for(int i = 1; i <= n; ++i)
58 if(download[i] == size + 1) --download[i];
59 }
60 }
61
62 void output() {
63 for(int i = 1; i <= n; ++i) {
64 printf("%d%%\n", download[i] * 100 / size);
65 }
66 }
67
68 int main() {
69 int kase;
70 scanf("%d", &kase);
71 while(kase--) {
72 init();
73 solve();
74 output();
75 }
76 }
View Code