BZOJ_2298_[HAOI2011]problem a_线段树

Description

一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低。”问最少有几个人没有说真话(可能有相同的分数)

Input

第一行一个整数n,接下来n行每行两个整数,第i+1行的两个整数分别代表ai、bi

Output

一个整数,表示最少有几个人说谎

Sample Input

3

2 0

0 2

2 2

Sample Output

1

HINT

100%的数据满足: 1≤n≤100000   0≤ai、bi≤n


分析:

首先对于每个人的说法ai,bi,可以把它转换一下,变成从ai+1~n-bi这段区间人的成绩都是相等的。

非常像贪心对吧,http://www.cnblogs.com/suika/p/8711400.html

但需要注意的是,这里每个人的ai,bi可能是相等的,这时他们说的一定同时对或同时错,和上面那道题不同,我们不知道这段区间剩余空间是多少。

于是我们将ai,bi相等的人合并,这样贪心就不能做了。

先排序再DP,设f[i]为从1~i这段区间最多能符合多少人的说法,对于每个说法ai,bi,用1~ai-1中的最大值+1来更新f[bi]。

注意ai,bi相等的人合并时个数要和区间长度取较小值。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100050
#define ls p<<1
#define rs p<<1|1
int t[N<<2],del[N<<2],n;
struct A {
int l,r,cnt;
}a[N],v[N];
bool cmp(const A &x,const A &y){
if(x.r==y.r) return x.l>y.l;
return x.r<y.r;
}
/*void build(int l,int r,int p) {
if(l==r) {
t[p]=1; return ;
}
int mid=l+r>>1;
build(l,mid,ls);
build(mid+1,r,rs);
t[p]=1;
}*/
void update(int l,int r,int x,int y,int p) {
if(l==r) {
t[p]=max(t[p],y); return ;
}
int mid=l+r>>1;
if(x<=mid) update(l,mid,x,y,ls);
else update(mid+1,r,x,y,rs);
t[p]=max(t[ls],t[rs]);
}
int query(int l,int r,int x,int y,int p) {
if(x<=l&&y>=r) return t[p];
int mid=l+r>>1,re=0;
if(x<=mid) re=max(re,query(l,mid,x,y,ls));
if(y>mid) re=max(re,query(mid+1,r,x,y,rs));
return re;
}
int main() {
scanf("%d",&n);
int i,x,y,tot=0,ans=0;
for(i=1;i<=n;i++) {
scanf("%d%d",&x,&y);
swap(x,y);
a[i].l=x+1; a[i].r=n-y;
}
sort(a+1,a+n+1,cmp);a[0].l=645656;
for(i=1;i<=n;i++) {
if(a[i].l!=a[i-1].l||a[i].r!=a[i-1].r) v[++tot].l=a[i].l,v[tot].r=a[i].r;
v[tot].cnt++;
}
/*build(1,n,1);
for(i=1;i<=tot;i++) {
if(v[i].l>v[i].r||v[i].l<1||v[i].r>n) continue;
int tmp=query(1,n,v[i].l,v[i].r,1);
if(tmp>0) {
ans+=min(v[i].cnt,v[i].r-v[i].l+1);
update(1,n,v[i].l,v[i].r,1);
}
}*/
for(i=1;i<=tot;i++) {
if(v[i].l>v[i].r||v[i].l<1||v[i].r>n) continue;
int tmp=query(0,n,0,v[i].l-1,1)+min(v[i].cnt,v[i].r-v[i].l+1);
ans=max(ans,tmp);
update(0,n,v[i].r,tmp,1);
}
printf("%d\n",n-ans);
}

BZOJ_2298_[HAOI2011]problem a_线段树的更多相关文章

  1. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  2. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  3. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  4. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  5. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  6. [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】

    题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...

  7. HDU5107---K-short Problem (线段树区间 合并、第k大)

    题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...

  8. CodeForces903G Yet Another Maxflow Problem 扫描线 + 线段树 + 最小割

    给定两条链\(A, B\),其中\(A\)链某些点向\(B\)链有连边,支持修改\(A\)链中的某条边权以及查询\(A_1\)到\(B_n\)的最大流 显而易见,\(A\)和\(B\)链中一定满足左部 ...

  9. ZOJ 3686 A Simple Tree Problem(线段树)

    Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...

随机推荐

  1. Roman to Integer(将罗马数字转成整数)

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. 面试题:JQuery有几种选择器?

    很多种,大概归纳为9种. (1)基本 #id element .class * selector1,selector2,selectorN (2)层次选择器: ancestor descendant ...

  3. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  4. Windows 10创意者更新ISO发布!官方下载

    http://news.mydrivers.com/1/526/526719.htm 去年7月份,微软面向Windows 10推出了“周年更新”,让系统变得更加稳定好用.今天,Windows 10迎来 ...

  5. 与班尼特·胡迪一起找简单规律(HZOJ-2262)

    与班尼特·胡迪一起找简单规律 Time Limit:  1 s      Memory Limit:   256 MB Description 班尼特·胡迪发现了一个简单规律 给定一个数列,1 , 1 ...

  6. R贡献文件中文

    贡献文件 注意: 贡献文件的CRAN区域被冻结,不再被主动维护. 英文 --- 其他语言 手册,教程等由R用户提供.R核心团队对内容不承担任何责任,但我们非常感谢您的努力,并鼓励大家为此列表做出贡献! ...

  7. 使用Rapidxml读取xml文件

    现有xml文件如上,写在一个string中.需要获取节点上元素的类别和属性信息,并存储到结构体表中. 结构体如下: 得到的结果如下:

  8. error LNK2001: 无法解析的外部符号 "public: char * __thiscall

    error LNK2001: 无法解析的外部符号 "public: char * __thiscall CamPinPadCtrl::KeysConvert(unsigned long,ch ...

  9. ArrayList源码分析超详细

    ArrayList源码分析超详解 想要分析下源码是件好事,但是如何去进行分析呢?以我的例子来说,我进行源码分析的过程如下几步: 找到类:利用 IDEA 找到所需要分析的类(ztrl+N查找ArraLi ...

  10. spring cloud 入门系列七:基于Git存储的分布式配置中心

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...