-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem61.cpp
More file actions
75 lines (62 loc) · 1.65 KB
/
problem61.cpp
File metadata and controls
75 lines (62 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class CardInput {
public:
int n;
vector<long long> cards;
void readInput() {
cout << "Enter number of cards: ";
cin >> n;
cards.resize(n);
cout << "Enter card numbers: ";
for (int i = 0; i < n; i++)
cin >> cards[i];
}
bool isValidRange() {
return (n >= 2 && n <= 1e5);
}
bool isValidCards() {
for (long long x : cards)
if (x < 1 || x > 1e9) return false;
return true;
}
};
class BeautifulPairsCounter {
public:
static long long countBeautifulPairs(const vector<long long>& cards) {
set<long long> unique(cards.begin(), cards.end());
vector<long long> nums(unique.begin(), unique.end());
long long count = 0;
int sz = nums.size();
// For each possible (first, last) pair:
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
if (nums[i] > nums[j]) count++;
}
}
return count;
}
};
class ResultPrinter {
public:
static void print(long long count) {
cout << "Total beautiful arrangements: " << count << endl;
}
};
int main() {
CardInput input;
input.readInput();
if (!input.isValidRange()) {
cout << "Number of cards must be in range [2, 1e5]" << endl;
return 1;
}
if (!input.isValidCards()) {
cout << "Card numbers must be in range [1, 1e9]" << endl;
return 1;
}
long long result = BeautifulPairsCounter::countBeautifulPairs(input.cards);
ResultPrinter::print(result);
return 0;
}