Skip to content

Commit 8b8c15c

Browse files
authored
Merge pull request #324 from suzzzal/day3sol2
Day3sol2
2 parents 59b2162 + 8f0e89f commit 8b8c15c

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// submission link - https://codeforces.com/problemset/submission/1771/355587277
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*
7+
Approach:
8+
1. The maximum absolute difference between any two elements in the array
9+
is always (maximum element - minimum element).
10+
2. A pair (ai, aj) is interesting only if one of them is the minimum value
11+
and the other is the maximum value.
12+
3. Count how many times the minimum value appears (cntMin)
13+
and how many times the maximum value appears (cntMax).
14+
4. Since (ai, aj) and (aj, ai) are considered different pairs,
15+
the total number of interesting pairs is:
16+
2 * cntMin * cntMax
17+
5. Special case:
18+
- If all elements are equal (min == max), then every ordered pair
19+
(i, j) with i ≠ j is valid.
20+
- The answer becomes n * (n - 1).
21+
*/
22+
23+
int main(){
24+
25+
int t;
26+
cin >> t;
27+
while(t--){
28+
int n;
29+
cin >> n;
30+
31+
vector<long long> a(n);
32+
for(int i = 0; i < n; i++) cin >> a[i];
33+
34+
long long mn = *min_element(a.begin(), a.end());
35+
long long mx = *max_element(a.begin(), a.end());
36+
37+
if(mn == mx){
38+
cout << 1LL * n * (n - 1) << "\n";
39+
continue;
40+
}
41+
42+
long long cntMin = 0, cntMax = 0;
43+
for(long long x : a){
44+
if(x == mn) cntMin++;
45+
if(x == mx) cntMax++;
46+
}
47+
48+
cout << 2LL * cntMin * cntMax << "\n";
49+
}
50+
return 0;
51+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// submission link - https://codeforces.com/contest/343/submission/355607613
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*
7+
1. We want to make fraction a/b using smallest number of 1 resistors.
8+
2. We can add resistor in line (series) or side (parallel).
9+
3. Instead of making from 1, we go backward from a/b.
10+
4. If a is bigger than b, then we minus b from a.
11+
5. If b is bigger than a, then we minus a from b.
12+
6. Every minus means we used one resistor.
13+
7. We count how many times we can minus using divide.
14+
8. This work same like Euclid algo (gcd thing).
15+
9. When one number become zero, we stop.
16+
10. Total count is our answer.
17+
*/
18+
19+
int main()
20+
{
21+
unsigned long long a, b;
22+
cin >> a >> b;
23+
24+
long long totalResistors = 0;
25+
26+
while (a && b)
27+
{
28+
totalResistors += a / b;
29+
a %= b;
30+
swap(a, b);
31+
}
32+
33+
cout << totalResistors;
34+
return 0;
35+
}

0 commit comments

Comments
 (0)