#include<iostream> #include<vector> #include<list> #include<cassert> #include <algorithm> #include <memory> #include <tuple> #include <cmath> #include <map> #include <set> #include <unordered_map> #include <iostream> #include <string> #include <math.h> using namespace std; struct soldat { unsigned short a = 0; unsigned short b = 0; }; vector<unsigned long long> CONFLICTS; unsigned long long MIN_CONFLICTS = -1; bool isConflict(unsigned short a1, unsigned short b1, unsigned short a2, unsigned short b2) { return (a1 > a2 && b1 < b2) || (a1 < a2 && b1 > b2); } unsigned long long checkConflicts(vector<soldat>& arr) { unsigned long long res = 0; for (unsigned short i = 0; i < arr.size(); ++i) { for (unsigned short j = i; j < arr.size(); ++j) { if (isConflict(arr[i].a, arr[i].b, arr[j].a, arr[j].b)) { ++res; if (res > MIN_CONFLICTS) { return res; } } } } return res; } void combinationUtil(vector<soldat>& arr, vector<soldat>& data, int start, int end, int index, int r); void printCombination(vector<soldat>& arr, int n, const unsigned int k) { vector<soldat> data(k); combinationUtil(arr, data, 0, n - 1, 0, k); } void combinationUtil(vector<soldat> & arr, vector<soldat>& data, int start, int end, int index, int k) { if (index == k) { unsigned long long conflicts = checkConflicts(data); if (conflicts > MIN_CONFLICTS) { return; } else if (conflicts < MIN_CONFLICTS) { MIN_CONFLICTS = conflicts; } CONFLICTS[conflicts]++; return; } for (unsigned short i = start; i <= end && end - i + 1 >= k - index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i + 1, end, index + 1, k); } } int main() { unsigned short N; cin >> N; vector<soldat> NVec(N); for (unsigned short i = 0; i < N; ++i) { NVec[i].a = i; cin >> NVec[i].b; } CONFLICTS.resize(N*N); for (unsigned short k = 1; k <= N; ++k) { std::fill(CONFLICTS.begin(), CONFLICTS.end(), 0); MIN_CONFLICTS = -1; printCombination(NVec, N, k); for (unsigned short i = 0; i < CONFLICTS.size(); ++i) { if (CONFLICTS[i] != 0) { cout << i << " " << CONFLICTS[i] << endl; break; } } } 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include<iostream> #include<vector> #include<list> #include<cassert> #include <algorithm> #include <memory> #include <tuple> #include <cmath> #include <map> #include <set> #include <unordered_map> #include <iostream> #include <string> #include <math.h> using namespace std; struct soldat { unsigned short a = 0; unsigned short b = 0; }; vector<unsigned long long> CONFLICTS; unsigned long long MIN_CONFLICTS = -1; bool isConflict(unsigned short a1, unsigned short b1, unsigned short a2, unsigned short b2) { return (a1 > a2 && b1 < b2) || (a1 < a2 && b1 > b2); } unsigned long long checkConflicts(vector<soldat>& arr) { unsigned long long res = 0; for (unsigned short i = 0; i < arr.size(); ++i) { for (unsigned short j = i; j < arr.size(); ++j) { if (isConflict(arr[i].a, arr[i].b, arr[j].a, arr[j].b)) { ++res; if (res > MIN_CONFLICTS) { return res; } } } } return res; } void combinationUtil(vector<soldat>& arr, vector<soldat>& data, int start, int end, int index, int r); void printCombination(vector<soldat>& arr, int n, const unsigned int k) { vector<soldat> data(k); combinationUtil(arr, data, 0, n - 1, 0, k); } void combinationUtil(vector<soldat> & arr, vector<soldat>& data, int start, int end, int index, int k) { if (index == k) { unsigned long long conflicts = checkConflicts(data); if (conflicts > MIN_CONFLICTS) { return; } else if (conflicts < MIN_CONFLICTS) { MIN_CONFLICTS = conflicts; } CONFLICTS[conflicts]++; return; } for (unsigned short i = start; i <= end && end - i + 1 >= k - index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i + 1, end, index + 1, k); } } int main() { unsigned short N; cin >> N; vector<soldat> NVec(N); for (unsigned short i = 0; i < N; ++i) { NVec[i].a = i; cin >> NVec[i].b; } CONFLICTS.resize(N*N); for (unsigned short k = 1; k <= N; ++k) { std::fill(CONFLICTS.begin(), CONFLICTS.end(), 0); MIN_CONFLICTS = -1; printCombination(NVec, N, k); for (unsigned short i = 0; i < CONFLICTS.size(); ++i) { if (CONFLICTS[i] != 0) { cout << i << " " << CONFLICTS[i] << endl; break; } } } return 0; } |