ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description
● a i ∈ [0,n]
● a i ≠ a j( i ≠ j )
For sequence a and sequence b, the integrating degree t is defined as follows(“�” denotes exclusive or):
t = (a 0 � b 0) + (a 1 � b 1) +・・・+ (a n � b n)
(sequence B should also satisfy the rules described above)
Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.
Input
For each case, the first line contains an integer n(1 ≤ n ≤ 10 5), The second line contains a 0,a 1,a 2,...,a n.
Output
Sample Input
Sample Output
这个题目想到贪心策略就好办了。
最先想到的一种策略就是对于数k的二进制形式,把0换成1, 1换成0,亦或后将得到全1的最大数。而且每个数的0、1组合都是唯一的,自然其对应的那个数也是唯一的。
但是会遇到一种情况,比如二进制1111和11111,它们都是和0亦或得到最大。
于是这样考虑:
对于任意一个数,只有两种情况,一是和比它小的数亦或,二是和比它大的数亦或。如果和比它小的数亦或,自然对应的数第一位肯定是0。后面可能会跟若干个0,最坏是1111这种,对应的是0;如果和比它大的数亦或,会发现,对应的数二进制位数一定比这个数长,自然1111对应的可能是10000、110000等等。
于是,只需要从大到小进行一一匹配,就不会出现之前的情况。例如之前那种情况,如果11111先匹配,11111将会和0匹配,而1111将会在之前就和10000匹配了。
ps:据说sum结果是n*(n+1),可以找规律找出来。不过我暂时推不出来。
ps:结果可能很大,需要long long来存。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std; int a[100005], Hash[100005], n;
long long sum; void qt()
{
sum = 0;
memset(Hash, -1, sizeof(Hash));
int k, v, val, p;
for (int i = n; i >= 0; i--)
{
if (Hash[i] >= 0)
continue;
k = i;
p = 1;
val = 0;
for (;k;)
{
v = k & 1;
val += (v^1) * p;
p <<= 1;
k >>= 1;
}
Hash[i] = val;
Hash[val] = i;
}
for (int i = 0; i <= n; ++i)
sum += a[i]^Hash[a[i]];
} int main()
{
//freopen("test.txt", "r", stdin);
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i <= n; ++i)
scanf("%d", &a[i]);
qt();
printf("%I64d\n", sum);
for (int i = 0; i <= n; ++i)
{
if (i)
printf(" ");
printf("%d", Hash[a[i]]);
}
printf("\n");
}
return 0;
}
ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)的更多相关文章
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strange storehouse in PKU. In this ...
- hdu 5011 nim博弈 (2014西安网赛E题)
n堆石子,每次可以选一堆取走至少一个,之后你可以不操作或者把该堆石子分成两堆,每堆至少一个,和还是原来(取完石子后)的石子个数. Sample Input1121 131 2 3 Sample Out ...
- hdu 5007 水题 (2014西安网赛A题)
题意:出现Apple.iPod.iPhone.iPad时输出MAI MAI MAI!,出现Sony,输出SONY DAFA IS GOOD! Sample InputApple bananaiPad ...
- HDU 5014 Number Sequence(位运算)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
随机推荐
- 【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
第一步.下载.解压.配置环境变量: 官网下载sqoop1.99.3 http://mirrors.cnnic.cn/apache/sqoop/1.99.3/ 将sqoop解压到目标文件夹,我的是 /h ...
- cmake学习之- cmake_parse_arguments
最后更新: 2019-06-08 一.指令介绍 cmake_parse_arguments 为解析函数(function)或 宏(macros) 参数的命令: cmake_parse_argument ...
- less1.5中的减错误
(@bodywidth)-@leftwidth即在第一个变量外加括号---正确 @bodywidth-@leftwidth即直接使用不加括号--------错误
- js 中的 prototype 和 constructor
var a=function(){ this.msg="aa"; } a.prototype.say=function(){ alert('this is say');} 1.只有 ...
- java从apk文件获取包名、版本号、icon
依赖:仅依赖aapt.exe 支持:仅限windows 功能:用纯java获取apk文集里的包名,版本号,图标文件[可获取到流直接保存到文件系统] 原理:比较上一篇文章里通过反编译然后解析Androi ...
- SSH 使用密钥登录并禁止口令登录
小结:修改下sshd配置文件,把公钥传上去就好了 先生成公钥和私钥,默认在/root/.ssh/目录,可以先看一下有没有这个目录. 生成公钥后,以后其它服务器也都可以复用这个公钥 最好生成时输入密码! ...
- PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案
一:PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案 (1) php服务器把图片处理成缩率图或指定大小的图片在通过PHP程序代码 操作FTP 上传到图片服务器. 二:图片服务器 ...
- kfaka windows安装
1 官网下载 解压到D:\developTools\kfaka\kafka_2.10-0.9.0.0 2 windows cmd启动 新开cmd命令:cd /d D:\developTools\kfa ...
- 使用@Scheduled注解编写spring定时任务
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframewor ...
- 性能测试--测试流程、APDEX、linux性能知识
测试流程.APDEX.linux性能知识 一.性能测试流程: 整体流程:收集需求-->搭建测试环境-->设计性能测试场景-->开发测试脚本-->执行测试-->收集数据-- ...