题目

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

输入格式

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

输出格式

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

输入样例

3

2 0

0 2

2 2

输出样例

1

提示

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

题解

一个人的话意思就是自己的分数排名在区间\([a_i + 1,n - b_i]\)之间,且这个区间内的分数都相等

那么我们现在有\(n\)个这样的区间

先去掉那些\(l > r\)的区间

由于相等的区间具有相同的真假性,我们可以合并

相交的区间不能共存,问题就转化为了选若干个带权区间不相交的最大值

按\(r\)排序,设\(f[i]\)为第\(i\)个区间为止的最大答案

我们只需找到最大的\(j<i\)使得\(r_j <l_i\),那么

\[f[i] = max(f[i - 1],f[j] + v_i)
\]

可以二分查找

\(n - f[m]\)就是答案

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
struct inter{
int l,r,v;
}e[maxn];
inline bool operator <(const inter& a,const inter& b){
return a.l == b.l ? a.r < b.r : a.l < b.l;
}
inline bool cmp(const inter& a,const inter& b){
return a.r == b.r ? a.l < b.l : a.r < b.r;
}
int n,tot,f[maxn];
int find(int l,int r,int x){
int mid;
while (l < r){
mid = l + r + 1 >> 1;
if (e[mid].r < x) l = mid;
else r = mid - 1;
}
return l;
}
int main(){
int tn;
tn = n = read();
REP(i,n){
e[i].l = read() + 1;
e[i].r = n - read();
e[i].v = 1;
}
tot = 0;
for (int i = 1; i <= n; i++)
if (e[i].l <= e[i].r) e[++tot] = e[i];
n = tot;
sort(e + 1,e + 1 + n);
tot = 1;
for (int i = 2; i <= n; i++)
if (e[i].l == e[tot].l && e[i].r == e[tot].r)
e[tot].v++;
else e[++tot] = e[i];
REP(i,tot) e[i].v = min(e[i].v,e[i].r - e[i].l + 1);
sort(e + 1,e + 1 + tot,cmp);
f[1] = e[1].v;
for (int i = 2; i <= tot; i++){
int pre = find(0,i - 1,e[i].l);
f[i] = max(f[i - 1],f[pre] + e[i].v);
}
printf("%d\n",tn - f[tot]);
return 0;
}

BZOJ2298 [HAOI2011]problem a 【dp】的更多相关文章

  1. BZOJ2302 [HAOI2011]Problem c 【dp】

    题目 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1,a ...

  2. BZOJ 2298: [HAOI2011]problem a【动态规划】

    Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  7. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  8. 【dp】D. Caesar's Legions

    https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/D [题意]给定n1个A,n2个B,排成一排,要求A最多能连续k1个紧挨着,B最多 ...

  9. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...

随机推荐

  1. Java设计模式之责任链模式、职责链模式

    本文继续介绍23种设计模式系列之职责链模式.   什么是链 1.链是一系列节点的集合. 2..链的各节点可灵活拆分再重组.   职责链模式 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间 ...

  2. 设置DdtaGridView控件中网格线的样式

    实现效果: 知识运用: DataGridView控件的GridColor属性 //用来获取或设置网格线的颜色 public Color GridColor { get; set; } //属性值: C ...

  3. pysql用类进行封装

    pyMySQL用类进行封装 class SqlHelper(object): def __init__(self): self.connect() def connect(self): self.co ...

  4. ios基础学习

    action中调用函数方法别忘了冒号1. 各个视图之间的关系要分辨清楚 2. MVC (Model-View-Controller). In this pattern, models keep tra ...

  5. js类型判别大合集

    1.typeof number,string,boolean,undefined,symbol,object,function 对象中除了函数为function,其他对象都判别为object, 缺陷: ...

  6. PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0  ...

  7. mysql crash cource 书中实例

    样例表 CREATE TABLE customers(  cust_id      int       NOT NULL AUTO_INCREMENT,  cust_name    char(50)  ...

  8. 【转】matlab练习程序(奇异值分解压缩图像)

    介绍一下奇异值分解来压缩图像.今年的上半年中的一篇博客贴了一篇用奇异值分解处理pca问题的程序,当时用的是图像序列,是把图像序列中的不同部分分离开来.这里是用的不是图像序列了,只是单单的一幅图像,所以 ...

  9. WCF_基础学习

    1.https://www.cnblogs.com/swjian/p/8126202.html 2.https://www.cnblogs.com/dotnet261010/p/7407444.htm ...

  10. JQuery 在线编辑器和手册

    JQuery 在线编辑器 JQuery 在线编辑器 JQuery 菜鸟教程 手册 JQuery 菜鸟教程 手册