#include <iostream> #include <set> #include <algorithm> #include <vector> #include <list> #include <iterator> #include <map> #include <stack> std::set< std::vector<int> > oMapPralineSets; class CompareLess { public: bool operator() (int i, int j) { return (i < j); } } CompareLessObject; int suma( const std::vector<int>& oVector, const std::vector<int>& oPralineVector ) { int suma = 0; for(int i=0; i< oVector.size(); i++ ) suma += oPralineVector[oVector[i]]; return suma; } bool IsExistElementSet( const std::vector<int>& oVector ) { return oMapPralineSets.find(oVector) != oMapPralineSets.end(); } /* int suma() { int suma = 0; std::set< std::vector<int> >::iterator it = oVectorPralineSets.begin(); int n = it->size(); for (int i = 0; i < n; i++) suma += it->at(i); return suma; } */ bool InsertElementIntoVector( std::vector<int>& oVector, const int iNumberOfPralines ) { bool bIsInsertIntoVector = true; oVector.push_back( iNumberOfPralines ); return bIsInsertIntoVector; } void DeleteElementFromVector(std::vector<int>& oVector, const int iNumberOfPralines) { bool bIsDeleteFromVector = true; oVector.erase( oVector.begin() + iNumberOfPralines ); return; } int main() { int n = 5; int result = 0; int sumOfPralines = 0; std::vector<int> oVectorPralineCount{ 2,7,4,4,1 }; std::map<int,int> oDubledValuesVectorMap; std::map<int,int> oOccurencyValuesVectorMap; int k = 0; if( n >= 1 && n <= 5000 ) { std::sort( oVectorPralineCount.begin(), oVectorPralineCount.end(), CompareLessObject ); for( int i = 0; i < n; i++ ) { sumOfPralines += oVectorPralineCount[i]; oDubledValuesVectorMap[oVectorPralineCount[i]]++; } for( int i = 1; i <= sumOfPralines; i++ ) { std::vector<int> oSingleEntryVector; std::stack<int> oSingleEntryUsedStack; std::vector<int> oTempVectorPralineCount = oVectorPralineCount; std::vector<int>::iterator min_element; int iMinElement = 0; std::vector<int>::iterator iMinElementIndex; std::vector<int>::iterator iteratorTemp; oOccurencyValuesVectorMap.clear(); while( suma(oSingleEntryVector, oVectorPralineCount) < i ) { min_element = std::min_element(oTempVectorPralineCount.begin(), oTempVectorPralineCount.end()); if( min_element != oTempVectorPralineCount.end() ) { iMinElement = *min_element; iMinElementIndex = std::find(oVectorPralineCount.begin(), oVectorPralineCount.end(), iMinElement); iMinElement = std::distance(oVectorPralineCount.begin(), iMinElementIndex); oTempVectorPralineCount.erase(min_element); } if( std::find( oSingleEntryVector.begin(), oSingleEntryVector.end(), iMinElement ) == oSingleEntryVector.end() ) { InsertElementIntoVector(oSingleEntryVector, iMinElement); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } if (oDubledValuesVectorMap[oVectorPralineCount[iMinElement]] > 1 //&& oOccurencyValuesVectorMap[oVectorPralineCount[iMinElement]] > 1 //&& i - 1 == oVectorPralineCount[iMinElement] //&& i < oVectorPralineCount[iMinElement] && IsExistElementSet(oSingleEntryVector)) { if( oMapPralineSets.find(oSingleEntryVector) != oMapPralineSets.end() ) { DeleteElementFromVector(oSingleEntryVector, iMinElement); if (oVectorPralineCount[iMinElement] == oVectorPralineCount[iMinElement + 1]) { InsertElementIntoVector(oSingleEntryVector, iMinElement + 1); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } else if (oVectorPralineCount[iMinElement] == oVectorPralineCount[iMinElement - 1]) { InsertElementIntoVector(oSingleEntryVector, iMinElement - 1); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } } } //oOccurencyValuesVectorMap[oVectorPralineCount[ iMinElement ] ]++; } std::cout << "Iteracja " << i << " "; int isize = oSingleEntryVector.size(); for (int z = 0; z < isize; z++) { std::cout << oSingleEntryVector[z] << " "; } std::cout << std::endl; oMapPralineSets.insert( oSingleEntryVector ); } result = oMapPralineSets.size(); if( result >= 1000000007 ) { result %= 1000000007; } std::cout << result; } }
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #include <iostream> #include <set> #include <algorithm> #include <vector> #include <list> #include <iterator> #include <map> #include <stack> std::set< std::vector<int> > oMapPralineSets; class CompareLess { public: bool operator() (int i, int j) { return (i < j); } } CompareLessObject; int suma( const std::vector<int>& oVector, const std::vector<int>& oPralineVector ) { int suma = 0; for(int i=0; i< oVector.size(); i++ ) suma += oPralineVector[oVector[i]]; return suma; } bool IsExistElementSet( const std::vector<int>& oVector ) { return oMapPralineSets.find(oVector) != oMapPralineSets.end(); } /* int suma() { int suma = 0; std::set< std::vector<int> >::iterator it = oVectorPralineSets.begin(); int n = it->size(); for (int i = 0; i < n; i++) suma += it->at(i); return suma; } */ bool InsertElementIntoVector( std::vector<int>& oVector, const int iNumberOfPralines ) { bool bIsInsertIntoVector = true; oVector.push_back( iNumberOfPralines ); return bIsInsertIntoVector; } void DeleteElementFromVector(std::vector<int>& oVector, const int iNumberOfPralines) { bool bIsDeleteFromVector = true; oVector.erase( oVector.begin() + iNumberOfPralines ); return; } int main() { int n = 5; int result = 0; int sumOfPralines = 0; std::vector<int> oVectorPralineCount{ 2,7,4,4,1 }; std::map<int,int> oDubledValuesVectorMap; std::map<int,int> oOccurencyValuesVectorMap; int k = 0; if( n >= 1 && n <= 5000 ) { std::sort( oVectorPralineCount.begin(), oVectorPralineCount.end(), CompareLessObject ); for( int i = 0; i < n; i++ ) { sumOfPralines += oVectorPralineCount[i]; oDubledValuesVectorMap[oVectorPralineCount[i]]++; } for( int i = 1; i <= sumOfPralines; i++ ) { std::vector<int> oSingleEntryVector; std::stack<int> oSingleEntryUsedStack; std::vector<int> oTempVectorPralineCount = oVectorPralineCount; std::vector<int>::iterator min_element; int iMinElement = 0; std::vector<int>::iterator iMinElementIndex; std::vector<int>::iterator iteratorTemp; oOccurencyValuesVectorMap.clear(); while( suma(oSingleEntryVector, oVectorPralineCount) < i ) { min_element = std::min_element(oTempVectorPralineCount.begin(), oTempVectorPralineCount.end()); if( min_element != oTempVectorPralineCount.end() ) { iMinElement = *min_element; iMinElementIndex = std::find(oVectorPralineCount.begin(), oVectorPralineCount.end(), iMinElement); iMinElement = std::distance(oVectorPralineCount.begin(), iMinElementIndex); oTempVectorPralineCount.erase(min_element); } if( std::find( oSingleEntryVector.begin(), oSingleEntryVector.end(), iMinElement ) == oSingleEntryVector.end() ) { InsertElementIntoVector(oSingleEntryVector, iMinElement); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } if (oDubledValuesVectorMap[oVectorPralineCount[iMinElement]] > 1 //&& oOccurencyValuesVectorMap[oVectorPralineCount[iMinElement]] > 1 //&& i - 1 == oVectorPralineCount[iMinElement] //&& i < oVectorPralineCount[iMinElement] && IsExistElementSet(oSingleEntryVector)) { if( oMapPralineSets.find(oSingleEntryVector) != oMapPralineSets.end() ) { DeleteElementFromVector(oSingleEntryVector, iMinElement); if (oVectorPralineCount[iMinElement] == oVectorPralineCount[iMinElement + 1]) { InsertElementIntoVector(oSingleEntryVector, iMinElement + 1); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } else if (oVectorPralineCount[iMinElement] == oVectorPralineCount[iMinElement - 1]) { InsertElementIntoVector(oSingleEntryVector, iMinElement - 1); std::sort(oSingleEntryVector.begin(), oSingleEntryVector.end(), CompareLessObject); } } } //oOccurencyValuesVectorMap[oVectorPralineCount[ iMinElement ] ]++; } std::cout << "Iteracja " << i << " "; int isize = oSingleEntryVector.size(); for (int z = 0; z < isize; z++) { std::cout << oSingleEntryVector[z] << " "; } std::cout << std::endl; oMapPralineSets.insert( oSingleEntryVector ); } result = oMapPralineSets.size(); if( result >= 1000000007 ) { result %= 1000000007; } std::cout << result; } } |