Task schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
AC:用模拟+优先队列 做的又费事又费力还耗内存- =伤不起 (水题)。
//author: svtter
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#define INF 0xffffff
#define lln long long
using namespace std;
struct item
{
int n;
int i;
bool operator < (const item &a)const
{
return n > a.n;
}
};
int m, n;
int work[100001];
int extra[100001];
priority_queue <item> q;
int main()
{
int i, cur;
int t;
int t1;
int a;
item it, b;
freopen("test2", "r", stdin);
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++)
scanf("%d", &work[i]);
for(i = 0; i < m; i++)
{
scanf("%d", &it.n);
it.i = i;
q.push(it);
}
sort(work, work+n);
t1 = 0;
cur = 0;
while(!q.empty())
{
if(t1 != n)
a = work[t1];
else
{
while(!q.empty())
{
b = q.top();
if(b.n <= cur)
extra[b.i] = cur;
else
{
extra[b.i] = b.n;
}
q.pop();
}
break;
}
if(!q.empty())
b = q.top();
else
break;
if(a > b.n && a != cur)
{
if(b.i <= cur)
extra[b.i] = cur;
else
extra[b.i] = b.n;
q.pop();
}
else
{
cur = work[t1]+1;
t1++;
}
}
for(i = 0; i < m; i++)
printf("%d\n", extra[i]);
printf("\n");
}
return 0;
}