HDU - 5014 Number Sequence(贪心+位运算)

Viobot使用教程总贴 使用教程总贴 阅读详情
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[]   [Go Back]   [Status]  

Description

There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: 

● 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

There are multiple test cases. Please process till EOF. 

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

For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b  0,b  1,b  2,...,b n. There is exactly one space between b  i and b  i+1 (0 ≤ i ≤ n - 1). Don’t ouput any spaces after b  n. 
 

Sample Input

    
4 2 0 1 4 3
 

Sample Output

    
20 1 0 2 3 4
 

题目大意:
有一个a集合满足
● a i ∈ [0,n] 
● a i ≠ a j( i ≠ j ) 

同样的b也满足该规则,
现在问你是否存在一个序列b,使得t最大

t = (a 0 � b 0) + (a 1 � b 1) +・・・+ (a n � b n)

n(1 ≤ n ≤ 10 5)


解析:贪心题,但是比赛时候我用的是回溯求解该题,一直超时。后来想想我太笨了如果用回溯的话复杂度为O(10^10),太大了。

其实弄清楚了这题的思路敲起来还是蛮简单的,每一个数的最大的异或是和他互补的数。如10101^01010最大,如果全部都取最大,那么最后的t肯定最大。

那么一个数的互补怎么求呢? 就是这个数 异或上 与该数二进制位数相同,且都为1的数

如

       1010

xor  1111

       0101

弄清楚这个就好办了。求出所有数的互补,把它存入一个数组中,最后按照输入时的顺序输出该数组,就好了。

注意:Hdu有个比较坑的地方,就是不能开成long long 所以应该把t 声明为__int64才能过,坑爹啊。


#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef __int64 LL;
const int MAX = 100005;
int a[MAX],ans[MAX],vis[MAX];

int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		for(int i = 0;i <= n;++i)
			scanf("%d",&a[i]);
		memset(vis,0,sizeof(vis));
		memset(ans,0,sizeof(ans));
		for(int i = n;i > 0;--i) {
			if(vis[i])
				continue;
			int d = log2(i)+1;
			int temp = ((1 << d) - 1)  ^ i;
			ans[i] = temp;
			ans[temp] = i;
			vis[temp] = vis[i] = 1;
		}
		LL sum = 0;
		for(int i = 0;i <= n;++i)
			sum += (LL)(i ^ ans[i]);
		printf("%I64d\n",sum);
		for(int i = 0;i < n;++i)
			printf("%d ",ans[a[i]]);
		printf("%d\n",ans[a[n]]);
	}
	return 0;
}


手把手教你学Simulink--基于高频与高速信号处理的滤波器场景实例:毫米波雷达中匹配滤波器的多目标分辨仿真 在自动驾驶、智能交通和工业感知等领域,毫米波雷达(mmWave Radar)因其高分辨率全天候工作和抗电磁干扰能力,成为关键的感知传感器。其核心信号处理算法之一是匹配滤波器(Matched Filter),用于在强噪声背景下最大化信噪比(SNR),实现对微弱目标的检测。在调频连续波(FMCW, Frequency-Modulated Continuous Wave)雷达中,发射信号为线性调频(Chirp)信号。当信号遇到多个目标时,回波是多个延迟版本的叠加。 阅读详情

相关推荐

layui 监听单元格点击事件,获取当前点击单元格的内容以及 所在的表头内容

