【编程题】牛牛找工作

原题出处:牛客网-网易2019实习生招聘编程题集合

为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬。牛牛选工作的标准是在难度不超过自身能力值的情况下,牛牛选择报酬最高的工作。在牛牛选定了自己的工作后,牛牛的小伙伴们来找牛牛帮忙选工作,牛牛依然使用自己的标准来帮助小伙伴们。牛牛的小伙伴太多了,于是他只好把这个任务交给了你。
输入描述:
每个输入包含一个测试用例。
每个测试用例的第一行包含两个正整数,分别表示工作的数量N(N<=100000)和小伙伴的数量M(M<=100000)。
接下来的N行每行包含两个正整数,分别表示该项工作的难度Di(Di<=1000000000)和报酬Pi(Pi<=1000000000)。
接下来的一行包含M个正整数,分别表示M个小伙伴的能力值Ai(Ai<=1000000000)。
保证不存在两项工作的报酬相同。

输出描述:
对于每个小伙伴,在单独的一行输出一个正整数表示他能得到的最高报酬。一个工作可以被多个人选择。

输入例子1:
3 3
1 100
10 1000
1000000000 1001
9 10 1000000000

输出例子1:
100
1000
1001

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
	static int[] SelectJob(int[] a, Dictionary<int,int> dp)
	{
		SortedDictionary<int, int> sortedDp = new SortedDictionary<int, int>(dp);
		int[] res = new int[a.Length];
		for (int i = 0; i < a.Length; i++)
		{
			int t = sortedDp.Values.First<int>();
			foreach (KeyValuePair<int, int> kv in sortedDp)
			{
				if(a[i] < kv.Key)
				{
					res[i] = t;
					break;
				}else if(a[i] == kv.Key)
				{
					res[i] = kv.Value;
					break;
				}
				t = kv.Value;
			}
		}

		return res;
	}

	static void Main(string[] args)
	{
		string tStr;
		while((tStr = Console.ReadLine()) != null)
		{
			int N = int.Parse(tStr.Split(' ')[0]);
			int M = int.Parse(tStr.Split(' ')[1]);
			int[] A = new int[M];
			Dictionary<int, int> dp = new Dictionary<int, int>();

			string str;
			for (int i = 0; i < N; i++)
			{
				str = Console.ReadLine();
				int d = int.Parse(str.Split(' ')[0]);
				int p = int.Parse(str.Split(' ')[1]);
				dp.Add(d, p);
			}
			str = Console.ReadLine();
			for (int i = 0; i < M; i++)
			{
				A[i] = int.Parse(str.Split(' ')[i]);
			}

			int[] result = SelectJob(A, dp);
			for (int i = 0; i < result.Length; i++)
			{
				if(i==result.Length-1)
				{
					Console.Write(result[i]);
					break;
				}
				Console.WriteLine(result[i]);
			}
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值