EWTD

Untitled

Dec 9th, 2019
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  3. #include <iostream>
  4. #include <cmath>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <map>
  8. #include <set>
  9. #include <bitset>
  10. #define MAX_INT 2147483647
  11. #define ll long long
  12.  
  13. using namespace std;
  14.  
  15. vector<int> coins, change, costs;
  16. set<int> S_coins, S_change;
  17.  
  18. template <size_t N>
  19. void increment ( std::bitset<N>& in ) {
  20. for ( size_t i = 0; i < N; ++i ) {
  21. if ( in[i] == 0 ) {
  22. in[i] = 1;
  23. break;
  24. }
  25. in[i] = 0;
  26. }
  27. }
  28.  
  29. void parse_digits(const string& str, int i,int j, vector<int>& vec){
  30. string value;
  31. for(;i < j; ++i){
  32. if(str[i] == ','){
  33. vec.push_back(stoi(value));
  34. value = "";
  35. continue;
  36. }
  37. value.push_back(str[i]);
  38. }
  39. vec.push_back(stoi(value));
  40. }
  41. void parse(const string& str){
  42. vector<int> left_bracket_idx, right_bracket_idx;
  43. for(int i = 0; i < str.size(); ++i){
  44. if(str[i] == '['){
  45. left_bracket_idx.push_back(i);
  46. }
  47. if(str[i] == ']'){
  48. right_bracket_idx.push_back(i);
  49. }
  50. }
  51. parse_digits(str,left_bracket_idx[0]+1,right_bracket_idx[0],coins);
  52. parse_digits(str,left_bracket_idx[1]+1,right_bracket_idx[1],change);
  53. parse_digits(str,left_bracket_idx[2]+1,right_bracket_idx[2],costs);
  54. }
  55. template<size_t N>
  56. long long can_afford(const bitset<N>& b){
  57. long long sum = 0;
  58. for(int i = 0; i < min(N,costs.size()); ++i){
  59. if(b[i] == 1){
  60. sum += costs[i];
  61. }
  62. }
  63. for( auto it = S_coins.lower_bound(sum); it != S_coins.end(); ++it){
  64. if(S_change.find(*it-sum) != S_change.end()){
  65. return sum;
  66. }
  67. }
  68. return -1;
  69. }
  70.  
  71. void generate_combinations(vector<int>& vec, set<int>& S){
  72. bitset<20> b(1);
  73. for(int i = 1; i < (1 << vec.size()); ++i){
  74. ll sum = 0;
  75. for(int j = 0; j < min(vec.size(),(size_t)20); ++j){
  76. if(b[j] == 1){
  77. sum += vec[j];
  78. }
  79. }
  80. S.insert(sum);
  81. increment(b);
  82. }
  83. }
  84. int main() {
  85. string input;
  86. getline(cin,input);
  87. parse(input);
  88. generate_combinations(change,S_change);
  89. generate_combinations(coins,S_coins);
  90. S_change.insert(0);
  91. long long min_sum = INT32_MAX;
  92. int amount = 0;
  93. bitset<20> b(1);
  94. for(int i = 1; i < (1 << costs.size()); ++i){
  95. auto ret = can_afford(b);
  96. if(ret != -1){
  97. int t = __builtin_popcountll(i);
  98. if(__builtin_popcountll(i) == amount && ret < min_sum){
  99. min_sum = ret;
  100. }
  101. if(__builtin_popcountll(i) > amount){
  102. amount = __builtin_popcountll(i);
  103. min_sum = ret;
  104. }
  105.  
  106. }
  107. increment(b);
  108. }
  109. cout << min_sum << '\n';
  110. return 0;
  111. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×