-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfinalsover.cpp
More file actions
314 lines (254 loc) · 5.57 KB
/
finalsover.cpp
File metadata and controls
314 lines (254 loc) · 5.57 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// FINAL-LEFTMOST SOLVER PART-1 !!!
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
int a[100005];
int n;
int b[100005];
vector<int> query;
pair<int,int> start_end[1000];
int q,flag=1;
void initial()
{
for(int i=0;i<n;i++){
cin>>a[i];
}
}
void simplesol(int l1, int r1, int q1)
{
vector<int> filled;
vector<int> empty;
for(int i=l1;i<r1;i++){
if(a[i]==4)empty.pb(i); // position of empty blocks
if(a[i]==1)filled.pb(i); // position of filled blocks
}
int k=l1; // index of array
int l=q1; // index of the clues
int total=query.size();
while(k<r1&&l<total){ // while we reach the end of array of the or all the clues are used
int x=query[l];
int check=1;
//cout<<k<<endl;
for(int i=k;i<k+x;i++){
if(a[i]==1||b[i]==3){
check=0;
break;
}
}
if(k+x<r1){
if(a[k+x]==4) // if the element just after the block is filled we can't place the block
check=0;
}
if(k>l1){
if(a[k-1]==4)check=0; // if the element just before the bolck is filled we can't place the block
}
if(check==1){
//cout<<k<<endl;
for(int i=k;i<k+x;i++){
//if(b[i]!=4)
b[i]=3; // it is the left most solution
}
k+=x; // move to next of next cell
}
k++;
if(check==1) // if check==1 we move to the next clue;
l++;
}
}
void inputquery()
{
for(int i=0;i<q;i++){
int x;
cin>>x;
query.pb(x);
}
}
vector<int> assign(int l1,int r1)
{
vector<int> assigned;
for(int i=l1;i<r1;i++){
if(b[i]!=3&&a[i]==4){
assigned.pb(i);
}
}
return assigned;
}
void startend()
{
int idx=0;
int i=0;
while(i<n){
if(b[i]==3){
start_end[idx].fi=i;
i++;
while(b[i]==3){
i++;
}
i--;
start_end[idx].se=i;
//cout<<start_end[idx].fi<<" "<<start_end[idx].se<<endl;
idx++;
}
i++;
}
}
void final_output()
{
for(int i=0;i<n;i++){
cout<<b[i]<<" ";
}
}
int getblock(int pos,int j)
{
for(int i=j;i>=0;i--)
{
if(start_end[i].second<pos)
return i;
}
return -1;
}
void reposition(int x, int s)
{
}
int firstpart(int pos,int x,int rep)
{
int l,r;
l=start_end[x].fi;
r=start_end[x].se;
int tempx=l;
int tempy=r;
//cout<<tempx<<" "<<tempy<<endl;
int done=0;
// bolck just to the right of unassigned solid
l+=pos-r;
r=pos;
// while the block overlaps the solid
while(l<=pos&&r>=pos){
int chk=1;
// not a valid position if empty cell lies
for(int i=l;i<r;i++){
if(a[i]==1){
chk=0;
break;
}
}
// not a valid position if another block is adjacent
if(a[l-1]==3||a[l-1]==4||a[r+1]==3||a[r+1]==4){
chk=0;
}
// if invalid position then move the block
if(chk==0){
l++;
r++;
continue;
}
// if valid position the mark all the unassigned solids in this range as solid
if(chk==1)
{
done=1;
// removing the old block
for(int i=tempx;i<=tempy;i++)b[i]=0;
// removing the earlier assigned blocks
for(int i=tempx;i<=tempy;i++){
if(a[i]==4)assigned.pb(i);
}
// for(int i=tempx;i<=tempy;i++){
// if(a[i]==4)assigned.pb();
//}
// adding the new block
for(int i=l;i<=r;i++)
b[i]=3;
if(r+1<n)
b[r+1]=1;
if(rep==1)
reposition(x,r+1);
break;
}
}
return done;
}
void checker()
{
int y=query.size()-1;
while(assigned.size()!=0)
{
int pos=assigned[assigned.size()-1];
//cout<< pos<<endl;
assigned.pop_back();
//assigned.pop();
// block is already assigned
if(b[pos]==3){
continue;
}
// rightmost unassigned solid
int y=getblock(pos,y);
if(y==-1)
{
flag=0;
return;
}
int x=firstpart(pos,y,0);
if(x==1)
{
y--;
break;
}
else
{
int chk=0;
while(y>0&&x!=1)
{
y--;
x=firstpart(pos,y,1);
if(x==1)
{
chk=1;
break;
}
}
if(chk)
continue;
else
{
flag=0;
break;
}
}
}
}
void printsimplesol()
{
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
}
// 0 - dont know
// 1 - empty
// 3 - leftmost solution
// 4 - filled
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt","r",stdin);
// run SIMPLE-LEFTMOST SOLVER
//vector<int> arr=sipleLeftSolver();
cin>>n;
cin>>q;
inputquery(); // l se r ki zaroorat shayad na ho
initial(); // l se r ki zaroorat shayad na ho
simplesol(l1,r1,q1); // starting from array index l1 upto r1 and q1 denotes the query from which it will start running
vector<int> assign_block;
assign_block = assign(l1,r1); // to get the assigned blocks in the range l1 to r1
printsimplesol();
startend();
checker();
final_output();
return 0;
}