BZOJ_2298_[HAOI2011]problem a_线段树
BZOJ_2298_[HAOI2011]problem a_线段树
Description
Input
第一行一个整数n,接下来n行每行两个整数,第i+1行的两个整数分别代表ai、bi
Output
一个整数,表示最少有几个人说谎
Sample Input
2 0
0 2
2 2
Sample Output
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_线段树的更多相关文章
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- 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 ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...
- HDU5107---K-short Problem (线段树区间 合并、第k大)
题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...
- CodeForces903G Yet Another Maxflow Problem 扫描线 + 线段树 + 最小割
给定两条链\(A, B\),其中\(A\)链某些点向\(B\)链有连边,支持修改\(A\)链中的某条边权以及查询\(A_1\)到\(B_n\)的最大流 显而易见,\(A\)和\(B\)链中一定满足左部 ...
- 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 ...
随机推荐
- oracle 导入/导出遇到的 问题总结
0925: 解决oracle 11g空数据 exp 少表的问题 1:生成处理语句 Select 'alter table '||table_name||' allocate extent;' from ...
- DBC的故事
1.DBC定义 DBC(data base CAN)是汽车ECU间进行CAN通讯的报文内容,有了它相互之间才能听懂. 2.DBC查看 DBC是文本文件,可以用记事本打开,一般都用CANdb++,可以更 ...
- scons脚本示例
import os def list_dir(dir): all_dirs = [] for root, dirs, files in os.walk('./', True): for name in ...
- windows10不能修改hosts解决方案(亲测)
hosts文本解释: 有时候我们要破解一些软件与服务器通讯,所以通常都需要更改Hosts文件来达到目的,XP系统可以直接修改保存,但是Win10系统却提示没有权限去修改,那么我们要怎样办呢,我们修改的 ...
- Yii2访问自定义模块下的controller
之前,由于所要访问的controller都是位于根目录下的controllers目录下,就像下面这样: 此时,我们可以直接通过 localhost/basic/web/index.php?r=dao/ ...
- Jquery浅克隆与深克隆
Jquery浅克隆与深克隆 JavaScript部分 $("div").on('click', function() {//执行操作}) //clone处理一 $("di ...
- PHP中的 $_SERVER 函数说明详解
用php在开发软件的时候,我们经常用到 $_SERVER[]这个函数,今天就来讲下这个数组的值,方便以后使用: A: $_SERVER['ARGC'] #包含传递给程序的 命令行参数的个数(如果运行在 ...
- windows + maven + eclipse
一处教程:http://www.yiibai.com/maven/maven_overview.html Maven中央仓库信息速查:http://maven.outofmemory.cn/ ...
- Java父类对象调用子类实体:方法重写与动态调用
众所周知Java的handle和C++的ponter而不是object对应,我们很熟悉C++的父类pointer调用子类实体的例子,那么对于Java的handle是不是也可以这样呢? 这里我先给一个例 ...
- Centos制作本地yum源
本地YUM源制作 1. YUM相关概念 1.1. 什么是YUM YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shel ...