-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsecvmax.cpp
81 lines (80 loc) · 1.7 KB
/
secvmax.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
# include <fstream>
# include <algorithm>
# define inf 9999999999
using namespace std;
ifstream f("secvmax.in");
ofstream g("secvmax.out");
struct elem
{
int nr,i,sol;
}a[100005],cp[100005];
bool cmp (elem x, elem y)
{
if (x.nr>=y.nr) return 0;
else return 1;
}
bool cmp2 (elem x, elem y)
{
if (x.i>=y.i) return 0;
else return 1;
}
int i,n,m,VV,poz,maxx;
int st[100005], dr[100005], v[100005], ap[100005];
void procesare ()
{
int i;
VV=1;
for (i=1; i<=m; ++i) //pt fiecare intrebare
{
while (VV<=n && a[i].nr>=cp[VV].nr)
{
poz=cp[VV].i; ++VV; ap[poz]=1;
st[poz]=poz; dr[poz]=poz;
maxx=max(maxx,1);
if (st[poz-1] && dr[poz+1])
{
maxx=max(maxx, dr[poz+1]-st[poz-1]+1);
st[dr[poz+1]]=st[poz-1];
dr[st[poz-1]]=dr[poz+1];
continue;
}
if (st[poz-1])
{
maxx=max(maxx, poz-st[poz-1]+1);
st[poz]=st[poz-1];
dr[st[poz-1]]=poz;
continue;
}
if (dr[poz+1])
{
maxx=max(maxx, dr[poz+1]-poz+1);
st[dr[poz+1]]=poz;
dr[poz]=dr[poz+1];
continue;
}
}
a[i].sol=maxx;
}
sort (a+1, a+m+1, cmp2);
for (i=1; i<=m; ++i)
g<<a[i].sol<<"\n";
}
int main ()
{
f>>n>>m;
for (i=1; i<=n; ++i)
{
f>>v[i];
cp[i].nr=v[i];
cp[i].i=i;
}
for (i=1; i<=m; ++i)
{
f>>a[i].nr;
a[i].i=i;
}
sort (a+1, a+m+1, cmp);
sort (cp+1, cp+n+1, cmp);
procesare ();
return 0;
}