ural 1247. Check a Sequence
1247. Check a Sequence
Time limit: 0.5 second
Memory limit: 64 MB
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
Problem Source: Ural State University Personal Programming Contest, March 1, 2003
Tags: none
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 }