-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolve.c
More file actions
151 lines (141 loc) · 3.14 KB
/
solve.c
File metadata and controls
151 lines (141 loc) · 3.14 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<stdio.h>
int arr[100001];
int dp[100001][31];
int l[100001];
int r[100001];
int color[100001];
/*--------------------------STACK-----------------------------------*/
int MAXSIZE=1000001;
int stack[1000001];
int top = -1;
int isempty()
{
if(top <= -1)
return 1;
else
return 0;
}
int isfull()
{
if(top >= MAXSIZE)
return 1;
else
return 0;
}
int peek()
{
return stack[top];
}
void pop()
{
if(!isempty())
{
top = top - 1;
}
}
void push(int data)
{
if(!isfull())
{
top = top + 1;
stack[top] = data;
}
}
/*------------------------------------STACK_END------------------------*/
long long max(long long a, long long b)
{
if(a>b)return a;
else return b;
}
int main()
{
int n,c;
while(scanf("%d %d",&n,&c)==2 && n && c)
{
int i,j;
for(i=1; i<=n; i++)scanf("%d",&arr[i]);
for(i=1; i<=n; i++)
{
scanf("%d",&color[i]);
for(j=0; j<c; j++)
{
if(j==color[i])dp[i][j]=dp[i-1][j]+1;
else dp[i][j]=dp[i-1][j];
}
}
arr[0] = arr[n+1] = 0;
// push(0);
// for (i = 1; i <= n; i++) {
// while (arr[i] <= arr[peek()])
// {
// pop();
// }
// l[i] = peek() + 1, push(i);
// }
// while(!isempty())
// {
// pop();
// }
// push(n+1);
// for (i = n; i >= 1; i--) {
// while (arr[i] <= arr[peek()])
// {
// pop();
// }
// r[i] = peek() - 1, push(i);
// }
long long ans=0;
i=1;
int tp;
while (i <= n)
{
if (isempty() || arr[peek()] <= arr[i])
push(i++);
else
{
tp = peek(); // store the top index
pop(); // pop the top
int curr=peek();
int hoise=1;
for(j=0; j<c; j++)
{
if((dp[curr][j]-dp[i-1][j])==0)
{
hoise=0;
break;
}
}
if(hoise)
{
long long area_with_top = (long long)arr[tp] * (long long)(isempty() ? i : i - peek() - 1);
if (ans < area_with_top)
{
ans = area_with_top;
}
}
}
}
while(!isempty())
{
tp = peek();
pop();
int curr=peek();
int hoise=1;
for(j=0; j<c; j++)
{
if((dp[curr][j]-dp[i-1][j])==0)
{
hoise=0;
break;
}
}
if(hoise)
{
long long area_with_top = (long long)arr[tp] * (long long)(isempty() ? i : i - peek() - 1);
if (ans < area_with_top)
ans = area_with_top;
}
}
printf("%lld\n",ans);
}
}