hdu 5014 贪心
来源:互联网 发布:刚开始做淘宝卖什么好 编辑:程序博客网 时间:2024/06/10 13:28
题意:
给一个数列a,有n+1个整数:
● ai ∈ [0,n]
● ai ≠ aj( i ≠ j )
然后对于这样两个数列,他们的异或和的定义是:
t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)
现在,给出数列a,求最大的和t,和实现最大的和的数列b。
解析:
贪心,给一个数直接猜他对应的那个最大的数为多少:
比如,6的二进制为110,则由111 - 110 = 1,它所对应的数为1。
再比如,4的二进制为100,则由111 - 100 = 011,它所对应的数为3。
...
依次类推,由于较小的数是由较大的数推算出来的,所以通过一个数组num来记录并且从大到小来循环。
代码:
#pragma comment(linker, "/STACK:1677721600")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <cassert>#include <iostream>#include <algorithm>#define pb push_back#define mp make_pair#define LL long long#define lson lo,mi,rt<<1#define rson mi+1,hi,rt<<1|1#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define mem(a,b) memset(a,b,sizeof(a))#define FIN freopen("in.txt", "r", stdin)#define FOUT freopen("out.txt", "w", stdout)#define rep(i,a,b) for(int i=(a); i<=(b); i++)#define dec(i,a,b) for(int i=(a); i>=(b); i--)using namespace std;const int mod = 1e9 + 7;const double eps = 1e-8;const double ee = exp(1.0);const int inf = 0x3f3f3f3f;const int maxn = 100000 + 10;const double pi = acos(-1.0);const LL iinf = 0x3f3f3f3f3f3f3f3f;int readT(){ char c; int ret = 0,flg = 0; while(c = getchar(), (c < '0' || c > '9') && c != '-'); if(c == '-') flg = 1; else ret = c ^ 48; while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48); return flg ? - ret : ret;}int a[maxn];int num[maxn];int fun(int x){ int cnt = 0; int xx = x; while (x) { cnt++; x /= 2; } int t = 1; t <<= (cnt); return t - xx - 1;}int main(){ #ifdef LOCAL FIN; #endif // LOCAL int n; while (~scanf("%d", &n)) { mem(num, 0); rep(i, 0, n) { a[i] = readT(); } LL sum = 0; num[0] = 0; dec(i, n, 1) { if (num[i]) continue; num[i] = fun(i); num[num[i]] = i; } rep(i, 0, n) { sum = sum + (a[i] ^ num[a[i]]); } printf("%I64d\n", sum); rep(i, 0, n) { printf("%d%c", num[a[i]], (i != n) ? ' ' : '\n'); } } return 0;}
0 0
- HDU 5014 贪心
- hdu 5014 贪心
- 贪心 HDU 5014
- hdu 5014 number sequence 贪心
- hdu 5014 Number Sequence(贪心)
- hdu 5014 贪心+位处理
- hdu 5014 Number Sequence(贪心)
- 贪心-HDU 5014Number Sequence
- HDU 贪心
- 【贪心】hdu
- HDU-#5014 Number Sequence(贪心+构造)
- HDU 5014 Number Sequence(贪心)
- hdu 5014 Number Sequence (贪心)
- HDU 2111 Saving HDU(贪心)
- HDU 2111贪心--【Saving HDU】
- HDU 2111 Saving HDU (贪心)
- HDU Saving HDU (贪心)
- HDU 2111 Saving HDU【贪心】
- 数据挖掘(9):BP神经网络算法与实践
- tablebview滚动到顶部
- 通过串口终端操作开发板
- 个人学习笔记---linux内核同步
- 查询表上的约束
- hdu 5014 贪心
- Remove Element
- 「两个指针的应用」最短摘要的生成
- ajax(1)
- coroutine
- Codeforces #319(Div.2) B. Modulo Sum (动态规划)
- Linux网络编程中服务端判断客户端断开连接。
- Baskets of Gold Coins
- 前端学习笔记-9月--基本布局