HDU4907-Task schedule

Task schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 586    Accepted Submission(s): 292


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
 

Source
 

Recommend
We have carefully selected several similar problems for you:  4910 4909 4908 4906 4904 
//AC代碼
/*
此題暴力基本沒戲
所以先枚舉出空閒時間,再二分
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=200001;
using namespace std;
int s[Max];
int t[Max];
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
int main()
{
    int n,m,i,j,q,p,res,k;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=Max;i++)
        {
            s[i]=0;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d",&q);
            s[q]=1;
        }
        k=0;
        for(i=1;i<=Max;i++)
        {
            if(s[i]==0)
                t[++k]=i;
        }
        sort(t+1,t+(k+1));
        //cout<<t[1]<<" "<<t[2]<<endl;
        while(m--)
        {
            scanf("%d",&p);
            if(s[p]==0)
                printf("%d\n",p);
            else
            {
                int left=1;
                int right=k;
                while(left<right)
                {
                    int mid=(left+right)>>1;
                    //cout<<left<<" "<<right<<" "<<mid<<endl;
                    if(p<t[mid])
                    {
                        res=t[mid];
                        right=mid-1;
                        if(p<t[right])
                            res=t[right];

                    }
                    else
                    {
                        left=mid+1;
                        if(p<t[left])
                            res=t[left];
                    }
                }
                printf("%d\n",res);
            }
        }
    }
    return 0;
}

發佈了243 篇原創文章 · 獲贊 12 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章