B - Memory Management System

It is your first day in your new job and guesses what, your first task is already waiting for you! Your task is to build a memory management system.

In this system, the memory will consist of m

bytes ordered from 1 to m. Originally, there are n files in the system, such that the ith file is occupying all bytes from li to ri

(inclusive). It is guaranteed that no two files share the same memory position (byte).

The main goal of the system is to answer multiple queries such that each query consists of a file of size k

bytes, and the system must determine if there exist at least k

consecutive bytes that can be reserved and assigned to that file.

Formally, at the jth

query, you are given an integer kj and your task is to find a pair of integers lj and rj

, such that:

  • All bytes between lj

and rj

  • (inclusive) are free (not occupied).
  • rj−lj+1≥kj
  • .
  • If there are multiple solutions, find the one with the maximum rj
  • . If there are still multiple solutions, find the one with maximum lj
  • .
  • If the jth
  • file cannot be saved in the system, both lj and rj must be −1
    • .

    Please note that you are not reserving the bytes for the files in the queries, you are just checking if there exists an empty space for the file in the system. So, an empty byte in the system can be assigned to multiple queries files. However, if a byte is originally occupied in the system, you cannot use it for the queries files.

    Input

    The first line contains an integer T

    (1≤T≤10

    ) specifying the number of test cases.

    The first line of each test case contains three integers n

    , m, and q (0≤n≤m, 1≤m≤105, 1≤q≤min(105,m)), in which n is the number of files in the system originally, m is the number of bytes the system contains, and q

    is the number of queries.

    Then n

    lines follow, each line contains two integers li and ri (1≤li≤ri≤m

    ), giving the files in system. It is guaranteed that no two files share the same memory position (byte).

    Then q

    lines follow, each line contains an integer kj (1≤kj≤m

    ), giving the queries.

    Output

    For each query j

    , print two space-separated integers lj and rj, as described in the problem statement. If there are multiple solutions, find the one with the maximum rj. If there are still multiple solutions, find the one with maximum lj. If the file cannot be saved in the system, both lj and rj must be −1

    .

    Example

    Input

    2
    3 9 2
    1 1
    5 5
    8 9
    3
    2
    2 5 3
    5 5
    1 2
    1
    2
    4
    

    Output

    2 4
    6 7
    4 4
    3 4
    -1 -1
    

    Note

    In the first test case, the memory system can be represented as "OFFFOFFOO", in which 'O' represent an occupied position and 'F' represent a free position.

    • The first file needs to occupy 3
    bytes. So, the only available answer is 24
    • .
    • The second file needs to occupy 2 bytes. There are 3
    • available intervals, which are: 23, 34, and 67. The interval that satisfies the problem conditions is 67。

题解:从后往前找连续空闲子串即可。

#include<bits/stdc++.h>
using namespace std;
map<int,pair<int,int> >mp;//pair分别对应L,R
int a[100010];
int main()
{
    int t,n,m,q,l,r;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        memset(a,0,sizeof(a));
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&l,&r);
            for(int j=l; j<=r; j++)
                a[j]=1;
        }
        int ans=0;
        for(int i=m;i>=1;i--)//从后往前寻找简单些,因为我从前往后找TLE了~~
        {
            int s,sum=0;
            if(a[i]==0)//空闲
            {
                s=i;//记录最后一个空闲的位置
                while(a[i]==0&&i>=1)//往前找空闲位置
                {
                    i--;
                    sum++;
                }
            }
            if(sum>ans)//足够大了才有更新的余地
            {
                for(int j=ans+1;j<=sum;j++)//只需更新原来没有记录的即可,因为只要更新过的肯定是最优的
                {
                    mp[j].first=s-j+1;//l位置
                    mp[j].second=s;//r位置
                }
                ans=sum;//更新最优值
            }
        }
        while(q--)
        {
            int x;
            scanf("%d",&x);
            if(x>ans)//没有最大连续长度为x的
                printf("-1 -1\n");//还卡printf也是醉了
            else
                printf("%d %d\n",mp[x].first,mp[x].second);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/89072459