-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA12967.cpp
123 lines (114 loc) · 1.73 KB
/
UVA12967.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
UVA 12697 Solution Written By DemoVersion in 20140904.
*/
#include<iostream>
#include<iomanip>
#include<queue>
#include<stack>
#include<sstream>
#include<algorithm>
#include<list>
#include<map>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<set>
#define Author "DemoVersion"
using namespace std;
int dx[]={0,0,-1,1,1,-1,1,-1};
int dy[]={1,-1,0,0,1,1,-1,-1};
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const int mN=500100;
int good_luck_random(){
int ret=rand();
ret^=rand()<<8;
ret^=rand()<<16;
return ret;
}
ll A[mN],X;
int n;
bool check(int M){
int i,lastIn=0;
ll UlSum=0;
for(i=0;i<n;i++){
if(i>M){
if(A[lastIn]<0){
UlSum-=A[lastIn];
lastIn++;
}
}
if(UlSum<0){
lastIn=i;
UlSum=0;
}
if(i-lastIn>=M){
UlSum-=A[i-M];
lastIn++;
}
if(UlSum<0){
lastIn=i;
UlSum=0;
}
UlSum+=A[i];
if(UlSum>=X){
return 1;
}
}
return 0;
}
int main(){
ios_base::sync_with_stdio(0);
#ifdef _DEBUG
freopen("in.txt","r",stdin);
freopen("out2.txt","w",stdout);
#endif
ll UlSum=0,val;
int c,T,lastIn,i,minm,L,R,M,found,rM;
cin>>T;
while(T--){
cin>>n>>X;
minm=1<<28;
UlSum=0;
found=0;
for(i=0;i<n;i++){
if(UlSum<0)UlSum=0;
cin>>A[i];
UlSum+=A[i];
if(UlSum>=X){
found=1;
}
}
if(found==0){
cout<<-1<<endl;
continue;
}
R=n;
L=1;
while(L<R){
M=(L+R)/2;
rM=(L+R)/2;
lastIn=0;
UlSum=0;
found=0;
found=check(M);
for(i=0;i<25;i++){
if(found)break;
c=(R-L)/2+(R-L)%2;
M=rM-good_luck_random()%c;
found=check(M);
}
if(found){
R=M;
}else{
L=M+1;
}
}
cout<<R<<endl;
}
return 0;
}