Skip to content

Commit 1561c92

Browse files
authored
Merge pull request #369 from Naman2251/main
Create day003sol02.cpp
2 parents 322997c + e52803f commit 1561c92

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Submission Link: https://codeforces.com/contest/1997/submission/355835686
2+
3+
4+
/*
5+
So first I made the RBS with minimmum cost by putting closing bracket as soon as we get a opening bracket before it, i.e., closing bracket as early as
6+
possible(greedy appraoch). After getting corect bracket sequence I put the indexes of '(' in vector o and indexes of ')' in vector c in order they occur. Both
7+
vector must contain n/2 elements and now for final answer I simply added the difference of each element in o and c at same index to ans to calculate cost
8+
which was initially zero and then printed the ans.
9+
*/
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
int main() {
14+
long long t;
15+
cin>>t;
16+
while(t--) {
17+
long long n;
18+
cin>>n;
19+
string s;
20+
cin>>s;
21+
long long a=0;
22+
for(long long i=0; i<n; i++) {
23+
if(s[i]=='(') a++;
24+
if(s[i]==')') a--;
25+
if(s[i]=='_') {
26+
if (a>0) {
27+
s[i]=')';
28+
a--;
29+
}
30+
else {
31+
s[i]='(';
32+
a++;
33+
}
34+
}
35+
}
36+
long long ans=0;
37+
vector <long long> o, c;
38+
for(long long i=0; i<n; i++) {
39+
if(s[i]=='(') o.push_back(i);
40+
else c.push_back(i);
41+
}
42+
for(long long i=0; i<n/2; i++) ans+=(c[i]-o[i]);
43+
cout<<ans<<endl;
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Submission Link: https://codeforces.com/contest/343/submission/355828800
2+
3+
/*
4+
Problem: Find the mininmum number of 1 ohm resistance needed to make equivalent resistance a/b
5+
6+
The Calkin-Wilf Tree
7+
This problem can be modeled using the Calkin-Wilf Tree, a binary tree that generates every positive rational number exactly once.
8+
Starting from 1/1, the two possible operations are:
9+
1. Series: a/b -> (a+b)/b (Moves to the Right Child)
10+
2. Parallel: a/b -> a/(a+b) (Moves to the Left Child)
11+
12+
Approach: If a resistance a/b can be obained with k resistors then it is clear that we can get resistance (a+b)/b(if 1 ohm in series) or
13+
a/(a+b)(if 1 ohm in parellel) with k+1 resistance. So we just go in reverse direction if a is greater than b then the last step must have been adding resistors
14+
in series so we add a/b to our answer and make a equal to a%b.. similarly if b is greater than a then The last step must have been adding resistors in parallel so
15+
we add b/a in answer and make b=b%a. This process continues untile either a or b becomes zero as either of a and b can become zero only when one is
16+
completely divisible by other and in this case we will get minimum number of resistor as the quotient.
17+
18+
Time Complexity: O(log(min(a, b))) as it is equivalent to the Euclidean GCD algorithm
19+
*/
20+
21+
#include <bits/stdc++.h>
22+
using namespace std;
23+
24+
int main() {
25+
long long a,b;
26+
cin>>a>>b;
27+
long long ans=0;
28+
while(a>0 && b>0) {
29+
if(a>b) {
30+
ans+=a/b;
31+
a=a%b;
32+
}
33+
else {
34+
ans+=b/a;
35+
b=b%a;
36+
}
37+
}
38+
cout<<ans<<endl;
39+
}

0 commit comments

Comments
 (0)