首页 > 试题广场 >

小团的选调计划

[编程题]小团的选调计划
  • 热度指数:1047 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

        美团打算选调n名业务骨干到n个不同的业务区域,本着能者优先的原则,公司将这n个人按照业务能力从高到底编号为1~n。编号靠前的人具有优先选择的权力,每一个人都会填写一个意向,这个意向是一个1~n的排列,表示一个人希望的去的业务区域顺序,如果有两个人同时希望去某一个业务区域则优先满足编号小的人,每个人最终只能去一个业务区域。

        例如3个人的意向顺序都是1 2 3,则第一个人去1号区域,第二个人由于1号区域被选择了,所以只能选择2号区域,同理第三个人只能选择3号区域。

        最终请你输出每个人最终去的区域。


输入描述:

输入第一行是一个正整数n,表示业务骨干和业务区域数量。(n≤300)

接下来有n行,每行n个整数,即一个1~n的排列,第i行表示i-1号业务骨干的意向顺序。



输出描述:
输出包含n个正整数,第i个正整数表示第i号业务骨干最终去的业务区域编号。
示例1

输入

5
1 5 3 4 2 
2 3 5 4 1 
5 4 1 2 3 
1 2 5 4 3 
1 4 5 2 3

输出

1 2 5 4 3
python,使用visited数组
n=int(input())
vis=[0]+[False]*n
cnt=0
ans=[]
while n:
    cnt+=1
    will=[0]+list(map(int,input().split()))
    for i in range(1,cnt+1):
        if not vis[will[i]]:
            ans.append(will[i])
            vis[will[i]]=True
            break
    n-=1
print(" ".join(str(i) for i in ans))


发表于 2021-08-18 14:05:20 回复(0)
构建一个boolean数组,其中的元素表示区域i是否被选择过,然后依次遍历每位骨干的意向,遇到没有选择的岗位就进行选择。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());
        boolean[] used = new boolean[n + 1];
        for(int i = 0; i < n; i++){
            String[] choice = br.readLine().trim().split(" ");
            for(int j = 0; j < choice.length; j++){
                // 依次遍历第i位骨干的选择,遇到没有选过的就选
                if(!used[Integer.parseInt(choice[j])]){
                    used[Integer.parseInt(choice[j])] = true;  // 将这个区域标记为选择过
                    System.out.print(choice[j] + " ");
                    break;
                }
            }
        }
    }
}


发表于 2021-03-01 09:44:49 回复(0)
用二维数组存储输入的数据,另用一个数组r存储结果,循环二维数组的每一行,如果每一行中的数字出现在r中,就下一个,否则加入r中,循环结束后,r中的数据就是顺序。
let n=parseInt(readline())
    let a=[]
    for(let i=0;i<n;i++){
        a[i]=readline().split(' ').map(Number)
    }
    let r=[]
    for(let i=0;i<n;i++){
        for(let j=0;j<n;j++){
           if(r.indexOf(a[i][j])==-1){
               r.push(a[i][j])
               break
           }
        }
    }
    console.log(r.join(' '))


发表于 2022-09-07 09:22:43 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int count = sc.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        
        for (int i = 0; i < count; i++) {
            int[] arr = new int[count];
            for (int j = 0; j < count; j++) {
                 arr[j] = sc.nextInt();
            }
            
            for (int j = 0; j < count; j++) {
                if (map.get(arr[j]) == null) {
                    map.put(arr[j],j);
                    System.out.print(arr[j] + " ");
                    break;
                }
            }
        }
    }
}

发表于 2022-07-27 17:20:14 回复(0)
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
    int n;
    cin >> n;
    vector<vector<int>> a(n,vector<int>(n));
    vector<int> res(n);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> a[i][j];
    
    unordered_map<int, int> umap;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++) {
            int num = a[i][j];
            if (!umap.count(num)) {
                umap[num] = i;
                res[i] = num;
                break;
            }
        }
    }
    for (int i = 0; i < n; i++){
        cout << res[i] << " ";
    }
    cout << endl;

    return 0;
}


发表于 2021-04-28 23:12:01 回复(0)
n = int(input())
distribution = []
for i in range(n):
    rank = list(map(int,input().split()))
    for j in rank:
        if j not in distribution:
            distribution.append(j)
            break
print(str(distribution).replace('[','').replace(']','').replace(',',''))

发表于 2021-03-26 09:51:56 回复(0)
importjava.util.*;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {

        Main test = newMain();
        Scanner in = newScanner(System.in);
        intcount = in.nextInt();
        ArrayList<List<Integer>> lists = newArrayList<>();
        for(inti = 0; i < count; i++) {
            ArrayList<Integer> list = newArrayList<>();
            for(intj = 0; j < count; j++) {
                list.add(in.nextInt());
            }
            lists.add(list);
        }
      List<Integer> res= test.getArea(lists);//区域
        for(Integer re : res) {
            System.out.print(re+" ");
        }
    }
 
    privateList<Integer> getArea(ArrayList<List<Integer>> lists) {
        ArrayList<Integer> res = newArrayList<>();
        HashSet<Integer> set = newHashSet<>();
        for(List<Integer> list : lists) {
            for(Integer integer : list) {
                if(!set.contains(integer)){
                    set.add(integer);
                    res.add(integer);
                    break;
                }
            }
        }
        returnres;
    }
}
发表于 2021-02-24 21:11:44 回复(0)