Codeforces Gym 100340C ePig 模拟

题目大意:

题目很长.....不过实现并不难....当年华科校赛遇到这个题没做...现在补一下

就是现在P2P协议进行文件传输

现在又n个客户端, 一个包被分成k个部分进行发送, 发送到所有客户端需要的时间

发送规则:

首先初始状态只有Client 1拥有完整文件(所有的包)进行提供

每一轮 开始时所有Client机器决定需要的包, 每个Client需要的包是其没有的所有包中供源最少的包, 如果有多个选择序号最小的包

然后每个Client决定要从哪里获得包, 获得包的原则是在所有能提供这个包的Client中选择, 并且选择从拥有包的数量最少的Client那里获得, 如果依旧有多个选择就选择序号最小的Client

然后每个Client都会有得到的请求, 这个时候所有Client决定要给谁包, 每个Client只能满足一个请求, 每个Client会选择曾经给它包的数量最多的Client, 如果有多个, 那么选择拥有包数量最少的Client, 如果依旧有多个, 选择序号最小的

于是每轮都这样重复, 到某一轮之后所有Client有所有包, 传输结束

输出除了Client 1之外, 其他Client得到完整的全部包的轮数


大致思路:

上面的过程看懂了很容易过的....

模拟题还是要耐心...


代码如下:

Result  :  Accepted     Memory  :  460 KB     Time  :  560 ms

/*
 * Author: Gatevin
 * Created Time:  2015/9/1 16:41:44
 * File Name: C.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n, k;
struct Client
{
    int own[210];
    int pro[210];
    int has;
    int giv;
    int complete;
    int need;
    int req;
    int gei;
    Client()
    {
        memset(own, 0, sizeof(own));
        memset(pro, 0, sizeof(pro));
        has = giv = 0;
        complete = -1;
        need = req = -1;
        gei = -1;
    }
};

Client client[110];
int all = 0;
int provide[210];
vector<int> V[110];
int g[110][110];

void start()
{
    memset(g, 0, sizeof(g));
    int time = 1;
    client[1].complete = 1;
    fill(client[1].own, client[1].own + k + 1, 1);
    fill(client[1].pro, client[1].pro + k + 1, 1);
    client[1].has = client[1].giv = k;
    client[1].gei = -1;
    all = 1;
    for(int i = 1; i <= k; i++)
        provide[i] = 1;
    
    while(all < n)
    {
        for(int i = 1; i <= n; i++)
            client[i].need = client[i].req = -1;
        for(int i = 1; i <= n; i++)//decide which chunk need
        {
            if(client[i].has == k)
            {
                client[i].need = -1;
                continue;
            }
            int get = -1;
            for(int j = 1; j <= k; j++)
            {
                if(client[i].own[j] == 1) continue;
                if(get == -1 || provide[j] < provide[get]) get = j;
            }
            client[i].need = get;
        }
        for(int i = 1; i <= n; i++)//decide which client to request
        {
            int need = client[i].need;
            if(need == -1)
            {
                client[i].req = -1;
                continue;
            }
            int req = -1;
            int number = -1;
            for(int j = 1; j <= n; j++)
            {
                if(client[j].own[need])
                {
                    if(req == -1)
                    {
                        req = j;
                        number = client[j].giv;
                        continue;
                    }
                    if(number > client[j].giv)
                    {
                        req = j;
                        number = client[j].giv;
                    }
                }
            }
            client[i].req = req;
        }
        
        for(int i = 1; i <= n; i++) V[i].clear();
        for(int i = 1; i <= n; i++)
            if(client[i].req != -1)
            {
                V[client[i].req].push_back(i);
            }
        
        for(int i = 1; i <= n; i++)
        {
            int now = -1;
            for(int j = 0, sz = V[i].size(); j < sz; j++)
            {
                int nex = V[i][j];
                if(now == -1)
                {
                    now = V[i][j];
                    continue;
                }
                if(g[i][nex] > g[i][now])
                    now = nex;
                else if(g[i][nex] == g[i][now])
                {
                    if(client[nex].has < client[now].has)
                        now = nex;
                }
            }
            client[i].gei = now;
        }
        
        for(int i = 1; i <= n; i++)
        {
            if(client[i].gei != -1)
            {
                int nex = client[i].gei;
                int thing = client[nex].need;
                client[nex].own[thing] = 1;
                client[nex].has++;
                if(client[nex].has == k) client[nex].complete = time, all++;
                client[nex].pro[thing] = 1;
                provide[thing]++;
                client[nex].giv++;
                g[nex][i]++;
            }
        }
        time++;
    }
    for(int i = 2; i <= n; i++)
        printf("%d%c", client[i].complete, i == n ? '\n' : ' ');
}

int main()
{
    freopen("epig.in", "r", stdin);
    freopen("epig.out", "w", stdout);
    scanf("%d %d", &n, &k);
    start();
    return 0;
}


你可能感兴趣的:(codeforces,Gym,ePig,100340C)