【CodeForces】【338E】Optimize!
线段树
先搞出来每个a[i]能连多少条边记为w[i]……如果对一组s[i],都满足w[i]-rank[i]>=0则这是一组合法方案,然后线段树维护w[i]-rank[i](第一个元素出去的时候后面所有的rank要-1,加入最后一个元素的时候后面的元素rank+1,建关于a[i]的权值线段树,记得离散化)
线段树维护的时候modify(区间修改)忘加push_down了……so sad
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-')r=-;
for(; isdigit(ch);ch=getchar()) v=v*+ch-'';
return r*v;
}
const int N=,INF=~0u>>;
//#define debug
/********************template*******************/
struct node{
int size,min,add;
}t[N<<];
#define L (o<<1)
#define R (o<<1|1)
#define mid (l+r>>1)
int n,m,len,H,w[N],a[N],b[N],c[N];
void maintain(int o,int l,int r){
if (l==r) return;
t[o].size=t[L].size+t[R].size;
if (t[o].size) t[o].min=min(t[L].min,t[R].min);
else t[o].min=INF,t[o].add=;
}
int ql,qr;
void push_down(int o,int l,int r){
if (!t[o].add) return;
t[L].add+=t[o].add; t[R].add+=t[o].add;
t[L].min+=t[o].add; t[R].min+=t[o].add;t[o].add=;
}
void modify(int o,int l,int r,int num){
if (ql>qr) return;
if (ql<=l && qr>=r) t[o].add+=num,t[o].min+=num;
else{
push_down(o,l,r);
if (ql<=mid) modify(L,l,mid,num);
if (qr>mid ) modify(R,mid+,r,num);
maintain(o,l,r);
}
}
void update(int o,int l,int r,int pos,int rank){
if (l==r){
t[o].size^=;
t[o].min=t[o].size?w[pos]-rank-t[o].size:INF;
}else{
push_down(o,l,r);
if (a[pos]<=mid) update(L,l,mid,pos,rank);
if (a[pos]>mid) update(R,mid+,r,pos,rank+t[L].size);
maintain(o,l,r);
}
}
void out(int o,int l,int r){
printf("%d %d %d %d %d %d\n",
o,l,r,t[o].size,t[o].add,t[o].min);
if (l==r) return;
else {out(L,l,mid); out(R,mid+,r);}
}
inline bool cmp(int x,int y){return a[x]<a[y];}
int main(){
#ifndef ONLINE_JUDGE
freopen("CF338E.in","r",stdin);
freopen("CF338E.out","w",stdout);
#endif
n=getint(); len=getint(); H=getint();
F(i,,len) b[i]=getint();
sort(b+,b+len+);b[len+]=INF;
F(i,,n){
c[i]=i; a[i]=getint();
w[i]=len-(lower_bound(b+,b+len+,H-a[i])-b-);
}
sort(c+,c+n+,cmp);
F(i,,n) a[c[i]]=i;
F(i,,n*) t[i].min=INF;
F(i,,len){
ql=a[i]+; qr=n;
modify(,,n,-);
update(,,n,i,);
} #ifdef debug
F(i,,n) printf("%d %d\n",a[i],w[i]);
out(,,n);puts("");
#endif
int ans=t[].min>=;
F(i,len+,n){
ql=a[i-len]+; qr=n;
update(,,n,i-len,);
modify(,,n,); ql=a[i]+; qr=n;
update(,,n,i,);
modify(,,n,-);
if (t[].min>=) ans++;
#ifdef debug
printf("%d\n",i);
out(,,n);puts("");
#endif
}
printf("%d\n",ans);
return ;
}
【CodeForces】【338E】Optimize!的更多相关文章
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【Codeforces Round #438 C】 Qualification Rounds
[链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...
- 【Codeforces Round #438 B】Race Against Time
[链接]h在这里写链接 [题意] 时针.分钟.秒针走不过去. 问你从t1时刻能不能走到t2时刻 [题解] 看看时针.分钟.秒针的影响就好. 看看是不是在整时的位置就好. 然后看看影响到x不能到y; 然 ...
- 【Codeforces Round #438 A】Bark to Unlock
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...
- 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼
目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...
- 【codeforces contest 1119 F】Niyaz and Small Degrees
题目 描述 \(n\) 个点的树,每条边有一个边权: 对于一个 \(X\) ,求删去一些边后使得每个点的度数 \(d_i\) 均不超过 \(X\) 的最小代价: 你需要依次输出 \(X=0 \to n ...
- 【codeforces #282(div 1)】AB题解
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
随机推荐
- C# @Page指令中的AutoEventWireup,CodeBehind,Inherits
AutoEventWireup 如果 Page 指令的 AutoEventWireup 属性被设置为 true(或者如果缺少此属性,因为它默认为 true) ,该页框架将自动调用页事件,即 Page_ ...
- sprintf函数减少字符串拼接错误
$return_string=""; foreach($cat_list as $value){ $return_string .= sprintf('<dd>< ...
- php循环创建目录
代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...
- Kafka入门学习随记(二)
====Kafka消费者模型 参考博客:http://www.tuicool.com/articles/fI7J3m --分区消费模型 分区消费架构图 图中kafka集群有两台服务器(Server), ...
- python encode decode unicode区别及用法
decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...
- C语言接口的写法(以toyls命令为例)
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- Python input()
在Python语言中,我们经常需要与用户实现交互,下面是一个小实例 # -*- coding:UTF-8 -*- #获取输入参数,并将输入的值存储到txt文件中 String1 = input(&qu ...
- [原创]EnterpriseDB测试key申请方法
各位有对EnterpriseDB感兴趣的朋友,可以通过邮件方式申请测试key: 发送邮件至:zws@focus-soft.com,官方收到邮件后会有专人与您联系,一般情况都会很快得到一个测试key. ...
- hdu 5412 CRB and Queries
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...
- android开发系列之代码整洁之道
说起代码整洁之道,想必大家想到更多的是那本经典重构书籍.没错,记得当时自己读那本书的时候,一边结合项目实战,一边结合书中的讲解,确实学到了很多东西,对我自己的编码风格影响极深.随着时间的流逝,书中很多 ...