会议中心

 

1230: 会议中心

时间限制: 1 Sec  内存限制: 128 MB

题目描述

算法训练  会议中心    
时间限制:2.0s     内存限制:512.0MB 
  会议中心  Siruseri政府建造了一座新的会议中心。许多公司对租借会议中心的会堂很感兴趣,他们希望能够在里面举行会议。 
  对于一个客户而言,仅当在开会时能够独自占用整个会堂,他才会租借会堂。会议中心的销售主管认为:最好的策略应该是将会堂租借给尽可能多的客户。显然,有可能存在不止一种满足要求的策略。 
  例如下面的例子。总共有4个公司。他们对租借会堂发出了请求,并提出了他们所需占用会堂的起止日期(如下表所示)。 
开始日期 结束日期 
公司1 4 9 
公司2 9 11 
公司3 13 19 
公司4 10 17 
  上例中,最多将会堂租借给两家公司。租借策略分别是租给公司1和公司3,或是公司2和公司3,也可以是公司1和公司4。注意会议中心一天最多租借给一个公司,所以公司1和公司2不能同时租借会议中心,因为他们在第九天重合了。 
  销售主管为了公平起见,决定按照如下的程序来确定选择何种租借策略:首先,将租借给客户数量最多的策略作为候选,将所有的公司按照他们发出请求的顺序编号。对于候选策略,将策略中的每家公司的编号按升序排列。最后,选出其中字典序最小[1]的候选策略作为最终的策略。 
  例中,会堂最终将被租借给公司1和公司3:3个候选策略是{(1,3),(2,3),(1,4)}。而在字典序中(1,3)< (1,4)< (2,3)。 
  你的任务是帮助销售主管确定应该将会堂租借给哪些公司。 
输入格式 
  输入的第一行有一个整数N,表示发出租借会堂申请的公司的个数。第2到第N+1行每行有2个整数。第i+1行的整数表示第i家公司申请租借的起始和终止日期。对于每个公司的申请,起始日期为不小于1的整数,终止日期为不大于109的整数。 
输出格式 
  输出的第一行应有一个整数M,表示最多可以租借给多少家公司。第二行应列出M个数,表示最终将会堂租借给哪些公司。 
数据规模和约定 
  对于50%的输入,N≤3000。在所有输入中,N≤200000。 
样例输入 

4  9 
9  11 
13  19 
10  17 
样例输出 

1  3 

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int MAXN = 2e5 + 10;
const int INF = 0x3f3f3f3f;
struct node
{
    int l, r;
    node() {}
    node(int l_, int r_) : l(l_), r(r_) {}
    bool operator < (const node &s) const
    {
        return l < s.l || (l == s.l && r < s.r);
    }
};
int n;
int tag[MAXN << 1];
int net[20][MAXN << 1];
int cnt = 0, limit = 1, limit_;
node A[MAXN];
node B[MAXN];
node tmp[MAXN];
set<node> ss;
set<node>::iterator it;
int bs(int x)
{
    for (int l = 0, r = limit - 1; l < r + 1;)
    {
        int m = (l + r) >> 1;
        if (x == tag[m])
        {
            return m;
        }
        if (x < tag[m])
        {
            r = m - 1;
        }
        else
        {
            l = m + 1;
        }
    }
    return 0;
}
bool cmp(const node &a,const node &b)
{
    return a.r < b.r || (a.r == b.r && a.l > b.l);
}
void discrete()
{
    sort(tag, tag + (n << 1));
    limit = (int)(unique(tag, tag + (n << 1)) - tag);
    for (int i = 0; i < n; i++)
    {
        tmp[i] = A[i] = node(bs(A[i].l), bs(A[i].r));
    }
    sort(tmp, tmp + n, cmp);
    int p = 0;
    B[cnt++] = tmp[0];
    for (int i = 1; i < n; ++i)
    {
        if (tmp[i].l > tmp[p].l)
        {
            B[cnt++] = tmp[p = i];
        }
    }
}
void next_set()
{
    int p = cnt - 1;
    net[0][limit] = INF;
    for (int j = limit - 1; j >= 0; j--)
    {
        if (p > -1 && j == B[p].l)
        {
            net[0][j] = B[p--].r + 1;
        }
        else
        {
            net[0][j] = net[0][j + 1];
        }
    }
    for (int i = 0; 1; i++)
    {
        bool flag = 0;
        net[i + 1][limit] = INF;
        for (int k = 0; k < limit; k++)
        {
            if (net[i][k] == INF)
            {
                net[i + 1][k] = INF;
            }
            else
            {
                net[i + 1][k] = net[i][net[i][k]];
            }
            if (net[i + 1][k] < INF)
            {
                flag = 1;
            }
        }
        if (!flag)
        {
            limit_ = i;
            break;
        }
    }
}

int max_cnt(int l, int r)
{
    if (l > r)
    {
        return 0;
    }
    r++;
    int ans = 0, p = l;
    for (int i = limit_; i > -1 && p < r; i--)
    {
        if (net[i][p] <= r)
        {
            p = net[i][p];
            ans += 1 << i;
        }
    }

    return ans;
}

bool query(int x)
{
    int l = A[x].l, r = A[x].r;
    it = ss.lower_bound(node(l, INF));

    if (it-- == ss.begin())
    {
        return 0;
    }

    int l_ = it->l, r_ = it->r;
    if (l_ > l || r_ < r)
    {
        return 0;
    }
    if (max_cnt(l_, l - 1) + max_cnt(r + 1, r_) + 1 < max_cnt(l_, r_))
    {
        return 0;
    }
    ss.erase(it);
    if (l_ < l)
    {
        ss.insert(node(l_, l - 1));
    }
    if (r < r_)
    {
        ss.insert(node(r + 1, r_));
    }
    return 1;
}
int main()
{
    cin >> n;
    int l, r;
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d", &l, &r);
        A[i] = node(l, r);
        tag[i << 1] = l;
        tag[(i << 1) + 1] = r;
    }
    discrete();
    next_set();
    printf("%d\n", max_cnt(0, limit - 1));
    ss.insert(node(0, limit - 1));
    for (int i = 0; i < n; i++)
    {
        if (query(i))
        {
            printf("%d ", i + 1);
        }
    }
    putchar(10);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值