Sicily 1800. Sequence

1800. Sequence

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Given a sequence S of n numbers and two length bounds L and U.

Your task is to write a program to find a segment S[i], S[i+1], …, S[j] with minimum sum over all segments of S with length at least L and at most U.

Input

The input consists of multiple datasets. The end of the input is indicated by a line containing a zero.

The format of each dataset is as follows.

n

L U

SS2 … Sn

Here, all data items are integers. n is the length of the sequence (N≤32767). L and Uare two length bounds where LUS1, S2, …, Sn are the sequence of n numbers.

Output

For each dataset, output the minimum sum of segment, in a separate line.

Sample Input

9
2 8
-3 2 -2 5 -4 1 -2 3 1
0

Sample Output

-5

Hint

the segment is -4 1 -2.

建立一棵线段树存储前缀和的最大值,注意从 0 开始

枚举区间右端点 i,查询 max(0,i-u) 到 i 范围内前缀和的最大值,用 i 处的前缀和减去它就是所求的最小值

其实一开始还想歪用 O(nlogn) 的分治法求最小连续子段和,枚举区间左右端点……这样就成了 O(n^2) 了(

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define rep(i,l,r) for(int i = l; i <= r; i++)
 6 #define clr(x,y) memset(x,y,sizeof(x))
 7 #define travel(x) for(Edge *p = last[x]; p; p = p -> pre)
 8 using namespace std;
 9 const int maxn = 32800;
10 inline int read(){
11     int ans = 0, f = 1; char c = getchar();
12     for(; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -1;
13     for(; isdigit(c); c = getchar()) ans = ans * 10 + c - ‘0‘;
14     return ans * f;
15 }
16 struct Node{
17     int l, r, s;
18 }t[maxn<<2];
19 int n, l, u, s[maxn];
20 void build(int u,int v,int w){
21     t[w].l = u; t[w].r = v;
22     if (u == v){
23         t[w].s = s[u]; return;
24     }
25     int mid = (u + v) >> 1;
26     build(u,mid,w<<1); build(mid+1,v,w<<1|1);
27     t[w].s = max(t[w<<1].s, t[w<<1|1].s);
28 }
29 int query(int u,int v,int w){
30     if (t[w].l == u && t[w].r == v) return t[w].s;
31     int mid = (t[w].l + t[w].r) >> 1;
32     if (v <= mid) return query(u,v,w<<1);
33     else if (u > mid) return query(u,v,w<<1|1);
34     else return max(query(u,mid,w<<1), query(mid+1,v,w<<1|1));
35 }
36 void work(){
37     l = read(); u = read();
38     s[0] = 0; rep(i,1,n) s[i] = s[i-1] + read();
39     build(0,n,1);
40     int ans = 0x7fffffff;
41     rep(i,l,n) ans = min(ans, s[i] - query(max(0,i-u),i-l,1));
42     printf("%d\n",ans);
43 }
44 int main(){
45     n = read();
46     while (n){
47         work(); n = read();
48     }
49     return 0;
50 }

时间: 2024-10-11 18:43:13

Sicily 1800. Sequence的相关文章

&nbsp; 使用tshark进行数据包分析

选项说明Options -r 读取数据包 -C 选择对应的配置文件 -d 解码为... -D 通过行进行打印输出 -e 定义需要打印的行内容 -E 定义具体的打印格式 -T 定义具体的打印方式 命令tshark -d <layer type>==<selector>,<decode-as protocol> tshark -r vmx.cap -d tcp.port==446,http 命令注解 在一些分析中可能会遇到接口信息没有采用标准的类型所以可以通过-d选项将其解

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

sicily 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size

Sicily 1779. Fibonacci Sequence Multiplication

1779. Fibonacci Sequence Multiplication Constraints Time Limit: 1 secs, Memory Limit: 63.9990234375 MB Description Maybe all of you are familiar with Fibonacci sequence. Now you are asked to solve a special version of Fibonacci sequence: The Multipli

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi