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. 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. .net找List1和List2的差集

    有个需求是找两个自定义类泛型集合的差集: class Person { public string Name{get; set;} public string Country{get; set;} } ...

  3. erlang进程概述

    一.概述 与大多数的进程相反,Erlang中的并发很廉价,派生出一个进程就跟面向对象的语言中分配一个对象的开销差不多. 在启动一个复杂的运算时,启动运算.派生进程以及返回结果后,所有进程神奇的烟消云散 ...

  4. PHP中的 $_SERVER 函数说明详解

    用php在开发软件的时候,我们经常用到 $_SERVER[]这个函数,今天就来讲下这个数组的值,方便以后使用: A: $_SERVER['ARGC'] #包含传递给程序的 命令行参数的个数(如果运行在 ...

  5. IDEA安装教程

    1.下载安装程序A,链接:https://pan.baidu.com/s/1IAsGDbApfyNsHuS7_m0rdw 密码:fthp 2.下载一个配置程序B,下载安装之后,暂时不用管,之后会用到. ...

  6. display的属性值测试

    由于在学习CSS的display的属性值只针对block.inline.inline-block和flex进行过了解,并且自己观察得知列表中li的display属性是list-item,而想要触发BF ...

  7. IBM x3850 RAID5数据恢复过程

    [raid数据恢复故障描述]    需要进行数据恢复的是北京一家公司的IBM X3850服务器,服务器挂载了5块73G SAS硬盘组成raid5磁盘阵列,4号盘为热备盘(Hot-Spare),由于未知 ...

  8. 深入出不来nodejs源码-编译启动(1)

    整整弄了两天,踩了无数的坑,各种奇怪的error,最后终于编译成功了. 网上的教程基本上都过时了,或者是版本不对,都会报一些奇怪的错误,这里总结一下目前可行的流程. node版本:v10.1.0. 首 ...

  9. DataSourceBuilder.create().build()

    Spring Boot also provides a utility builder class DataSourceBuilder that can be used to create one o ...

  10. java获取一个月的天数

    import java.text.SimpleDateFormat; import java.util.Calendar; public class Test { public static void ...