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. android_浅析canvas的save()和restore()方法

    <span style="font-size:18px;"> </span> <span style="font-size:18px;&qu ...

  2. 近期微信上非常火的小游戏【壹秒】android版——开发分享

    近期在朋友圈,朋友转了一个html小游戏[壹秒],游戏的规则是:用户按住button然后释放,看谁能精准地保持一秒的时间.^_^刚好刚才在linuxserver上调试程序的时候server挂了,腾出点 ...

  3. Git以及github的使用方法(五),暂存区和工作区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...

  4. c++程序猿经典面试题(2)

    1.以下程序的输出结果是? #include<stdio.h> main(){ int b=3; int arr[]={6,7,8,9,10}; int *ptr=arr; *(ptr++ ...

  5. java性能监控工具jconsole-windows

    jconsole Starts a graphical console that lets you monitor and manage Java applications. Synopsis jco ...

  6. Content Provider 详解

    几个概念:Cursor. Content provider . Uri  .contentresolver 1. Cursor : 个人理解为数据库中的一行数据,它是每行数据的集合.它是一个类.通过它 ...

  7. javascript变量初始化位置

    变量在之前<script type="text/javascript"></script>(或引用的js文件)中初始化,可以正常访问. 运行程序:弹出123 ...

  8. 7.2 HAVING子句

    7.2 HAVING子句正在更新内容.请稍后

  9. 阿里云 访问控制RAM

    https://help.aliyun.com/product/28625.html 为用户分配最小权限 别名主要用于 RAM 用户登录以及成功登录后的显示名. 强烈建议您给主账号绑定多因素认证. 设 ...

  10. JAR、WAR、EAR(转载)

    转自:http://blog.csdn.net/mashengwang/article/details/6105189 区别:Jar.war.EAR.在文件结构上,三者并没有什么不同,它们都采用zip ...