-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmbitious Kid
376 lines (358 loc) · 9.12 KB
/
Ambitious Kid
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*/ Author: Sumit8707 /*/
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define endl "\n"
#define nl endl
#define ff first
#define ss second
#define max3(a, b, c) max(max((a), (b)), (c))
#define max4(a, b, c, d) max(max((a), (b)), max((c), (d)))
#define min3(a, b, c) min(min((a), (b)), (c))
#define min4(a, b, c, d) min(min((a), (b)), min((c), (d)))
#define sz(x) ((int)(x).size())
#define pb push_back
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
/*--------------------------------------------------------------------------------------------------------------*/
// policy based data structure
#define ook order_of_key // Number of elements STRICTLY smaller than X
#define fbo find_by_order // *ITERATOR* pointing to the kth element (0 order)
template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using moset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
/*---------------------------------------------------------------------------------------------------------------*/
// void kick_start(int t) {cout << "Case #" << t << ": ";}
template <class T>
istream &operator>>(istream &stream, vector<T> &v)
{
for (T &vec : v)
{
stream >> vec;
}
return stream;
}
template <class T>
ostream &operator<<(ostream &stream, vector<T> &v)
{
for (T &vec : v)
{
stream << vec << ' ';
}
return stream;
}
// gcd
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// gcd of array : Time Complexity: O(N * log(N)), where N is the largest element of the array and space-O(1)
int gcd_of_array(vector<int> &arr)
{
int gc = 0;
for (int i = 0; i < arr.size(); i++)
{
gc = gcd(gc, arr[i]);
}
return gc;
}
// lcm
int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// lcm of array : Time Complexity: O(n log n), where n is the length of the input array and space-O(1)
int lcm_of_array(vector<int> &arr)
{
int lc = arr[0];
for (int i = 1; i < arr.size(); i++)
{
int num1 = lc;
int num2 = arr[i];
int val = gcd(num1, num2);
lc = (lc * arr[i]) / val;
}
return lc;
}
// binary exponentiation calculating a^b
int binpow(int a, int b)
{
int res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
// calculating a^b mod(m)
int modbinpow(int a, int b, int m)
{
a %= m;
int res = 1;
while (b > 0)
{
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
// first occurance of an element in a sorted array using binary search //O(logn)time e search korbe
int fo(vector<int> v, int n, int x)
{
// n = size of array and x = target element;
int index = -1;
int l = 0;
int r = n - 1;
while (l <= r)
{
int mid = l + (r - l) / 2;
if (v[mid] == x)
{
index = mid;
r = mid - 1;
}
else if (v[mid] < mid)
{
l = mid + 1;
}
else
r = mid - 1;
}
return index;
}
// last occurance of an element in a sorted array using binary search //O(logn)time e search korbe
int lo(vector<int> v, int n, int x)
{
// n = size of array and x = target element;
int index = -1;
int l = 0;
int r = n - 1;
while (l <= r)
{
int mid = l + (r - l) / 2;
if (v[mid] == x)
{
index = mid;
l = mid + 1;
}
else if (v[mid] < mid)
{
l = mid + 1;
}
else
r = mid - 1;
}
return index;
}
int mex(vector<int>v,int n){ //return the first missing positive integer TC=O(n) SC=O(1)
for (int i = 0; i < n; i++){while(v[i]>=0 and v[i]<n and v[i]!=v[v[i]]){swap(v[i],v[v[i]]);}}
for (int i = 0; i < n; i++){if(v[i]!=i)return i;}return n;
}
// int nCr(int n, int r)
// {
// return fact(n) / (fact(r) * fact(n - r));
// }
// Returns factorial of n
int fact(int n)
{
if (n == 0)
return 1;
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
bool sortbyCond(const pair<int, int> &a,
const pair<int, int> &b)
{
if (a.first != b.first)
return (a.first < b.first);
else
return (a.second > b.second);
}
void printBoard(int n,int board[][20]){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout<<board[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
bool canPlace(int board[][20], int n, int x, int y){
/* x = current row and y = current column where we want to put our queen */
//column
for (int i = 0; i < x ; i++){
if (board[i][y]==1){
return false;
}
}
//left diagonal
int i = x;
int j = y;//i and j te x and y copy kore nilam
while(i>=0 && j>=0){
if (board[i][j]==1){
return false;
}
i--;j--;
}
//right diagonal
i = x;
j = y;
while(i>=0 && j<n){
if (board[i][j]==1){
return false;
}
i--; j++;
}
return true;
}
//for returning all possible ways to fill the board
int solveNQueen(int n, int board[][20],int i){
//base case
if(i==n){
// if i reaches n means we can finish the board in one ways
printBoard(n,board);
return 1;
}
//rec case
//try to place a queen in every row
int ways = 0;
for(int j = 0; j<n; j++){
//here j represents the column;
if (canPlace(board,n,i,j)){
board[i][j]=1;
ways += solveNQueen(n,board,i+1);
//backtrack
board[i][j] = 0;
}
}
return ways;
}
// bool solveNQueen(int n, int board[][20],int i){
// //base case
// if(i==n){
// //print the board
// printBoard(n,board);
// return true;
// }
// //rec case
// //try to place a queen in every row
// for(int j = 0; j<n; j++){
// //here j represents the column;
// if (canPlace(board,n,i,j)){
// board[i][j]=1;
// bool success = solveNQueen(n,board,i+1);
// //if success is true that means the remaining part(lower)of board
// //can be filled with queens else we will move our current rows queen to next column(j+1)
// //if somehow j becomes n that means we can't put a queen in this row
// //so we return false to parent call(hey listen please change your queens position)
// if(success){
// return true;
// }
// //backtrack
// board[i][j] = 0;
// }f
// }
// return false;
// }
// unordered_map<int,vector<int>>m;
// m[v[i]].pb(i);
/*------------------------------------------------------------------------------------------------------------*/
void wrong_submission()
{
int n;cin>>n;
vector<int>v(n);
for (int i = 0; i < n; i++){
cin>>v[i];
v[i]=abs(v[i]);
}
cout<<*min_element(all(v));
}
/*------------------------------------------------------------------------------------------------------------*/
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
auto start = std::chrono::high_resolution_clock::now();
cout << setprecision(10);
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; i++)
{
// kick_start(i);
wrong_submission();
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
#ifndef ONLINE_JUDGE
cerr << "Time taken : " << ((long double)duration.count()) / ((long double)1e9) << "s " << endl;
#endif
return 0;
}
/*
int n,s; cin>>n>>s;
int temp = n;
vector<int>v;
while(n>0){
v.pb(n%10);
n/=10;
}
int sum = accumulate(all(v),0ll);
if(sum<=s){
cout<<0<<endl;
return;
}
vector<int>ans;
// for(auto &it:v)cout<<it;
// cout<<endl;
for (int i = 0; i < v.size(); i++)
{
sum = sum-v[i];
// sum++;
ans.pb(0);
v[i]=0;
// cout<<sum;
if(sum<=s)break;
}
if(ans.size()==v.size()){
v.pb(1);
int a = v.size();
// cout<<a<<endl;
int b = binpow(10,a-1);
cout<<b-temp<<endl;
return;
}
for (int i = 0; i < v.size(); i++)
{
if(v[i]!=0)v[i]++;
break;
}
reverse(all(v));
for(auto &it:v)cout<<it;
cout<<endl;
// cout<<endl;
*/