题目链接 \(Click\) \(Here\)

\(DP\)神题。以后要多学习一个,练一练智商。

关键点在于把“有\(a_i\)个人分数比我高,\(b_i\)个人分数比我低”这句话转换成“排名为\(a_i+1\),且有\(n-a_i-b_i\)个人和我分数相同“。解决了这一点,问题就解决了一大半,接下来就变成了最大不相交区间集合选择问题。本来我是用最长路写的,不知道为什么出锅了,所以就改用\(DP\)+二分了。

#include <bits/stdc++.h>
using namespace std; const int N = 100010; struct Segment {
int l, r, sz; Segment (int _l = 0, int _r = 0, int _sz = 0) {
l = _l, r = _r, sz = _sz;
} bool operator < (Segment rhs) const {return l == rhs.l ? r < rhs.r : l < rhs.l;}
bool operator == (Segment rhs) const {return l == rhs.l && r == rhs.r;}
} arr[N]; map <Segment, int> mp; int n, a[N], b[N], tot, dp[N], dis[N], vis[N]; bool cmp (Segment lhs, Segment rhs) {return lhs.r == rhs.r ? lhs.l < rhs.l : lhs.r < rhs.r;} int main () {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
if (a[i] + b[i] <= n - 1) {
arr[++tot] = Segment (a[i] + 1, n - b[i], n - a[i] - b[i]);
mp[arr[tot]]++;
}
}
sort (arr + 1, arr + 1 + tot);
tot = unique (arr + 1, arr + 1 + tot) - arr - 1;
for (int i = 1; i <= tot; ++i) {
mp[arr[i]] = min (mp[arr[i]], arr[i].sz);
// printf ("Segment %d = [%d, %d]\n", i, arr[i].l, arr[i].r);
}
sort (arr + 1, arr + 1 + tot, cmp);
for (int i = 1; i <= tot; ++i) {
dp[i] = max (dp[i], dp[i - 1]);
int l = 1, r = i;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (arr[mid].r < arr[i].l) {
l = mid;
} else {
r = mid - 1;
}
}
dp[i] = max (dp[i], dp[r] + mp[arr[i]]);
}
cout << n - dp[tot] << endl;
}

Luogu P2519 [HAOI2011]problem a的更多相关文章

  1. [luogu] P2519 [HAOI2011]problem a (贪心)

    P2519 [HAOI2011]problem a 题目描述 一次考试共有n个人参加,第i个人说:"有ai个人分数比我高,bi个人分数比我低."问最少有几个人没有说真话(可能有相同 ...

  2. Luogu P2522 [HAOI2011]Problem b

    如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...

  3. 【题解】Luogu P2522 [HAOI2011]Problem b

    原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 我们看题面,让求对于区间\([a,b]\)内的整数x和\([c,d]\)内的y,满足$ gcd(x,y)=k$的数对的个数 我们珂以跟容斥原理(二 ...

  4. P2519 [HAOI2011]problem a

    思路 神仙思路,就差一步就能想出来了... 看到第i个人给出的条件,发现有\(a_i\)个大于,\(b_i\)个小于并不好处理 考虑把条件转化成第i个人对应的排名处理,设第i个人的排名为\(a_i+1 ...

  5. Luogu P2522 [HAOI2011]Problem b 莫比乌斯反演

    设$f(d)=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)==d],\\F(n)=\sum_{n|d}f(d)=\lfloor \frac{N}{n} \rfloor \lflo ...

  6. 洛谷 P2519 [HAOI2011]problem a

    传送门 考虑转化为求最多说真话的人数 设$f(i)$表示排名前$i$的人中最多说真话的人的数量,考虑转移,如果由$j$转移而来,可以设$[j,i]$之间的人全都分数相等,那么式子就是$f[i]=f[j ...

  7. luogu 2519 [HAOI2011]problem a 动态规划+树状数组

    发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...

  8. P2522 [HAOI2011]Problem b (莫比乌斯反演)

    题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...

  9. 洛谷P2522 - [HAOI2011]Problem b

    Portal Description 进行\(T(T\leq10^5)\)次询问,每次给出\(x_1,x_2,y_1,y_2\)和\(d\)(均不超过\(10^5\)),求\(\sum_{i=x_1} ...

随机推荐

  1. PHPStorm 配置命名空间

    文件-设置-Directories 选中:application     点击顶部:Sources,右侧会出现 Source Floders 配置项 点击:p进行设置 输入app\

  2. php new self()

    php里new self() 一般在类内部使用,作用是对自身类实例化 <?php class test{ public function __construct(){        echo ' ...

  3. freemarker 设置中文

    在web中添加一段代码 <servlet> <servlet-name>freemarker</servlet-name> <servlet-class> ...

  4. Python——Label控件说明

    Anchor :   标签中文本的位置: background(bg)foreground(fg) :背景色:前景色: borderwidth(bd) :边框宽度: width  .height   ...

  5. spring boot web开发 简单的增删改查和spring boot 自带的Junit测试 案例

    创建 web项目 配置pom.xml文件   ------相当于jar包 配置application.yml -----配置文件(spring数据库连接.server服务.logging日志等) 创建 ...

  6. hdu1878-并查集,欧拉回路

    纯裸题..写着方便理解... 题意:判断一个无向图是否存在欧拉回路... 解题思路:并查集判断一下是否联通,然后再判断一下点的度数是否为偶数就行了: #include<iostream> ...

  7. windows 动态库的封装以及调用

    1.一个程序从源文件编译生成可执行文件的步骤:预编译 -->  编译 -->  汇编 --> 链接(1)预编译,即预处理,主要处理在源代码文件中以“#”开始的预编译指令,如宏展开.处 ...

  8. HTML知识点总结[部分]

    Web服务的本质(socket实例) import socket def handle_request(client): buf = client.recv(1024) client.send(byt ...

  9. CH2906 武士风度的牛(算竞进阶习题)

    水..... 直接bfs... #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; typedef l ...

  10. 基于FPGA的UART协议实现(通过线性序列机)

    //////////////////2018/10/15 更新源代码: 实现uart这东西其实早就写了,不过不太完善,对于一个完美主义者来说,必须解决掉它. 1.什么是UART?        通用异 ...