特别是后台的一些管理系统中 业务中还是会碰见这样的需求呢 如果你的后台使用layui写的话 在表格中 layui 也给我们提供了监听单元格的事件 但是好像不能直接获取到单元格内容个 和 对应的表头 其实 还是可以解决 换个角度使劲就会发现新的世界 不是吗 event: 'username' 在每个表头中加一个 event 属性 这样就可以监听表头事件 记住个 event 和 当前列的字段最好保持一样 table.render({ e...

业余前端的职业经历 1万+

HDOJ 5014 Number Sequence

找规律 Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 303    Accepted Submission(s): 149 Special Judge Problem D

码代码的猿猿 1253

关于【中孚计算机终端保密检查系统】的卸载

卸载中孚计算机终端保密检查系统

码农之路,向死而生 4万+

HDU5014Number Sequence(贪心)

题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,并且使得t = A0^B0 + Ai + 1 ^ Bi + 1 + ... + An ^ Bn 最大。 解题思路: 对于一个数字进行异或,要求结果最大的话,那么取这个数字的二进制互补数字是最好的情况,并且可以发现每次找到一个数字和对应的互补

1199

RT-Thread与ROS2通信:micro-ROS在STM32上的移植实践

本文详细介绍了在STM32平台上使用RT-Thread实时操作系统移植micro-ROS实现与ROS2通信的实践方案。通过对比rosserial方案,重点阐述了micro-ROS基于XRCE-DDS协议栈在数据可靠性(丢包率低于1%)、双模式通信(UDP/串口)以及C/C++混合编程方面的优势,为嵌入式设备与ROS2的高效通信提供了完整解决方案。

emacs5lisp的博客 327

第2章 数据库关系模型-单元测验-数据库原理及应用

第2章 数据库关系模型---数据库与机器学习 作业 常用的关系查询操作包括选择、投影、连接、并、交等。 v 实体完整性是指关系表的属性组成必须是完整的。 x 自然连接是一种等值连接。 v 在参照完整性中,可以定义级联操作。 v 用户自定义完整性可以实施数据的业务约束。 v 每个关系是一个二维表,但二维表不一定是关系。 v 关系中复合键至少包含两个属性。 v 代理键是为了唯一标识关系的不同元组,需要在表单或报表中显示出来。 x ...

aiqq136的博客 5723

HDU——5014 Number Sequence

Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 807    Accepted Submission(s): 387 Special Judge Problem Description

u014086857的专栏 513

2025年从零开始:构建你的首个ComfyUI工作流-ComfyUI入门教程

然而,由于它拥有众多的插件节点,以及较为复杂的操作流程,学习起来相对困难。以前面搭建的文生图工作流为例,首先点击功能区的【保存】,然后你的浏览器会出来一个弹窗,你可以进行文件命名,点击确定后浏览器会下载该文件,也就是带“json”后缀的文件,这样我们的工作流就打包成一份文件了。思考一下前面说的,节点是不是需要下载安装才能使用,而我们复用别人的工作流,他用的节点你不一定下载了,因为目前有600多个节点,你不可能全部下载了吧,所以我们日后使用别人的工作流,你会经常出现这样的问题。

weixin_59486588的博客 1454

hdu5014——构造打表找规律

#include #include #include #include using namespace std; int a[100005]; int b[100005]; int n; int fin(int x){ int temp=0; int ret=0; int i=0; while(x){ temp=((x&1)^1);

zxl博客 395

Amber实战:小分子配体MD模拟全流程避坑指南(附磷酸基团处理技巧)

本文提供了一份基于Amber软件进行小分子配体分子动力学模拟的实战避坑指南。详细解析了从晶体结构预处理、配体参数化(重点探讨AM1-BCC与RESP电荷选择)、复合物体系构建到模拟稳定性调试的全流程。特别针对磷酸基团等特殊官能团处理中常见的模拟崩溃问题,给出了系统性的诊断与解决方案,旨在帮助研究者获得稳定可靠的模拟结果。

weixin_29237635的博客 682

11.8 LangGraph生产级AI Agent开发:从节点定义到高并发架构的终极指南

本文介绍了如何使用 LangGraph 构建生产级 AI Agent,重点解析了 LangGraph 的核心设计、节点与边的实现方法论、生产级实现技巧以及性能优化策略。LangGraph 通过图结构抽象复杂 AI 工作流,核心组件包括节点、边、状态和检查点。节点通过 Python 函数封装原子业务逻辑,边通过条件分支实现动态工作流。生产级实现技巧包括状态管理、多会话控制和错误处理机制。性能优化策略涉及并行执行和缓存机制设计。文章还提供了旅行规划 Agent 的实战案例和常见问题解决方案,帮助开发者构建高效、

yonggeit的博客 3268

Number Sequence HDU - 5014(思维构造)

Number Sequence HDU - 5014 There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence...

codeswarrior的博客 355

W5500以太网&PoE扩展模块原理图设计,已量产

本文将以 W5500 以太网 & PoE 扩展板为对象,从嵌入式硬件设计的视角,深度拆解其电路架构、核心器件选型、关键功能实现,以及这款板子解决行业痛点的设计思路。

不脱发的程序猿 937

贪心 + 找规律 之 hdu 5014 Number Sequence

/* 关键:贪心 + 找规律 0 1 2 3 4 5 6 肯定先从大的开始去找(贪心): 6:110 ---> 1:001  ==>  6^1 = 7 结果发现: 5: 101 ---> 2:010  ==>  5^2 = 7 4:100 ---> 3: 011  ==>  4^3 = 7 若 x ---> y,则区间 [x, y] 的所有值均可找到与其相匹配的数字。。。 */

JmingS 671

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

hdu 5014 Number Sequence(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 题意:给定数列以及数列值的范围,求该范围内另一个数列,使得两个数列对应位的异或和最大。 思路:从n到0贪心即可,要是异或和最大,那么叫二进制每一位都是是1,也就是两个数字对应为相反的数,比如1001,和0110异或和就是1111,所以对于每一个数字,先判断他是几位二进制,然后减去1,就是他所

li1004133206的专栏 470

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

hdu 5014 Number Sequence

题意:给出一个0~n的排列,你需要

A Cube Master 590

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

贪心-HDU 5014 Number Sequence

思路: 对于每一个数字的二进制是唯一的,所以他的反也是唯一的(除符号位)。这样0 ^ 1 = 1 可以使的所有数字取得最大值。 (之前写的时候判断了 下是否有重复的,看了下网上代码发现想多了。。) 每次对于一个数字找到他的按位反,并且记录映射关系。最后输出即可。注意每次操作可以确定 两个数 3-->4  同时4->3也就随之确定了。ans注意*2 #include #include #

GameRoad 291

hdu 5014

想法比较笨,,,不过好歹能做得出来,,,, 每个数想要有最优的异或值,必然是在二进制上互补

The Summer, The Winter 1317

【HDU - 5014】Number Sequence(贪心构造)

题干: There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai∈ [0,n] ● ai≠ aj( i ≠ j ) For sequence a and sequence b, the integrating deg...

xuanweiace的博客 401

HDU-5014 Number Sequence 最大异或和

每个数想要得到最大的异或值,就肯定是在二进制上是互补的,所以暴力找出互补的数。 #include #include #include #include #include #include #define LL long long using namespace std; int a[110000]; int n; int link[110000]; int bit(int n) {

聂小白的专栏 959

UVA - 10057 A mid-summer night's dream.

Problem C A mid-summer night�s dream Input: standard input Output: standard output   This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here

小G的ACM之路 1314

UVA - 714 Copying Books(二分+贪心)

Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been

小G的ACM之路 1196

UVA - 10905 Children's Game(贪心)

4thIIUCInter-University Programming Contest, 2005 A Children�s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman There

小G的ACM之路 1143

UVA - 311 Packets(贪心)

Packets  A factory produces products packed in square packets of the same height h and of the sizes  ,  ,  ,  ,  ,  . These products are always delivered to customers in the square parc

小G的ACM之路 1127

UVA - 10954 Add All(贪心+优先队列)

Problem F Add All Input: standard input Output: standard output Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++

小G的ACM之路 1075

UVA - 10706 Number Sequence(贪心)

Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A single positive integer iis given. Write a program to find the digit located in the position iin

小G的ACM之路 1060
上一篇: UVA - 10026 Shoemaker's Problem(贪心)
下一篇: UVA - 311 Packets(贪心)
HelloWorld10086
博客等级 码龄12年 88粉丝 614原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值