CodeForces 411B Multi-core Processor

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 of n 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 a deadlock: 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 within m cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during the m 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 follow n lines describing instructions. The i-th line contains m integers: xi1, xi2, ..., xim (0 ≤ xij ≤ k), where xij is the instruction that must be executed by the i-th core at the j-th cycle. If xij equals 0, then the corresponding instruction is «do nothing». But if xij is a number from 1 to k, then the corresponding instruction is «write information to the memory cell number xij».

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

Output

Print n lines. In the i-th line print integer ti. This number should be equal to 0 if the i-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 5
1 0 0
1 0 2
2 3 1
3 2 0
Output
1
1
3
0
Input
3 2 2
1 2
1 2
2 2
Output
1
1
0
Input
1 1 1
0
Output
0

第一个例子,第一次指令锁住了内核1,2,以及内存1,第三次指令内核3访问被锁的内存1,内核3锁住

#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int num[110][110],lockstep[110],lockedcell[110];

int main(){
    int n,m,k,i,j,q;
    long long sum;
    while(cin>>n>>m>>k){
        memset(num,0,sizeof(num));
        memset(lockstep,0,sizeof(lockstep));
        memset(lockedcell,0,sizeof(lockedcell));
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                cin>>num[i][j];
            }
        }
        for(q=1;q<=m;q++){
            for(i=1;i<=n;i++){
                if(lockstep[i]||num[i][q]==0){
                    continue;
                }
                if(lockedcell[num[i][q]]){
                    lockstep[i]=q;
                    continue;
                }
                for(j=1;j<=n;j++){
                    if(lockstep[j]||num[j][q]==0){
                        continue;
                    }
                    if(i==j){
                        continue;
                    }
                    if(lockedcell[num[j][q]]){
                        lockstep[j]=q;
                        continue;
                    }
                    if(num[i][q]==num[j][q]){
                        lockstep[i]=q;
                        lockstep[j]=q;
                        lockedcell[num[i][q]]=1;
                    }
                }
            }
        }
        for(i=1;i<=n;i++){
            cout<<lockstep[i]<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值