CF 338E Optimize! (线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
出题人题解没看懂。。。囧。
然后看了下tourist代码,很短,也很好理解。。。
我们将b排序之后,很显然如果组合的话肯定是贪心。
那么对于a的某个子串a'要满足条件的话,那么显然是所有的数和b中最大元素相加不小于h。
至少有len - 1个数的b中次大元素相加不小于h。。。以此类推那么首先预处理出对于a中的每个元素,和b串的哪些元素相加不小于h,显然是排序之后的二分
那么选中某个区间的数,就是一个区间覆盖,判断b中第i大元素是否至少被覆盖了i次。
为了方便,我们先将第i位减去一个i,然后判断区间最小值是否非负。
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lson step << 1
#define rson step << 1 | 1
using namespace std;
typedef long long LL;
const int N = 150005;
struct Node {
int left , right , add , mn;
}L[N << 2];
int n , h , b[N] , len , a[N];
void bulid (int step , int l , int r) {
L[step].left = l;
L[step].right = r;
L[step].add = 0;
L[step].mn = 0;
if (l == r) return ;
int m = (l + r) >> 1;
bulid (lson , l , m);
bulid (rson , m + 1 , r);
}
void update (int step , int l , int r , int v);
void push_down (int step) {
int l = L[step].left , r = L[step].right , m = (l + r) >> 1;
if (L[step].add) {
update (lson , l , m , L[step].add);
update (rson , m + 1 , r , L[step].add);
L[step].add = 0;
}
}
void push_up (int step) {
L[step].mn = min (L[lson].mn , L[rson].mn);
}
void update (int step , int l , int r , int v) {
if (L[step].left == l && L[step].right == r) {
L[step].mn += v;
L[step].add += v;
return ;
}
push_down (step);
int m = (L[step].left + L[step].right) >> 1;
if (r <= m) update (lson , l , r , v);
else if (l > m) update (rson , l , r , v);
else {
update (lson , l , m , v);
update (rson , m + 1 , r , v);
}
push_up (step);
}
int main() {
int t;
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
#endif
scanf ("%d %d %d" , &n , &len , &h);
for (int i = 0 ; i < len ; i ++)
scanf ("%d" , &b[i]);
sort (b , b + len);
bulid (1 , 0 , len);
for (int i = 0 ; i < n ; i ++) {
scanf ("%d" , &a[i]);
a[i] = lower_bound (b , b + len , h - a[i]) - b;
}
for (int i = 0 ; i < len ; i ++)
update (1 , i , i , -(i + 1));
for (int i = 0 ; i < len - 1 ; i ++)
update (1 , a[i] , len , 1);
int ans = 0;
for (int i = len - 1 ; i < n ; i ++) {
update (1 , a[i] , len , 1);
if (L[1].mn >= 0) ans ++;
update (1 , a[i - len + 1] , len , -1);
}
printf ("%d\n" , ans);
return 0;
}
CF 338E Optimize! (线段树)的更多相关文章
- cf 786 B 线段树优化建图
cf 786 B 链接 CF 思路 n个点,3种建边方式,规模\(O(n^2)\) 线段树优化建图 注意 读入的数据好坑啊,说好的v,u变成了u,v. 两棵树,一棵出,一棵入.线段树的作用只不过是按照 ...
- [CF 474E] Pillars (线段树+dp)
题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- Codeforces 338E - Optimize!(Hall 定理+线段树)
题面传送门 首先 \(b_i\) 的顺序肯定不会影响匹配,故我们可以直接将 \(b\) 数组从小到大排个序. 我们考虑分析一下什么样的长度为 \(m\) 的数组 \(a_1,a_2,\dots,a_m ...
- CF 666E Forensic Examination 【SAM 倍增 线段树合并】
CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...
- CF 552(div 3) E Two Teams 线段树,模拟链表
题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
- CF 1023D Array Restoration - 线段树
题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...
- CF 787D Legacy(线段树思想构图+最短路)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)
题目链接 题意: 一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作: 1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色 2. 查询某个节点的子树上(包括本身)有多少个不 ...
随机推荐
- CodeForce 569A
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Litt ...
- Cramfs、JFFS2、YAFFS2的全面对比
Cramfs.JFFS2.YAFFS2的全面对比http://blog.csdn.net/daofengdeba/article/details/7721340 由于嵌入式系统自身存在一些特殊要求,使 ...
- 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...
- a标签阻止跳转的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python成长之路16
阅读(72) 一:jQuery是一个兼容多浏览器的javascript类库,核心理念是write less,do more(写得更少,做得更多),对javascript进行了封装,是的更加便捷的开发, ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
- js正则:零宽断言
JavaScript正则表达式零宽断言 var str="abnsdfZL1234nvcncZL123456kjlvjkl"var reg=/ZL(\d{4}|\d{6})(?!\ ...
- 11427 - Expect the Expected(概率期望)
11427 - Expect the Expected Some mathematical background. This problem asks you to compute the expec ...
- Java 输出通过 InetAddress 获得的 IP 地址数组
使用 InetAddress 获取 IP 地址会得到一个 byte 数组 如果你直接输出这个数组,你会发现 IP 地址中的某些位变成了负数 比如 61.135.169.105 会输出成 61.-121 ...
- Chapter 10 模版方法模式
我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模版模式来处理. 模版方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模 ...