动态规划——Compatible Numbers

Two integers x and y are compatible, if the result of their bitwise "AND" equals zero, that is, a & b = 0. For example, numbers 90 (10110102) and 36 (1001002) are compatible, as 10110102 & 1001002 = 02, and numbers 3 (112) and 6 (1102) are not compatible, as 112 & 1102 = 102.

You are given an array of integers a1, a2, ..., an. Your task is to find the following for each array element: is this element compatible with some other element from the given array? If the answer to this question is positive, then you also should find any suitable element.

Input

The first line contains an integer n (1 ≤ n ≤ 106) — the number of elements in the given array. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 4·106) — the elements of the given array. The numbers in the array can coincide.

Output

Print n integers ansi. If ai isn't compatible with any other element of the given array a1, a2, ..., an, then ansi should be equal to -1. Otherwise ansi is any such number, that ai & ansi = 0, and also ansi occurs in the array a1, a2, ..., an.

Example
Input
2
90 36
Output
36 90
Input
4
3 6 3 6
Output
-1 -1 -1 -1
Input
5
10 6 9 8 2
Output
-1 8 2 2 8

题意:给n个数,对于某个数a[i]如果有a[j]使a[i]&a[j]==0,则对于a[i]对应a[j],否则对应-1。要求输出每个数所对应的数。如果有多种情况输出其中一种即可。


思路:最容易想到的是对每个数在数组中遍历寻找,但是无疑会TLE。。。

于是看数据知道a[i]最大为4·106

那么可以设置一个常数k=(1<<22)-1;然后用dp[i^a[i]]=a[i]保存a[i]和与k的异或的值的对应关系。

之后寻找时对每个位枚举即可。

详见代码。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int k=(1<<22)-1;
int n;
int dp[k+1];
int a[4000005];
int main()
{
    while(~scanf("%d", &n))
    {
        memset(a, 0, sizeof(a));
        memset(dp, 0, sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            dp[k^a[i]]=a[i];
        }
        for(int i=k; i>=0; i--)
        {
            if(!dp[i])             //如果没有这个状态就不用判断
                for(int j=0; j<22; j++)
                {
                    if(dp[i|(1<<j)])             //枚举每一位是否出现过这种状态
                        dp[i]=dp[i|(1<<j)];
                }
        }
        for(int i=1; i<=n; i++)
        {
            if(dp[a[i]])
                printf("%d", dp[a[i]]);
            else
                printf("-1");
            if(i==n)
                printf("\n");
            else
                printf(" ");
        }
    }
    return 0;
}




# UART Echo Example (See the README.md file in the upper level 'examples' directory for more information about examples.) This example demonstrates how to utilize UART interfaces by echoing back to the sender any data received on configured UART. ## How to use example ### Hardware Required The example can be run on any development board, that is based on the Espressif SoC. The board shall be connected to a computer with a single USB cable for flashing and monitoring. The external interface should have 3.3V outputs. You may use e.g. 3.3V compatible USB-to-Serial dongle. ### Setup the Hardware Connect the external serial interface to the board as follows. ``` ----------------------------------------------------------------------------------------- | Target chip Interface | Kconfig Option | Default ESP Pin | External UART Pin | | ----------------------|--------------------|----------------------|-------------------- | Transmit Data (TxD) | EXAMPLE_UART_TXD | GPIO4 | RxD | | Receive Data (RxD) | EXAMPLE_UART_RXD | GPIO5 | TxD | | Ground | n/a | GND | GND | ----------------------------------------------------------------------------------------- ``` Note: Some GPIOs can not be used with certain chips because they are reserved for internal use. Please refer to UART documentation for selected target. Optionally, you can set-up and use a serial interface that has RTS and CTS signals in order to verify that the hardware control flow works. Connect the extra signals according to the following table, configure both extra pins in the example code `uart_echo_example_main.c` by replacing existing `UART_PIN_NO_CHANGE` macros with the appropriate pin numbers and configure UART1 driver to use the hardware flow control by setting `.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS` and adding `.rx_flow_ctrl_thresh = 122` to the `uart_config` structure. ``` --------------------------------------------------------------- | Target chip Interface | Macro | External UART Pin | | ----------------------|-----------------|-------------------- | Transmit Data (TxD) | ECHO_TEST_RTS | CTS | | Receive Data (RxD) | ECHO_TEST_CTS | RTS | | Ground | n/a | GND | --------------------------------------------------------------- ``` ### Configure the project Use the command below to configure project using Kconfig menu as showed in the table above. The default Kconfig values can be changed such as: EXAMPLE_TASK_STACK_SIZE, EXAMPLE_UART_BAUD_RATE, EXAMPLE_UART_PORT_NUM (Refer to Kconfig file). ``` idf.py menuconfig ``` ### Build and Flash Build the project and flash it to the board, then run monitor tool to view serial output: ``` idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. ## Example Output Type some characters in the terminal connected to the external serial interface. As result you should see echo in the same terminal which you used for typing the characters. You can verify if the echo indeed comes from ESP board by disconnecting either `TxD` or `RxD` pin: no characters will appear when typing. ## Troubleshooting You are not supposed to see the echo in the terminal which is used for flashing and monitoring, but in the other UART configured through Kconfig can be used. 我怎么使用
最新发布
05-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值