Final Exam Arrangement

题目链接

  • 题意:

    输入n个左闭右开的线段,如果两个线段有重叠部分,那么这两个线段必然不能在一组。求,最少分几组,并且输出每组都有谁

  • 分析:

    将一个线段拆开成左右端点,排序。从左向右扫描,如果遇到的是左端点,那么直接加入到集合中,此时集合中的这些线段两两有重合部分,所以是可以分到同一组的;如果遇到的是右端点,那么当前线段之后将和当前线段没有重合点,必然不能放到一组。这样贪心的将每一个线段尽可能的分到一个组中(分到哪个组无所谓,只要分到了一个组中,答案就能减少),就可以保证答案是最少的。当一个线段不能分到前一个组中,必然是和前一个组中的某条线段冲突,所以保证了正确性。

const int MAXN = 1100000;

struct Node
{
    int v, id, isr;
    int operator< (const Node& rhs) const
    {
        if (v != rhs.v)
            return v < rhs.v;
        else
            return isr > rhs.isr;
    }
} ipt[MAXN];

VI ans[MAXN];
int vis[MAXN];

int main()
{
    int n, l, r;
    while (~RI(n))
    {
        CLR(vis, 0);
        REP(i, MAXN)
            ans[i].clear();
        REP(i, n)
        {
            RII(l, r);
            int x = i << 1, y = x | 1;
            ipt[x].isr = 0;
            ipt[x].v = l;
            ipt[y].isr = 1;
            ipt[y].v = r;
            ipt[x].id = ipt[y].id = i + 1;
        }
        n <<= 1;
        sort(ipt, ipt + n);
        int val = 0;
        REP(i, n)
        {
            if (ipt[i].isr)
            {
                if (vis[ipt[i].id] != val)
                    continue;
                val++;
            }
            else
            {
                ans[val].push_back(ipt[i].id);
                vis[ipt[i].id] = val;
            }
        }
        WI(val);
        REP(i, val) REP(j, ans[i].size())
            printf("%d%c", ans[i][j], j == (int)ans[i].size() - 1 ? '\n' : ' ');
    }
    return 0;
}

Final Exam Arrangement,布布扣,bubuko.com

时间: 2025-01-08 21:49:33

Final Exam Arrangement的相关文章

BNUOJ 33535 Final Exam Arrangement

In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own time slot during the week. We can represent the time slot of a course by an left-closed right-open interval [s, t). Now we are going to arrange the fin

Math 353/DSC 301, Spring 2019, Final Exam

Math 353/DSC 301, Spring 2019, Final ExamPlease remember to write down your name and student ID. This is the take-home portion whichhas four problems. Only independently-finished and fully-justified answer will receive full credit.1. [11 = 3 + 2 + 4

STA 471 Final Exam

STA 471 Due: 5/15/2019Final ExamWhen compiling your answers to the following questions, follow all guidelines for homeworkassignments listed in the syllabus. A hard copy of your work is to be turned in to my office(Kimball 810) by 5:00 PM on the due

ECON4016 - FINAL EXAM

ECON4016 - FINAL EXAMThe final exam consists 4 small projects. You can choose 2 of them to finish and send meyour report. For each of the small projects you choose, you should perform data analysisusing the data I provide to you and the techniques we

2019 Multi-University Training Contest 7 Kejin Player Final Exam

Kejin Player 期望DP 题意: 初始等级为1,每一级有四个参数 r , s , x , a . 每一级有一个概率p=r/s花费a的代价升级到下一级,失败可能会倒退到x级 设从 l 到 r 的期望为 g(l, r), 这种期望满足减法 g(l, r) = g(1, r) − g(1, l). 因为升级只能一级一 级升, 所以要从 1 升级到 r, 必然要经过 l. 求一个前缀和 sum[i+1]=sum[i]+ai * si / ri + (sum[i]-sum[x])*(si-ri)

oj--cs10116 final exam

http://cs101.openjudge.cn/practice/12560/ 样例的结果是对的,但是WA,还没找到原因 #include<cstdio> int map[105][105]; bool issur[105][105]; int main(){ int n,m; scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d&

有感于Java Final Exam

秋季学期的java已经结课了,当看到教务平台贴出的通知说考试形式为单选题时,心中喜忧参半. 可喜的是这种考试形式还是比较得民心,毕竟除了判断题,最好做的也就是单选题了. 然而期中考试选择题50%的命中率还历历在目,不免心生忧虑.然后后来的事实证明,果然我的忧虑不是毫无道理的,再次被虐,而且被虐的毫无怨言. 考试完后把还有印象的几道题目在eclipse上跑了跑,结果也是不堪回首.... 然后我决定把它们记下来,以期在不远的将来能够一雪前耻. #1.对,你没有看错,就是这道题,我当时靠着卓越的逆向思

Algorithm lesson final exam

1.algorithm analysis O B/W/AV/AMOR,混入其他问题,设计+分析 2.传统算法(肯定要考) 1)divide and conquer master therem. recursive tree.分析复杂度.递归树加起来得到最终结果 2)greedy algorithm example sort->select 拟阵 独立系统的贪心模板,直接得到近似比 3)dynamic programming sub-optimal structrue.编辑距离 3.graph a

加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL

Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Academia.edu) ADDITIONAL PRACTICE FOR THE FINAL In the following problems you will be asked to choose one of the four options (A)-(D). The options are sta