#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a; cin >> n; vector<int> score(n, 0); for (int i = 0; i < n; ++i) { cin >> a; score[i] = a; } cout << 0 << " " << n << "\n"; //todo uncomment vector<int> selected(n); for (int k = 2; k <= n; k++) { for (int i = 0; i < k; i++) { selected[i] = i; } unsigned int minConflicts = 1000000; unsigned int combinations = 0; while (true) { // for (int i = 0; i < k; i++) { // cout << selected[i] << " "; // } // cout << endl; //TODO remove int conflicts = 0; for (int i = 0; i < k; i++) { for (int j = i + 1; j < k; j++) { if (score[selected[i]] > score[selected[j]]) { conflicts++; } } } if (conflicts < minConflicts) { minConflicts = conflicts; combinations = 1; } else if (conflicts == minConflicts) { combinations++; } int lastIdx = k - 1; int lastMax = n - 1; while (lastIdx >= 0 && selected[lastIdx] == lastMax) { lastIdx--; lastMax--; } if (lastIdx >= 0) { selected[lastIdx]++; lastIdx++; while (lastIdx < k) { selected[lastIdx] = selected[lastIdx - 1] + 1; lastIdx++; } } else { break; } } cout << minConflicts << " " << combinations << "\n"; } return 0; }
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 76 77 78 | #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a; cin >> n; vector<int> score(n, 0); for (int i = 0; i < n; ++i) { cin >> a; score[i] = a; } cout << 0 << " " << n << "\n"; //todo uncomment vector<int> selected(n); for (int k = 2; k <= n; k++) { for (int i = 0; i < k; i++) { selected[i] = i; } unsigned int minConflicts = 1000000; unsigned int combinations = 0; while (true) { // for (int i = 0; i < k; i++) { // cout << selected[i] << " "; // } // cout << endl; //TODO remove int conflicts = 0; for (int i = 0; i < k; i++) { for (int j = i + 1; j < k; j++) { if (score[selected[i]] > score[selected[j]]) { conflicts++; } } } if (conflicts < minConflicts) { minConflicts = conflicts; combinations = 1; } else if (conflicts == minConflicts) { combinations++; } int lastIdx = k - 1; int lastMax = n - 1; while (lastIdx >= 0 && selected[lastIdx] == lastMax) { lastIdx--; lastMax--; } if (lastIdx >= 0) { selected[lastIdx]++; lastIdx++; while (lastIdx < k) { selected[lastIdx] = selected[lastIdx - 1] + 1; lastIdx++; } } else { break; } } cout << minConflicts << " " << combinations << "\n"; } return 0; } |