Skip to content
This repository was archived by the owner on Oct 24, 2020. It is now read-only.

Commit 4f25c13

Browse files
authored
Merge pull request kothariji#123 from Dipeshtwis/master
Add Greedy codechef solution
2 parents b0dae74 + 6c0a622 commit 4f25c13

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Greedy/(CODECHEF)DRCHEF

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Dipesh Kumar */
2+
3+
#include <bits/stdc++.h>
4+
typedef long long int ll;
5+
using namespace std;
6+
7+
int main() {
8+
int t;
9+
cin >> t;
10+
while(t--)
11+
{
12+
ll n, x;
13+
cin >> n >> x;
14+
vector<ll> a(n, 0);
15+
for(ll i = 0; i < n; i++)
16+
cin >> a[i];
17+
18+
ll d = 0;
19+
sort(a.begin(), a.end());
20+
vector<ll>::iterator it = lower_bound(a.begin(), a.end(), x);
21+
ll lb = it - a.begin();
22+
23+
for(ll i = lb;i < n; i++)
24+
{
25+
if(x < a[i])
26+
{
27+
while(x < a[i])
28+
{
29+
x *= 2;
30+
d++;
31+
}
32+
d++;
33+
}
34+
else
35+
d++;
36+
37+
x = (a[i] * 2);
38+
}
39+
ll t = lb + d;
40+
if(lb != 0)
41+
{
42+
d = 0;
43+
lb--;
44+
for(ll i = lb;i < n; i++)
45+
{
46+
if(x < a[i])
47+
{
48+
while(x < a[i])
49+
{
50+
x *= 2;
51+
d++;
52+
}
53+
d++;
54+
}
55+
else
56+
d++;
57+
58+
x = (a[i] * 2);
59+
}
60+
if(d + lb < t)
61+
cout << d + lb;
62+
else
63+
cout << t;
64+
}
65+
else
66+
cout << lb + d;
67+
68+
cout<< endl;
69+
}
70+
return 0;
71+
}

0 commit comments

Comments
 (0)