Approach
Sorting and greedy selection
For ABC175 B — Making Triangle, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 47 lines of C++ from the credited upstream file abc175_b.cpp.
- The implementation visibly relies on sequence storage.
- 4 loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <algorithm>2#include <iostream>3#include <vector>4 5using namespace std;6 7int main() {8 unsigned int n;9 cin >> n;10 11 vector<long> L(n, 0);12 for (auto& l : L) {13 cin >> l;14 }15 16 unsigned int ans = 0;17 vector<vector<vector<bool>>> checked(18 n, vector<vector<bool>>(n, vector<bool>(n, false)));19 for (unsigned int i = 0; i < n; i++) {20 for (unsigned int j = 0; j < n; j++) {21 for (unsigned int k = 0; k < n; k++) {22 if (L[i] == L[j] || L[j] == L[k] || L[k] == L[i]) {23 continue;24 }25 26 vector<unsigned int> c = {i, j, k};27 sort(c.begin(), c.end());28 if (i == k || j == k || checked[c[0]][c[1]][c[2]]) {29 continue;30 }31 checked[c[0]][c[1]][c[2]] = true;32 33 if ((L[c[0]] > L[c[1]] && L[c[0]] > L[c[2]] &&34 L[c[0]] < L[c[1]] + L[c[2]]) ||35 (L[c[1]] > L[c[2]] && L[c[1]] > L[c[0]] &&36 L[c[1]] < L[c[2]] + L[c[0]]) ||37 (L[c[2]] > L[c[0]] && L[c[2]] > L[c[1]] &&38 L[c[2]] < L[c[0]] + L[c[1]])) {39 ans++;40 }41 }42 }43 }44 45 cout << ans << endl;46 return 0;47}