HDU-4907 Task schedule

Task schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务。
有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行。
机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求。
 

Input
输入的第一行包含一个整数T, 表示一共有T组测试数据。

对于每组测试数据:
第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问;
第二行是n个不同的数字t1, t2, t3....tn,表示机器在ti时间执行第i个任务。
接下来m行,每一行有一个数字q,表示在q时间有一个工作表之外的任务请求。

特别提醒:m个询问之间是无关的。

[Technical Specification]
1. T <= 50
2. 1 <= n, m <= 10^5
3. 1 <= ti <= 2*10^5, 1 <= i <= n
4. 1 <= q <= 2*10^5
 

Output
对于每一个询问,请计算并输出该任务何时才能被执行,每个询问输出一行。
 

Sample Input
  
  
1 5 5 1 2 3 5 6 1 2 3 4 5

Sample Output
   
   
4 4 4 4 7

————————————————————集训6.3的分割线————————————————————

前言:打这场BestCoder的时候悲惨爆零了。

思路:注意一下数据范围的话,意识到不能暴力,需要用二分。二分什么呢??可能的答案肯定是空闲的时间,所以就必须排除那些有工作的时间。在空闲时间数组当中进行二分即可。不知道是不是因为比赛环境下过于紧张因此就想不出来怎么做了,心理素质硬伤。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 1e5+5;
const int MAX = 2e5+5;
int n, m;
int idle[MAX];
bool vis[MAX];

int main()
{
#ifdef J_Sure
	freopen("4907.in", "r", stdin);
	//freopen(".out", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &n, &m);
		memset(vis, 0, sizeof(vis));
		int x, maxX = -1;
		for(int i = 0; i < n; i++) {
			scanf("%d", &x);
			maxX = max(maxX, x);
			vis[x] = true;
		}
		int cnt = 0;
		for(int i = 1; i <= maxX+1; i++) if(!vis[i]) {
			idle[cnt++] = i;
		}
		int key;
		for(int i = 0; i < m; i++) {
			scanf("%d", &key);
			if(key > maxX)
				printf("%d\n", key);
			else {
				int id = lower_bound(idle, idle+cnt, key) - idle;
				printf("%d\n", idle[id]);
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值