Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <iostream> #include <vector> #include <map> #include <cmath> //#ifdef _DEBUG //#include <string> //#include <algorithm> //#include <time.h> //#endif using namespace std; typedef long long LL; typedef vector<LL> vLL; typedef map<LL, LL> mLLLL; bool try_insert(mLLLL &map_, LL b, LL sum) { mLLLL::iterator it = map_.upper_bound(b); if(it==map_.begin() || (it != map_.end() && it->second <= sum)) { while(it != map_.end() && it->second <= sum) { mLLLL::iterator it2 = it; it++; map_.erase(it2); } map_[b] = sum; return true; } else { it--; if(sum > it->second) { map_[b] = sum; return true; } else { return false; } } } //#ifdef _DEBUG //string bin(LL n) { // string res; // do { // res.push_back('0' + (int)(n&1LL)); // n>>=1; // } while(n>0); // reverse(res.begin(), res.end()); // return res; //} //#endif LL next_with_bits(LL val_to_inc, int bits_required, int level = 0) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "starting next_with_bits " << bin(val_to_inc) << " " << bits_required; //#endif if(bits_required == 1) { LL res = 1LL; while(res <= val_to_inc) res <<= 1; //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } LL res = val_to_inc + 1LL; int bits_have = 0; for(LL t = 1LL; t <= res; t<<=1) if(res & t) bits_have++; if(bits_have==bits_required) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } else if(bits_have < bits_required) { for(LL t = 1LL; true; t<<=1) { if((res & t) == 0LL) { res |= t; bits_have++; if(bits_have == bits_required) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } } } } else { // bits_have > bits_required LL t = 1LL; int delta_bits = bits_have - bits_required; while(delta_bits > 0) { if(res & t) { res ^= t; // 1 -> 0 delta_bits--; } t <<= 1; } while((res&t)==0) { t <<= 1; } // delta_bits == 0 return next_with_bits(res + t - 1LL, bits_required, level+1); // exactly once, no deep recursion; required, because this "+t" can clear too many "1"-s } } int main(int argc, char* argv[]) { //// #ifdef _DEBUG // freopen("input.txt", "rt", stdin); // freopen("output.txt", "wt", stdout); //// #endif int n; long long m; cin >> n >> m; int max_bits = log(m + 1.25)/log(2.0); vLL data(n+1); for(int i=0; i<n; i++) cin >> data[i]; vector<mLLLL> opt(n+1); opt[0][0LL] = 0LL; opt[1][0LL] = 0LL; // ��������� �������� b-��� (� ���� ����) ���� ���� ����� for(int i=0; i<n; i++) { //#ifdef _DEBUG // cerr << "started i = " << i << " at " << (clock()*1.0)/CLOCKS_PER_SEC; //#else if(i>1) opt[i-1].clear(); //#endif for(mLLLL::iterator it = opt[i].begin(); it != opt[i].end(); it++) { for(int bits_num = 1; bits_num <= max_bits; bits_num++) { LL b = next_with_bits(it->first, bits_num); if(b <= m - n + i + 1) { try_insert(opt[i+1], b, it->second + data[i] * bits_num); } } } //#ifdef _DEBUG // cerr << "; sz = " << opt[i+1].size() << endl; //#endif } cout << opt.back().rbegin()->second << endl; 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 133 134 135 136 137 138 139 140 141 142 143 | #include <iostream> #include <vector> #include <map> #include <cmath> //#ifdef _DEBUG //#include <string> //#include <algorithm> //#include <time.h> //#endif using namespace std; typedef long long LL; typedef vector<LL> vLL; typedef map<LL, LL> mLLLL; bool try_insert(mLLLL &map_, LL b, LL sum) { mLLLL::iterator it = map_.upper_bound(b); if(it==map_.begin() || (it != map_.end() && it->second <= sum)) { while(it != map_.end() && it->second <= sum) { mLLLL::iterator it2 = it; it++; map_.erase(it2); } map_[b] = sum; return true; } else { it--; if(sum > it->second) { map_[b] = sum; return true; } else { return false; } } } //#ifdef _DEBUG //string bin(LL n) { // string res; // do { // res.push_back('0' + (int)(n&1LL)); // n>>=1; // } while(n>0); // reverse(res.begin(), res.end()); // return res; //} //#endif LL next_with_bits(LL val_to_inc, int bits_required, int level = 0) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "starting next_with_bits " << bin(val_to_inc) << " " << bits_required; //#endif if(bits_required == 1) { LL res = 1LL; while(res <= val_to_inc) res <<= 1; //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } LL res = val_to_inc + 1LL; int bits_have = 0; for(LL t = 1LL; t <= res; t<<=1) if(res & t) bits_have++; if(bits_have==bits_required) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } else if(bits_have < bits_required) { for(LL t = 1LL; true; t<<=1) { if((res & t) == 0LL) { res |= t; bits_have++; if(bits_have == bits_required) { //#ifdef _DEBUG_EXTRA_OUT // cerr << "; res : " << bin(res) << endl; //#endif return res; } } } } else { // bits_have > bits_required LL t = 1LL; int delta_bits = bits_have - bits_required; while(delta_bits > 0) { if(res & t) { res ^= t; // 1 -> 0 delta_bits--; } t <<= 1; } while((res&t)==0) { t <<= 1; } // delta_bits == 0 return next_with_bits(res + t - 1LL, bits_required, level+1); // exactly once, no deep recursion; required, because this "+t" can clear too many "1"-s } } int main(int argc, char* argv[]) { //// #ifdef _DEBUG // freopen("input.txt", "rt", stdin); // freopen("output.txt", "wt", stdout); //// #endif int n; long long m; cin >> n >> m; int max_bits = log(m + 1.25)/log(2.0); vLL data(n+1); for(int i=0; i<n; i++) cin >> data[i]; vector<mLLLL> opt(n+1); opt[0][0LL] = 0LL; opt[1][0LL] = 0LL; // ��������� �������� b-��� (� ���� ����) ���� ���� ����� for(int i=0; i<n; i++) { //#ifdef _DEBUG // cerr << "started i = " << i << " at " << (clock()*1.0)/CLOCKS_PER_SEC; //#else if(i>1) opt[i-1].clear(); //#endif for(mLLLL::iterator it = opt[i].begin(); it != opt[i].end(); it++) { for(int bits_num = 1; bits_num <= max_bits; bits_num++) { LL b = next_with_bits(it->first, bits_num); if(b <= m - n + i + 1) { try_insert(opt[i+1], b, it->second + data[i] * bits_num); } } } //#ifdef _DEBUG // cerr << "; sz = " << opt[i+1].size() << endl; //#endif } cout << opt.back().rbegin()->second << endl; return 0; } |