标签:io for 2014 on c amp line size r
尽可能凑2^x-1
#include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0; i <= n; i ++) { scanf("%d", &a[i]); } memset(p, 0, sizeof p); long long ans = 0; for(int i = n; i >= 0; i --) { if(p[i] != 0) continue; if(i == 0) continue; int t = 1<<(init(i)); if(t == 2 * i) { p[i] = i-1; p[i-1] = i; ans += 2 * (i^(i-1)); } else if(t == i + 1) { p[i] = 0; p[0] = i; ans += 2*i; } else { if(p[t-i] == 0) { p[i] = t - i - 1; p[t-i-1] = i; ans += 2 * (t-1); } } } printf("%I64d\n", ans); for(int i = 0; i <= n; i ++) { if(i != 0) printf(" "); printf("%d", p[a[i]]); }puts(""); } return 0; }
HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi'an Online
标签:io for 2014 on c amp line size r
原文地址:http://blog.csdn.net/qq574857122/article/details/39272047