Coder-Strike 2014 - Qualification Round B. Multi-core Processor(模拟题)

来源:互联网 发布:freeVPN Plus for mac 编辑:程序博客网 时间:2024/05/16 05:10

1、http://codeforces.com/contest/411/problem/B

2、题目大意:

有n条指令,m次循环,k块内存,n个CPU,每一次循环n个CPU各执行一条指令,接到指令就立刻执行,相当于n条指令在同一次循环中一起执行,如果有超过一条指令同时申请同一块内存,那么执行这条指令的CPU和将要申请的内存都会被锁住,如果有一条指令申请一块已经被锁住的内存也将会被锁住,输出每条指令在第几次循环中被锁住,如果没被锁住,输出0

3、题目:

B. Multi-core Processor
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The research center Q has developed a new multi-core processor. The processor consists ofn cores and has k cells of cache memory. Consider the work of this processor.

At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to the cell). After receiving the command, the core executes it immediately. Sometimes it happens that at one cycle, multiple cores try to write the information into a single cell. Unfortunately, the developers did not foresee the possibility of resolving conflicts between cores, so in this case there is adeadlock: all these cores and the corresponding memory cell are locked forever. Each of the locked cores ignores all further commands, and no core in the future will be able to record an information into the locked cell. If any of the cores tries to write an information into some locked cell, it is immediately locked.

The development team wants to explore the deadlock situation. Therefore, they need a program that will simulate the processor for a given set of instructions for each core withinm cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during them cycles define for each core the number of the cycle, during which it will become locked. It is believed that initially all cores and all memory cells are not locked.

Input

The first line contains three integers n,m, k(1 ≤ n, m, k ≤ 100). Then follown lines describing instructions. The i-th line contains m integers:xi1, xi2, ..., xim(0 ≤ xij ≤ k), wherexij is the instruction that must be executed by thei-th core at the j-th cycle. Ifxij equals 0, then the corresponding instruction is «do nothing». But ifxij is a number from 1 tok, then the corresponding instruction is «write information to the memory cell numberxij».

We assume that the cores are numbered from 1 to n, the work cycles are numbered from 1 tom and the memory cells are numbered from 1 tok.

Output

Print n lines. In the i-th line print integer ti. This number should be equal to 0 if thei-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.

Sample test(s)
Input
4 3 51 0 01 0 22 3 13 2 0
Output
1130
Input
3 2 21 21 22 2
Output
110
Input
1 1 10
Output
0
4、AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int n,m,k;    int x[105][105];    int a[105];//内存    int flag[105];    int cnt[105];    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%d",&x[i][j]);            }        }        memset(a,0,sizeof(a));        memset(flag,0,sizeof(flag));        for(int j=1;j<=m;j++)        {            memset(cnt,0,sizeof(cnt));            for(int i=1;i<=n;i++)            {                if(flag[i]==0)                {                    cnt[x[i][j]]++;                }            }            for(int i=1;i<=n;i++)            {                if(x[i][j]==0)                continue;                if(flag[i]==0)                {                    if(cnt[x[i][j]]>1)                    {                        a[x[i][j]]=-1;                        flag[i]=j;                    }                    if(a[x[i][j]]==-1)                    {                        flag[i]=j;                    }                }            }        }        for(int i=1;i<=n;i++)        {            printf("%d\n",flag[i]);        }    }    return 0;}/*4 3 51 0 00 0 00 0 00 0 0*/


0 0