codeforces 31C Schedule 解题报告
题目链接:http://codeforces.com/problemset/problem/31/C
题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间。问通过删除一个lesson后能否使得所有lesson 持续的时间互不相交。注意,如果某个lesson结束的时刻恰好是另一个 lesson 的开始时刻,那么这两个lesson 也看作是不相交的。
看了题目之后,一如既往地,没什么想法= =。C题好多时候都这样,继续努力!!!
想着看Tutorial 直接学习,怎么知道没有滴,可能是太过久远了。只能看别人代码学了,学了个用时最少的GNU C++,不由得感叹,该人的解决方法真是巧妙,给个赞他,太聪明了!!!
我们先假定每个lesson 占据的时间是一段段的,左右两个端点分别是起始和终止时刻。用一个统计每个时刻出现次数的数组temp,对于第 i 个lesson 起始时刻in[i][0] 就 temp[in[i][0]]++,终止时刻 in[i][1] 就temp[in[i][1]]-- 。这样构造的好处应该就是把所有lesson 统一于只有一条特长线段来处理,可以发现,如果某一个时刻出现3次以上,就无论删除哪个lesson 都不可能使得所有lesson 的时间都不相交了,恰好2次的话可以尝试删除其中一个lesson 来验证之。如果某个lesson的结束时刻ti 是另一个lesson的开始时刻,就标记这个 ti 为0次。
可以发现,如果有解的话,次数标记数组temp 不会有超过3个1的时刻或者某个时刻出现3次以上出现的。
有点只可意会不能言转啊,就是很佩服这个人的解题思路。应该是多年做题的经验和直觉啦。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std; const int maxn = + ;
const int N = 1e6 + ; int in[maxn][];
int temp[N];
vector<int> ans; int main()
{
int n, i;
while (scanf("%d", &n) != EOF)
{
ans.clear();
memset(in, , sizeof(in));
memset(temp, , sizeof(temp));
for (i = ; i <= n; i++)
{
scanf("%d%d", &in[i][], &in[i][]);
temp[in[i][]]++;
temp[in[i][]]--;
}
int a = -, b = -;
int maxx = ;
for (i = ; i <= N; i++)
{
maxx += temp[i];
if (maxx > ) // <= 1表示本来的lesson已经两两不相交了,最终删除哪个lesson都满足条件
{
if (maxx >= ) // 怎么删除都不符合条件,因为有多于两个以上的 lesson 占据这个时刻
{
printf("0\n");
return ;
}
if (a == -)
a = i;
b = i;
}
}
for (i = ; i <= n; i++)
{
if (a == - || in[i][] <= a && b <= in[i][]-) // [a,b] 表示最小可删除区间,大于等于这个区间的lesson都可以删除
ans.push_back(i);
}
printf("%d\n", ans.size());
if (ans.size())
{
printf("%d", ans[]);
for (int i = ; i < ans.size(); i++)
printf(" %d", ans[i]);
printf("\n");
}
}
return ;
}
codeforces 31C Schedule 解题报告的更多相关文章
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
- codeforces 495C. Treasure 解题报告
题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...
- codeforces 490B.Queue 解题报告
题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...
- CodeForces 166E -Tetrahedron解题报告
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...
- codeforces 489A.SwapSort 解题报告
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
- codeforces 485A.Factory 解题报告
题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...
- codeforces 483A. Counterexample 解题报告
题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
随机推荐
- IOS 后台保持连接
当iphone应用程序进行网络编程时,切到后台后,socket连接会断掉,ios的设计就是这样. 但是好在apple公司也没有那么绝,还是有一些东西可以在后台运行的(backgroundmodes), ...
- 如何使用ssh远程编辑定时任务crontab?
linxu定时任务使用crontab,编辑crontab可以直接编辑:crontab -e:也可以直接读取文件 crontab file.这两种操作都不需要特殊权限sudo.区别在于,crontab ...
- cocos2d-x 事件分发机制 ——触摸事件监听
cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...
- python得到最长递增子串长度及起始位置【转】
def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - s ...
- The bean 'xxx' could not be injected as a 'xxx'because it is a JDK dynamic proxy that implements
启动springboot项目的时候示以下错误 Error starting ApplicationContext. To display the conditions report re-run yo ...
- AngularJS的表单验证提交示例
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsFormSubmit.rar 前台代码: <%@ page content ...
- LattePanda 之深入学习 Firmata通讯
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- JD笔试试题(凭记忆写的+人生感悟 try finally )
京东笔试:技术篇(一套卷.包含測试.算法,研发) 一:填空题(4分 * 15) 15 个 涉及的面很广的选择题,可是比較側重基础.包含数据结构的.c++类的,操作系统的,计算机网络的. 二:编程题(2 ...
- Triangle 三角形——找出三角形中从上至下和最小的路
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 串匹配算法讲解 -----BF、KMP算法
参考文章: http://www.matrix67.com/blog/archives/115 KMP算法详解 http://blog.csdn.net/yaochunnian/artic ...