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 showmessage

    package com.example.yanlei.yl6; import android.annotation.TargetApi; import android.app.Activity; im ...

  2. c++ 操作Mysql ado

    #pragma once #ifndef DB_MYSQL_H #define DB_MYSQL_H   #include "stdafx.h" #include <wins ...

  3. 解决Eclipse中C++代码显示Symbol &#39;std&#39; could not be resolved的问题

    第一次在Eclipse中写C++代码,写了一个简单的hello world程序,还没有等我编译.就报出了各种错误,但是这么简单的代码.怎么可能这么多错误.于是没有理会.编译执行后,能够正常输出!!!H ...

  4. EasyUI datagrid border处理,加边框,去边框,都可以,easyuidatagrid

    下面是EasyUI 官网上处理datagrid border的demo: 主要是这句: $('#dg').datagrid('getPanel').removeClass('lines-both li ...

  5. mysql手动停止无响应查询方法

    http://www.chenweionline.cn/archives/61.htm

  6. kubernetes调度之pod优先级和资源抢占

    系列目录 Pod可以拥有优先级.优先意味着相对于其它pod某个pod更为重要.如果重要的pod不能被调度,则kubernetes调度器会优先于(驱离)低优先级的pod来让处于pending状态的高优先 ...

  7. angular 复选框checkBox多选的应用

    应用场景是这样的,后台返回的数据在页面上复选框的形式repeat出来 可能会有两种需求: 第一:后台返回的只有项,而没有默认选中状态(全是待选状态) 这种情况相对简单只要repeat出相应选项 第二: ...

  8. bootstrap-table自己配置

    function initTable(){ var methodNameSearch=$("#methodNameSearch").val(); var requestUrl =  ...

  9. Core Data 版本号迁移经验总结

    大家在学习和使用Core Data过程中,第一次进行版本号迁移的经历一定是记忆犹新,至少我是这种,XD.弄的不好,就会搞出一些因为迁移过程中数据模型出错导致的Crash.这里总结了一下Core Dat ...

  10. Python之Pandas库常用函数大全(含注释)

    前言:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. 继续一个新的库,Pandas库.Pandas库围绕Series类型和D ...