#include <bits/stdc++.h> using namespace std; int next(int x){ int r, n, ret; r = x & (-x); n = x + r; ret = x ^ n; ret /= r; ret >>= 2; ret |= n; return ret; } struct val{ int m=1e09, prev=0, c=0, t[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; }; int main(){ int n; scanf("%d", &n); vector<val> dp(n+21); for(int i = 0; i < 20; ++i) dp[0].t[i] = (1<<(i+1)) - 1; //for(int i = 0; i < 20; ++i) printf("%d ", dp[0].t[i]); dp[0].m = 0; for(int i = 0; i < n; ++i){ for(int b = 0; b < 20; ++b){ if(dp[i].t[b] > dp[i].m && dp[i].t[b] < dp[i+b+1].m){ dp[i+b+1] = dp[i]; dp[i+b+1].prev = i; dp[i+b+1].m = dp[i].t[b]; dp[i+b+1].t[b] = next(dp[i+b+1].t[b]); ++dp[i+b+1].c; } } } int it = n; printf("%d\n", dp[n].c); int c = 0; while(it){ ++c; printf("%d ", dp[it].m); it = dp[it].prev; } //printf("\n%d\n", c); 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 | #include <bits/stdc++.h> using namespace std; int next(int x){ int r, n, ret; r = x & (-x); n = x + r; ret = x ^ n; ret /= r; ret >>= 2; ret |= n; return ret; } struct val{ int m=1e09, prev=0, c=0, t[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; }; int main(){ int n; scanf("%d", &n); vector<val> dp(n+21); for(int i = 0; i < 20; ++i) dp[0].t[i] = (1<<(i+1)) - 1; //for(int i = 0; i < 20; ++i) printf("%d ", dp[0].t[i]); dp[0].m = 0; for(int i = 0; i < n; ++i){ for(int b = 0; b < 20; ++b){ if(dp[i].t[b] > dp[i].m && dp[i].t[b] < dp[i+b+1].m){ dp[i+b+1] = dp[i]; dp[i+b+1].prev = i; dp[i+b+1].m = dp[i].t[b]; dp[i+b+1].t[b] = next(dp[i+b+1].t[b]); ++dp[i+b+1].c; } } } int it = n; printf("%d\n", dp[n].c); int c = 0; while(it){ ++c; printf("%d ", dp[it].m); it = dp[it].prev; } //printf("\n%d\n", c); return 0; } |