Skip to content

Commit 3208984

Browse files
Add files via upload
1 parent 2e47654 commit 3208984

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
If we meet a (
2+
3+
Then () is a regular balanced string
4+
5+
And (( is a palindrome
6+
7+
No matter what, (x is always a good string and we will extract it
8+
9+
-----
10+
11+
If we meet a )
12+
13+
Then, we cannot get a balanced string since it starts with )
14+
15+
We can only get a palindrome
16+
17+
All we need to do is find the next closed bracket !
18+
19+
) ((((( ... ( )
20+
21+
is a palindrome !
22+
23+
-----
24+
25+
void solve()
26+
{
27+
int length;
28+
string S;
29+
cin >> length >> S;
30+
31+
int minimum_moves = 0, remaining = length;
32+
for(int i = 0; i + 1 < length; i++)
33+
{
34+
if(S[i] == '(')
35+
{
36+
minimum_moves++;
37+
remaining -= 2;
38+
i++;
39+
}
40+
else if(S[i] == ')')
41+
{
42+
int j = i + 1;
43+
while(j < length && S[j] != ')')
44+
{
45+
j++;
46+
}
47+
48+
if(j == length)
49+
{
50+
break;
51+
}
52+
53+
minimum_moves++;
54+
remaining -= (j - i + 1); //cout << "i = " << i << " j = " << j << "\n";
55+
i = j;
56+
}
57+
}
58+
59+
cout << minimum_moves << " " << remaining << "\n";
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
We can always reach (x, y) in at most 2 moves
2+
3+
If we are at the origin, then we need 0 moves
4+
If (x^2 + y^2) is a perfect square, then we need only 1 move
5+
Otherwise, we will go from (0, 0) -> (x, 0) -> (y, 0) in 2 moves !
6+
7+
We will always need at most 2 moves !
8+
9+
------
10+
11+
int is_square(int n)
12+
{
13+
for(int i = 1; i*i <= n; i++)
14+
{
15+
if(i*i == n)
16+
{
17+
return true;
18+
}
19+
}
20+
21+
return false;
22+
}
23+
24+
void solve()
25+
{
26+
int x, y;
27+
cin >> x >> y;
28+
29+
int answer = 0;
30+
if(x == 0 && y == 0)
31+
{
32+
answer = 0;
33+
}
34+
else if(is_square(x*x + y*y))
35+
{
36+
answer = 1;
37+
}
38+
else
39+
{
40+
answer = 2;
41+
}
42+
43+
cout << answer << "\n";
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Be greedy and Set A[i] = A[i] + X, unless it exceeds the limit
2+
3+
------
4+
5+
void solve()
6+
{
7+
int no_of_elements, x, y, max_limit;
8+
cin >> no_of_elements >> max_limit >> x >> y;
9+
10+
long long sum = 0;
11+
vector <long long> A(no_of_elements + 1);
12+
for(int i = 1; i <= no_of_elements; i++)
13+
{
14+
if(A[i - 1] + x <= max_limit)
15+
{
16+
A[i] = A[i - 1] + x;
17+
}
18+
else
19+
{
20+
A[i] = A[i - 1] - y;
21+
}
22+
23+
sum += A[i];
24+
}
25+
26+
cout << sum << "\n";
27+
}

0 commit comments

Comments
 (0)