Interval [STL、双指针、二分]

题目链接 http://acm.hpu.edu.cn/problem.php?id=1193

或者

题目链接 http://acm.nyist.net/JudgeOnline/problem.php?pid=1316

看了题解有点迷,标程很厉害,很巧妙。先求出前缀和 presum[MAXN],区间长度至少是L,所以想到的是二分法,解决。二分具体:枚举左端点,从 N-L+1 到 l>=1;右端点一开始只有presum[N];所以左端点往左,维护一个右端点的前缀和,之后二分找    (左端点前缀和+M)>=presum [mid]  ;

那么满足条件的就有  (有序数组个数 - mid) ;  维护有序需要将presum [r] 插入,我使用内存复制比较快;

#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h> using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++) const int MAXN=10000+10;
int presum[MAXN];
int bin[MAXN];
int temp[MAXN];
void insert(int pos,int value,int _size)
{
memcpy(temp,bin+pos,(_size-pos)*sizeof(int));
bin[pos]=value;
memcpy(bin+pos+1,temp,(_size-pos)*sizeof(int));
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int N, L, M;
scanf("%d%d%d", &N, &L, &M);
for(int i = 1; i <= N; i++)
{
int v;
scanf("%d", &v);
presum[i] = presum[i - 1] + v;
}
int T = N;
int ans = 0;
for(int l=N-L+1,len=0,r=N; l>=1; l--,r--)
{
int pos;
pos=lower_bound(bin,bin+len, presum[r])-bin;
insert(pos,presum[r],len++);
pos=lower_bound(bin,bin+len,presum[l-1]+M)-bin;
int temp=len-pos;
ans+=temp;
}
printf("%d\n", ans);
}
return 0;
} /**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://blog.csdn.net/wr_technology **/
/**************************************************/

hpuoj 1193: Interval的更多相关文章

  1. Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx

    问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...

  2. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  3. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...

  5. MySQL interval()函数

    INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...

  6. oracle11g interval(numtoyminterval())自动创建表分区

    Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...

  7. maven执行报错resolution will not be reattempted until the update interval of nexus h

    maven在执行过程中抛错: 引用 ... was cached in the local repository, resolution will not be reattempted until t ...

  8. Leetcode Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. Hdu 5489 合肥网络赛 1009 Removed Interval

    跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...

随机推荐

  1. 017.View与窗口:AttachInfo

    每一个View都需要依赖于窗口来显示,而View和窗口的关系则是放在View.AttachInfo中,关于View.AttachInfo的文章少,因为这个是View的内部类而且不是公共的,在应用层用的 ...

  2. Android开发——内存优化 图片处理

    8.  用缓存避免内存泄漏 很常见的一个例子就是图片的三级缓存结构,分别为网络缓存,本地缓存以及内存缓存.在内存缓存逻辑类中,通常会定义这样的集合类. private HashMap<Strin ...

  3. WMS8_基本操作

    建立分拣[收货.出货.领料]         点击仪表盘上的任何一个 All operations 链接切换至分拣 列表视图         点击 creae 按钮,建立一个新的分拣     part ...

  4. C中的预编译编译链接

        http://ke.qq.com/webcourse/index.html#course_id=67888&term_id=100058920&taid=13934591901 ...

  5. 关于Swiper(概念)

    Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架,使用硬件加速过渡(如果该设备支持的话). 主要使用于移动端的网站.移动web apps,native apps和hybrid apps. ...

  6. 转 Python Selenium设计模式-POM

    前言 本文就python selenium自动化测试实践中所需要的POM设计模式进行分享,以便大家在实践中对POM的特点.应用场景和核心思想有一定的理解和掌握. 为什么要用POM 基于python s ...

  7. vim 模式切换

    1. 从插入模式退回到normal模式 <esc> <C-c> <C-[>

  8. Learning English From Android Source Code:2 Ampersand

    这一次想把标点符号的英语表达总结一下,这些单词非常重要但easy被我们忽视.以我的经验,还是多认识几个.以备不时之需. 以下从"标点符号"開始: punctuation [英][ˌ ...

  9. Apcahe Shiro学习笔记(二):通过JDBC进行权限控制

    一.概述: 官方对Realm(领域)的描述:https://www.infoq.com/articles/apache-shiro 其功能本质上是一个安全特定的DAO,用于链接数据持久层(任何形式的都 ...

  10. Oracle学习(十三):闪回

    1.知识点:能够对比以下的录屏进行阅读 SQL> --1. 错误地删除了记录 SQL> --2. 错误地删除了表 SQL> --3. 查询历史记录 SQL> --4. 怎样撤销 ...