Skip to content

Commit

Permalink
fasd;lfsa
Browse files Browse the repository at this point in the history
  • Loading branch information
KryptoCrash committed Feb 12, 2022
1 parent 249911b commit 5052b2b
Show file tree
Hide file tree
Showing 33 changed files with 2,160 additions and 0 deletions.
111 changes: 111 additions & 0 deletions 01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;

void bin(ll n) {
ll i;
cout << "0";
for (i = 1 << 30; i > 0; i = i / 2) {
if ((n & i) != 0) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}

void solve() {
ll a, b;
cin >> a >> b;

ll ans = 0;

queue<pi> q;
q.push({a, 0});
set<ll> vis;
vis.insert(a);
map<ll, ll> prev;
while (!q.empty()) {
ll v = q.front().first;
ll d = q.front().second;

q.pop();
if(v == b) {
ans = d;

break;

}

// add 1
if(vis.find(v + 1) == vis.end()) {
vis.insert(v + 1);
q.push({v + 1, d + 1});
prev[v + 1] = v;
if(b == v + 1) {
ans = d + 1;
break;
}
}
if(vis.find(v * 2) == vis.end() && v < b * 2) {
vis.insert(v * 2);
q.push({v * 2, d + 1});
prev[v * 2] = v;
if (b == v * 2) {
ans = d + 1;
break;
}
}
if((v % 2 == 0 ) && (vis.find(v >> 1) == vis.end())) {
vis.insert(v >> 1);
prev[v >> 1] = v;
q.push({v >> 1, d + 1});
if (b == v >> 1) {
ans = d + 1;
break;
}
}
}



ll cur = b;
vi path;
path.push_back(b);
while (prev.find(cur) != prev.end()) {
cout << cur << endl;
cur = prev[cur];
path.push_back(cur);
}

reverse(path.begin(), path.end());


cout << "ANS " << ans << endl;



cout << endl;

for(ll i : path) {
bin(i);
}
cout << endl;

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll n;
cin >> n;

while (n--) solve();

return 0;
}
58 changes: 58 additions & 0 deletions 01bad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;

void solve() {
ll a, b;
cin >> a >> b;

ll mn = 1e9;

for (ll i = 0; i < 30; i++) {
// switch
for (ll j = 0; j < 30; j++) {
ll v = a * (1 << j);

ll dif = b - v;
if (dif < 0) continue;
ll cnt = 0;

for (ll k = j; k >= 0; k--) {
for (ll w = 0;; w++) {
ll rem = (1 << k);
if (rem <= dif) {
cnt += 1;
dif -= rem;
continue;
}
break;
}
}

if (dif == 0) mn = min(mn, i + j + cnt);
}

if (a % 2 == 0)
a >>= 1;
else
a++;

}

cout << mn << endl;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll n;
cin >> n;

while (n--) solve();

return 0;
}
75 changes: 75 additions & 0 deletions 02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll n;
cin >> n;

vi a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
vi nxt_l(n, -1);
vi nxt_r(n, -1);

for (ll j = 0; j < 2; j++) {
stack<pi> s;
for (ll i = 0; i < n; i++) {
bool done = false;
while (!s.empty()) {
ll top = s.top().first;
if (top < a[i]) {
s.pop();
continue;
}
if (j == 0)
nxt_l[i] = s.top().second;
else
nxt_r[n - i - 1] = n - s.top().second - 1;
s.push({a[i], i});
done = true;
break;
}
if (!done) {
s.push({a[i], i});
}
}
while (!s.empty()) s.pop();
reverse(a.begin(), a.end());
}

ll ans = 0;
vi sum1(n);
vi cnt1(n);
vi sum2(n);
vi cnt2(n);

for (ll i = 0; i < n; i++) {
if (nxt_l[i] != -1) {
sum1[nxt_l[i]] += i;
cnt1[nxt_l[i]] += 1;
}
if (nxt_r[i] != -1) {
sum2[nxt_r[i]] += i;
cnt2[nxt_r[i]] += 1;
}
}

for (ll i = 0; i < n; i++) {
if (cnt1[i] != 0) ans += sum1[i] - (cnt1[i] * i) + cnt1[i];
}
for (ll i = 0; i < n; i++) {
if (cnt2[i] != 0) ans += (cnt2[i] * i) - sum2[i] + cnt2[i];
}

cout << ans << endl;

return 0;
}
76 changes: 76 additions & 0 deletions 03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll n, m;
cin >> n >> m;

vector<set<array<ll, 3>>> ch(m);
vpi p(n);

for (ll i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
a--, b--;
p[i] = {a, b};

ch[a].insert({b, i, 0});
ch[b].insert({a, i, 1});
}

vb vis(n);
vb done(m);
vi path;
ll ans = 0;

function<void(ll, bool)> dfs = [&](ll v, bool first) {
if (vis[v]) return;

vis[v] = true;
path.push_back(v + 1);

ll f = p[v].first;
ll s = p[v].second;

if(first && !done[f]) {
ans++;
done[f] = true;

for(auto i : ch[f]) {
if (i[1] == v) continue;
dfs(i[1], i[2]);
}
} else if(!done[s]) {
ans++;
done[s] = true;

for(auto i : ch[s]) {
if (i[1] == v) continue;
dfs(i[1], i[2]);
}
}

};

for (ll i = 0; i < n; i++) {
if (vis[i]) continue;
dfs(i, true);
}

cout << n - ans << endl;

for(ll i : path) {
cout << i << endl;
}

return 0;
}
51 changes: 51 additions & 0 deletions 767a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;

void solve() {
ll n, k;
cin >> n >> k;

vector<array<ll, 3>> a(n);
for(ll i = 0; i < n; i++) {
cin >> a[i][1];
a[i][2] = true;
}

for(ll i = 0; i < n; i++) {
cin >> a[i][0];
}

sort(a.begin(), a.end());
reverse(a.begin(), a.end());

while(true) {
bool works = false;
for (ll i = 0; i < n; i++) {
if(a[i][2] && (k >= a[i][1])) {
a[i][2] = false;
k += a[i][0];
works = true;
break;
}
}
if (!works) break;
}
cout << k << endl;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll t;
cin >> t;
while(t--) solve();

return 0;
}
Loading

0 comments on commit 5052b2b

Please sign in to comment.