题目链接

题意:给出n个数,求加和大于x的最短区间的区间长度。

如果前i个数字和为y,那么如果前j数字的和小于等于y-x,那么i-j就是一种可能的情况,我们对于所有的i找出前面最大的j就可以了,因为数据量比较大,用树状数组来处理前n项的最大值,但是每个数字又可能比较大,所以先离散化处理一下。

AC代码

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 typedef long long LL;
5
6 using namespace std;
7
8 const double EPS = 1e-8;
9 const int N = 510000;
10 const int INF = 0x3f3f3f3f;
11
12 LL a[N], k;
13 int len;
14
15 struct node
16 {
17 int l; //区间左端
18 LL sum;
19 }p[N];
20
21 void Solve(int l, int r)
22 {
23 LL sum = 0, s = p[r].sum;
24 if(s < k)
25 return ;
26
27 while(l < r)
28 {
29 sum += a[l];
30 if(s - sum >= k)
31 {
32 p[r].sum = s - sum;
33 p[r].l = l + 1; // 更新区间左端
34 }
35 l++;
36 }
37 len = min(len, r - p[r].l + 1);
38 }
39
40 int main()
41 {
42 int t, n;
43 scanf("%d", &t);
44 while(t--)
45 {
46 memset(a, 0, sizeof(a));
47 memset(p, 0, sizeof(p));
48
49 scanf("%d %lld", &n, &k);
50
51 p[0].l = 1;
52 len = INF;
53
54 for(int i = 1; i <= n; i++)
55 {
56 scanf("%lld", &a[i]);
57 if(p[i-1].sum > 0)
58 {
59 p[i].sum = a[i] + p[i-1].sum;
60 p[i].l = p[i-1].l;
61 }
62 else
63 {
64 p[i].sum = a[i];
65 p[i].l = i;
66 }
67 Solve(p[i].l, i);
68 }
69 if(len == INF) len = -1;
70 printf("%d\n", len);
71 }
72
73 return 0;
74 }

原文出处

E - Minimal Subarray Length(连续区间和)的更多相关文章

  1. UVA 12697 Minimal Subarray Length

    Minimal Subarray Length Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...

  2. 6609 - Minimal Subarray Length

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. UVALive 6609 Minimal Subarray Length (查找+构建排序数组)

    描述:给定n个整数元素,求出长度最小的一段连续元素,使得这段元素的和sum >= X. 对整个数组先求出sum[i],表示前i个元素的和,然后依次求出以a[i]为起点的,总和>= X的最小 ...

  4. UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)

    题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...

  5. Individual Contest #1 and Private Training #1

    第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...

  6. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  8. [poj1113][Wall] (水平序+graham算法 求凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  9. *HDU 1392 计算几何

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. Linux文本三剑客总结

    Linux文本处理三剑客 grep 文本过滤(模式:pattern)工具 grep, egrep, fgrep(不支持正则表达式搜索) grep  grep: Global search REgula ...

  2. Canvas 如何画一个四分之一圆

    转: Canvas 如何画一个四分之一圆 HTML: Document JS: var c = document.getElementById('ctx') var ctx = c.getContex ...

  3. pytorch(07)数据模型的读取

    DataLoader与Dataset pytorch中的数据读取机制 graph TB DataLoader --> DataLoaderIter DataLoaderIter --> S ...

  4. 【粉丝问答10】C语言关键字static的使用详解

    视频地址:https://www.ixigua.com/6935761378816819748 粉丝提问 粉丝问题,总结一下: 关键字static的使用方法. 要想搞清楚关键字static的使用方法, ...

  5. rest framework renderers

    渲染器 前TemplateResponse实例可以被返回给客户端,它必须被渲染.渲染过程需要模板和上下文的中间表示,并把它变成能够提供给客户端的最后一个字节流. - Django文档 REST框架包含 ...

  6. WPF 应用 - 拖拽窗体、控件

    1. 拖拽窗体 使用 System.Windows.Window 自带的 DragMove() 方法即可识别窗体拖动. DragMove(); 2. 拖拽控件:复制.移动控件 <Grid> ...

  7. C# 应用 - 使用 WebClient 发起 Http 请求

    1. 需要的库类 \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.dll System.Net.WebCli ...

  8. IPFS挖矿原理介绍

    随着近几年区块链行业迅速发展,虚拟货币交易机制逐渐成熟,作为「区块链新贵」的 IPFS渐渐走入广大投资者的视线. IPFS 与其激励层的运作原理是投资者们必须要了解的.所以今天我就来和大家讲讲 IPF ...

  9. 利用matplotlib和cmaps根据已有的colormap,重新定义colormap

    算法网上这哥们总结的还可以[1] ,但是使用matplotlib自定义colormap自己掌握的还不够,写在这里 希望达到的目标 使用什么样的颜色,可以自己定义 方便的调用其他人的色标, 使用一部分c ...

  10. c++ 反汇编 构造函数和析构函数

    构造函数和析构函数出现的时机 局部对象 109: // 局部对象定义调用构造函数 110: 111: CNumber Number; 00C8A37D 8D 4D EC lea ecx,[Number ...