ural 1247. Check a Sequence

1247. Check a Sequence

Time limit: 0.5 second
Memory limit: 64 MB

There is a sequence of integer numbers A1, A2, …, AS, and a positive integer N. It‘s known that all elements of the sequence {Ai} satisfy the restriction 0 ≤ Ai ≤ 100. Moreover, it‘s known that the sum of all elements of the sequence is equal to S + N. You are to write a program that given a sequence {Ai} and a number N will answer the question: is it true that for all 1 ≤ i ≤ j ≤ S the following inequality holds:

Ai + Ai+1 + … + Aj ≤ (j – i + 1) + N ?

Input

The first input line contains two separated with a space positive numbers S and N that do not exceed 30000. Then follow S lines with one number in a line that are elements of the sequence {Ai}.

Output

Output "YES", if the mentioned above inequality holds for all the values of the parameters i and j, and "NO" otherwise.

Samples

input output
4 3
2
3
0
2
YES
4 5
1
0
5
3
NO

Problem Author: Alexander Mironenko
Problem Source: Ural State University Personal Programming Contest, March 1, 2003

Tags: none  (hide tags for unsolved problems)

Difficulty: 245

题意:问一个数列是否满足对于任意的i,j,都有sigma(a[k]) <= (j-i+1)+M, i<=k<=j,其中M给出,1<=n<=30000

分析:

其实我这个傻逼一开始以为是斜率dp。

后来发现,那个式子不就是

sigma(a[k]-1) <= M吗。。。

不就是对数列减一,然后做个最大子段和和M比较。。。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <iostream>
 9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 typedef double DB;
17 #define For(i, s, t) for(int i = (s); i <= (t); i++)
18 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
19 #define Rep(i, t) for(int i = (0); i < (t); i++)
20 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
21 #define rep(i, x, t) for(int i = (x); i < (t); i++)
22 #define MIT (2147483647)
23 #define INF (1000000001)
24 #define MLL (1000000000000000001LL)
25 #define sz(x) ((int) (x).size())
26 #define clr(x, y) memset(x, y, sizeof(x))
27 #define puf push_front
28 #define pub push_back
29 #define pof pop_front
30 #define pob pop_back
31 #define ft first
32 #define sd second
33 #define mk make_pair
34 inline void SetIO(string Name)
35 {
36     string Input = Name+".in",
37     Output = Name+".out";
38     freopen(Input.c_str(), "r", stdin),
39     freopen(Output.c_str(), "w", stdout);
40 }
41
42 inline int Getint()
43 {
44     int Ret = 0;
45     char Ch = ‘ ‘;
46     bool Flag = 0;
47     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
48     {
49         if(Ch == ‘-‘) Flag ^= 1;
50         Ch = getchar();
51     }
52     while(Ch >= ‘0‘ && Ch <= ‘9‘)
53     {
54         Ret = Ret * 10 + Ch - ‘0‘;
55         Ch = getchar();
56     }
57     return Flag ? -Ret : Ret;
58 }
59
60 const int N = 30010;
61 int n, m, Arr[N], Ans;
62
63 inline void Input()
64 {
65     scanf("%d%d", &n, &m);
66     For(i, 1, n) scanf("%d", &Arr[i]);
67 }
68
69 inline void Solve()
70 {
71     int Cnt = Arr[1] - 1;
72     Ans = Cnt;
73     For(i, 2, n)
74     {
75         Cnt = max(Cnt + Arr[i] - 1, Arr[i] - 1);
76         Ans = max(Ans, Cnt);
77     }
78     if(Ans <= m) puts("YES");
79     else puts("NO");
80 }
81
82 int main()
83 {
84     #ifndef ONLINE_JUDGE
85     SetIO("F");
86     #endif
87     Input();
88     Solve();
89     return 0;
90 }

时间: 2024-12-09 10:46:48

ural 1247. Check a Sequence的相关文章

递推DP URAL 1081 Binary Lexicographic Sequence

题目传送门 1 /* 2 dp[i][1]/dp[i][0] 表示从左往右前i个,当前第i个放1或0的方案数 3 k -= dp[n][0] 表示当前放0的方案数不够了,所以必须放1,那么dp[n][0]个方案数都不能用了 4 相当于k减去这么多 5 详细解释:http://www.cnblogs.com/scau20110726/archive/2013/02/05/2892587.html 6 */ 7 #include <cstdio> 8 #include <algorithm&

URAL 1081 Binary Lexicographic Sequence

第13个位置第5个Bit :13>num[4] =>1 第四个bit 13-num[4]=5 :5<num[3] =>0 ,3-1 第三个Bit 5>num[2](3) 5-num[2]=2 ... #include<stdio.h> int num[45]; void init() { num[0]=1; num[1]=2; int k=2; while(k<44) { num[k]=num[k-1]+num[k-2]; k++; } } int main

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL1081 Binary Lexicographic Sequence(递归)

URAL 1081. Binary Lexicographic Sequence Time limit: 0.5 second Memory limit: 64 MB Description Consider all the sequences with length (0 < N < 44), containing only the elements 0 and 1, and no two ones are adjacent (110 is not a valid sequence of l

leveldb 源码阅读,细节记录memberTable

leveldb 是看着前辈们的大概分析,然后看着源码,将自己的疑惑和解决记录下来: Leveldb源码分析 从memberTable插入一条记录和查找一条记录从上而下分析 插入: 插入的函数 void MemTable::Add(SequenceNumber s, ValueType type,const Slice& key,const Slice& value) 参数: SequenceNumber    插入的序号,在skiplist里,这个序号是降序列的 ValueType typ

Reliability, Ordering and Congestion Avoidance over UDP

Introduction Hi, I’m Glenn Fiedler and welcome to the fourth article in my series Networking for Game Programmers In the previous article, we added our own concept of virtual connection on top of UDP. Now we’re going to add reliability, ordering and

动态规划——最长公共子串

引入: 最长公共子序列常用于解决字符串的相似度问题. 最长公共子序列(Longest Common Subsequence,LCS)与最长公共字串(Longest Common Substring):子串是串的一个连续的部分,子序列则是从不改变序列顺序,而从序列中去掉任意多个元素而获得的新的序列:也就是说,子串中字符的位置一定是连续的,而子序列不一定连续. a  not the 之一(得到的未必就是唯一的那个最长公共子串,只有长度是唯一的) --其余字符串问题,待续 解决方案: 1.穷举法(Br

3-Transport Layer

?Please indicate the source: http://blog.csdn.net/gaoxiangnumber1 Welcome to my github: https://github.com/gaoxiangnumber1 3.1 Introduction and Transport-Layer Services A transport-layer protocol provides for logical communication between application

leveldb memtable

memtable常驻于内存,需要按照key进行排序,通常意义上的话,可以使用二叉查找树来实现,跟进一步可以使用红黑树保证树的平衡,但是leveldb中使用了另外的一种数据结构:跳表Skip List. memtable声明在db/memtable.h中,定义如下: view plain class MemTable { public: // MemTables are reference counted.  The initial reference count // is zero and t