-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
224 lines (189 loc) · 4.36 KB
/
functions.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
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
// This is just a template solution used as initial script
#include <bits/stdc++.h>
#include <stdlib.h> /* abs */
#include <iostream>
#include <vector>
#include <numeric>
#include <unordered_map>
#include <map>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using pi = pair<int,int>;
#define INF std::numeric_limits<int>::max()
#define LLINF std::numeric_limits<long long>::max()
#define F(i, n) for (ll i = 0; i < n; ++i)
#define FF(i, start, n) for (ll i = start; i < n; ++i)
#define VPRINT(name, v) \
cout << name << " : "; \
for (auto& e : v) { \
cout << e << " "; \
} \
cout << std::endl;
#define VVPRINT(name, v) \
cout << name << " : " << endl; \
for (auto& e : v) { \
for (auto& k : e) { \
cout << k << " "; \
} \
cout << endl; \
} \
cout << std::endl;
template <typename T, typename A>
int arg_max(std::vector<T, A> const& vec) {
return static_cast<int>(std::distance(vec.begin(), max_element(vec.begin(), vec.end())));
}
template <typename T, typename A>
int arg_min(std::vector<T, A> const& vec) {
return static_cast<int>(std::distance(vec.begin(), min_element(vec.begin(), vec.end())));
}
vector<int> binary2(ll x)
{
vector<int> res;
while (x > 0)
{
res.push_back(x % 2);
x >>= 1;
}
return res;
}
void primeFactors(vector<int>& v, int n)
{
while (n % 2 == 0) {v.push_back(2);n = n/2;}
for (int i = 3; i <= sqrt(n); i = i + 2) {while (n % i == 0){ v.push_back(i); n = n/i;}}
if (n > 2) v.push_back(n);
}
int max_subarray_sum(const vector<int>& v)
{
int res = 0;
for(int i = 0; i < v.size(); i++)
{
int sum = 0;
for(int j = i; j < v.size(); j++)
{
sum += v[j];
res = max(res, sum);
}
}
return res;
}
void search_subset(int k, int n, vector<int>& subset, vector<vector<int>>& res)
{
if(k == n+1)
{
res.push_back(subset);
}
else
{
subset.push_back(k);
search_subset(k+1, n, subset, res);
subset.pop_back();
search_subset(k+1, n, subset, res);
}
}
int dfs(const vector<vector<int>>& g, vector<int>& dist, vector<int>& remove,
int curr, int curr_dist)
{
int tmp = 0;
if(dist[curr] == INF)
{
dist[curr] = curr_dist;
for(auto& a : g[curr])
{
if(dist[a] == INF)
tmp += dfs(g, dist, remove, a, curr_dist+1);
}
}
remove[curr] = tmp;
return tmp+1;
}
int bfs(const vector<vector<int>>& g, vector<int>& dist, int start, int end,
vector<int>& parent,
vector<vector<int>>& children)
{
// g is a list of neighbours for each node
// dist is assumed to be filled with INF
queue<int> q;
q.push(start);
queue<int> d;
d.push(0);
while (q.size() > 0)
{
auto x = q.front();
auto dd = d.front();
q.pop();
d.pop();
if (dist[x] == INF)
{
dist[x] = dd;
if (end == x)
break;
for (auto e: g[x]) {
if(dist[e] == INF)
{
children[x].push_back(e);
q.push(e);
parent[e] = x;
d.push(dd+1);
}
}
}
}
if (end >= 0 && end < dist.size())
return dist[end];
else
return -1;
}
int max_subarray_sum(const vector<int>& v)
{
int res = 0;
for(int i = 0; i < v.size(); i++)
{
int sum = 0;
for(int j = i; j < v.size(); j++)
{
sum += v[j];
res = max(res, sum);
}
}
return res;
}
void search_subset(int k, int n, vector<int>& subset, vector<vector<int>>& res)
{
if(k == n+1)
{
res.push_back(subset);
}
else
{
subset.push_back(k);
search_subset(k+1, n, subset, res);
subset.pop_back();
search_subset(k+1, n, subset, res);
}
}
// vector<pair<int, int>> v;
// sort(v.begin(), v.end(), [](auto const& a, auto const& b) {
// return a.second > b.second;
// });
struct mystruct
{
int somme;
int freq;
};
bool comp(const mystruct &a, const mystruct &b)
{
return a.freq > b.freq;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll tt;
cin >> tt;
F(tti, tt)
{
}
return 0;
}