-
Notifications
You must be signed in to change notification settings - Fork 0
/
week8-4.c
172 lines (143 loc) · 2.66 KB
/
week8-4.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int n,k;
struct Deque
{
int arr[INT_MAX];
int front;
int rear;
int size;
};
struct Deque * createDeque(int size){
struct Deque * temp=(struct Deque *)malloc(sizeof(struct Deque));
temp->size=size;
temp->front=-1;
temp->rear=0;
return temp;
}
int isFull(struct Deque * dq)
{
return ((dq->front == 0 && dq->rear == dq->size-1)||
dq->front == dq->rear+1);
}
int isEmpty (struct Deque * dq)
{
return (dq->front == -1);
}
void insertfront(struct Deque * dq,int key)
{
if (isFull(dq))
{
return;
}
if (dq->front == -1)
{
dq->front = 0;
dq->rear = 0;
}
else if (dq->front == 0)
dq->front = dq->size - 1 ;
else
dq->front = dq->front-1;
dq->arr[dq->front] = key ;
}
void insertrear(struct Deque * dq,int key)
{
if (isFull(dq))
{
return;
}
if (dq->front == -1)
{
dq->front = 0;
dq->rear = 0;
}
else if (dq->rear == dq->size-1)
dq->rear = 0;
else
dq->rear = dq->rear+1;
dq->arr[dq->rear] = key ;
}
void deletefront(struct Deque * dq)
{
if (isEmpty(dq))
{
return ;
}
if (dq->front == dq->rear)
{
dq->front = -1;
dq->rear = -1;
}
else if (dq->front == dq->size -1)
dq->front = 0;
else
dq->front = dq->front+1;
}
void deleterear(struct Deque * dq)
{
if (isEmpty(dq))
{
return ;
}
if (dq->front == dq->rear)
{
dq->front = -1;
dq->rear = -1;
}
else if (dq->rear == 0)
dq->rear = dq->size-1;
else
dq->rear = dq->rear-1;
}
int getFront(struct Deque * dq)
{
if (isEmpty(dq))
{
return -9999999 ;
}
return dq->arr[dq->front];
}
int getRear(struct Deque * dq)
{
if(isEmpty(dq) || dq->rear < 0)
{
return -9999999 ;
}
return dq->arr[dq->rear];
}
int printKMax(int arr[]){
struct Deque * dq=createDeque(k);
int i;
for(i=0;i<k;i++){
if(!isEmpty(dq)&&arr[i]>=arr[dq->rear]){
deletefront(dq);
}
insertrear(dq,i);
}
//printf("%d",arr[dq->front]);
for(;i<n;i++){
printf("%d\n",arr[dq->front]);
//sum+=arr[dq->front];
while (!isEmpty(dq) && dq->front <= i - k){
deletefront(dq); // Remove from front of queue
}
if(!isEmpty(dq)&&arr[i]>=arr[dq->rear]){
deletefront(dq);
}
insertrear(dq,i);
}
printf("%d\n",arr[dq->front]);
//sum+=arr[dq->front];
return sum;
}
int main(){
scanf("%d%d",&n,&k);
int array[n];
int i;
for(i=0;i<n;i++){
scanf("%d",&array[i]);
}
printf("%d",printKMax(array));
}