Codeforces 931.F Teodor is not a liar!
1 second
256 megabytes
standard input
standard output
Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha.
Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point xi, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions.
Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice.
First line of input contains two integer numbers: n and m (1 ≤ n, m ≤ 100 000) — amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about.
ith of next n lines contains two integer numbers li and ri (1 ≤ li ≤ ri ≤ m) — left and right ends of ith segment in the picture. Note that that left and right ends of segment can be the same point.
It is guaranteed that there is no integer point, that belongs to all segments.
Single line of output should contain one integer number k – size of largest set (xi, cnt(xi)) where all xi are different, 1 ≤ xi ≤ m, and cnt(xi) is amount of segments, containing point with coordinate xi, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know n).
2 4
1 2
3 4
4
4 6
1 3
2 3
4 6
5 6
5
First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows cnt(xi) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment.
In second example Sasha can ask about 5 points e.g. 1, 2, 3, 5, 6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him.
题目大意:有一条线段,上面的点被若干条线段覆盖着. Sasha想知道是否存在一个整点被所有的线段覆盖,她每次可以任选一个点,Teodor会告诉她这个点被多少个线段覆盖,但是Sasha不知道有多少条线段.求Sasha最多猜多少次还不知道这个问题的答案. 也就是说,如果你最多猜n次能知道答案,那么输出n-1.如果猜不到答案就输出n.
分析:这种题目把图一画,各种情况考虑一下就能做出来了.
什么情况下Sasha能知道答案呢?
在这幅图中,1,2,3都被猜过了,覆盖2的线段数小于覆盖1,3的,而线段是连续的,说明有线段到2这个点就中断了,自然就没有整点被所有的线段给覆盖了. 同样的,如果覆盖2的线段数大于覆盖1,3的,也是能够猜出来的. 为了使猜的次数最多,把1,3全都猜完就行了.
所以究竟是求什么呢? 要求猜的数组成的子序列中不能有两个凸起的部分,只能一边是单调函数,另一边也是单调函数.那么就是要求一个最长的单峰子序列.树状数组扫两次就好了.
小细节:树状数组查询,修改的数不能是0,而这道题中可能存在点没有被线段覆盖,所以要默认有一条线段覆盖了所有点.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
int n,m,c1[maxn],c2[maxn],a[maxn],sum,f1[maxn],f2[maxn],ans; int add1(int x,int v)
{
while (x <= n)
{
c1[x] = max(c1[x],v);
x += x & (-x);
}
} int query1(int x)
{
int res = ;
while (x)
{
res = max(res,c1[x]);
x -= x & (-x);
}
return res;
} int add2(int x,int v)
{
while (x <= n)
{
c2[x] = max(c2[x],v);
x += x & (-x);
}
} int query2(int x)
{
int res = ;
while (x)
{
res = max(res,c2[x]);
x -= x & (-x);
}
return res;
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
int l,r;
scanf("%d%d",&l,&r);
a[l]++;
a[r + ]--;
}
for (int i = ; i <= m; i++)
{
sum += a[i];
a[i] = sum + ;
}
for (int i = ; i <= m; i++)
{
f1[i] = query1(a[i]) + ;
add1(a[i],f1[i]);
}
for (int i = m; i >= ; i--)
{
f2[i] = query2(a[i]) + ;
add2(a[i],f2[i]);
}
for (int i = ; i <= m; i++)
ans = max(ans,f1[i] + f2[i] - );
printf("%d\n",ans); return ;
}
Codeforces 931.F Teodor is not a liar!的更多相关文章
- Codeforces 931F - Teodor is not a liar!
931F - Teodor is not a liar! 思路: 最长上升子序列 先差分数组染色 如果存在一个点,被所有区间包含,那么这张图一定是山峰状,如下图: 那么只要分别从前和从后找一个最长非严 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 931 C. Laboratory Work
http://codeforces.com/problemset/problem/931/C 题意: 给定一个数列,要求构造一个等长的数列,使得数列的平均值等于给定数列,并且使得构造出的数列中与原数列 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- Codeforces 622 F. The Sum of the k-th Powers
\(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
随机推荐
- 【python 3.6】调用另一个文件的类的方法
文件1:test12.py 文件2:test13.py 文件1 如下: #!/usr/bin/python # -*- coding: utf-8 -*- ''' ''' class abcd(obj ...
- Qt应用程序重启
重启应用程序是一种常见的操作,在Qt中实现非常简单,需要用到QProcess类一个静态方法: // program, 要启动的程序名称 // arguments, 启动参数 bool startDet ...
- WEB前端开发流程总结
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 WEB前端开发项目流程总结 1.新建项目 ...
- “Hello World!”团队第六周的第五次会议
今天是我们团队“Hello World!”团队第六周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...
- struts传值方式ModelDriven的使用
struts传值不需要用到request,struts会处理好. 1.不是面向对象直接在jsp页面和Java代码都写:name,password... 以下为面向对象 2.action类实现Model ...
- DP---(POJ1159 POJ1458 POJ1141)
POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题 ...
- DB2 UDB V8.1 管理
在DB2中有关实例(Instance), 数据库(Database),表空间(TableSpace),容器(Container)等概念: 在一个操作系统中,DB2数据服务可以同时运行多个实例(有别于O ...
- MySQL的并发访问控制(锁)
前言:任何的数据集只要支持并发访问模型就必须基于锁机制进行访问控制 锁种类 读锁:共享锁,允许给其他人读,不允许他人写写锁:独占锁, 不允许其他人读和写 锁类型 显示锁:用户手动请求读锁或写锁隐式锁: ...
- CCF——相邻数对201409-1
问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好 ...
- 结对项目之对PIE的测试程序
项目要求: 构造程序,分别是: 不能触发Fault. 触发Fault,但是不能触发Error. 触发Error,但是不能产生Failure. 结对对象:陈秋月 学号:2013110404 博客地址 ...