Codeforces Round #618 (Div. 1)-----A. Anu Has a Function
提示:
Codeforces Round #618 (Div. 1)-----A. Anu Has a Function
题意:
- 定义一个函数f(x , y) = f(x | y) - y
- 给定一个长度为n的序列a,给序列重新排序,使得f(f…f(f(a1,a2),a3),…an−1),an)的值最大
- n(1≤n≤10^5), ai(0≤ai≤10^9)
解题思路:
上图是计算f(11,6)的过程,可以发现运算结果对于 “11” 的二进制表示就是如果该位在"6"的二进制表示中对应的值是1那么这位就是0,否则不变,然后我们就可以得出f(x,y) = (x | y) - y = x & (~y)
由题中所要求的f(f…f(f(a1,a2),a3),…an−1),an)化简为a1 & (~a2) & (~a3) & (~a4) & … & (~an),要使得这个式子最大,只需要找到一个a1和剩余的式子与最大就ok,假设a1为数组中的第k个,则
最后记录令函数值最大的k,最后a1与ak交换输出序列就ok,时间复杂度为O(n)
解题代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
int n, k, mx = -0x3f3f3f3f, tp;
int sum1[N], sum2[N], a[N];int main()
{scanf("%d",&n);for(int i = 1 ; i <= n ; ++i){scanf("%d",&a[i]);sum1[i] = sum1[i-1] | a[i];}for(int i = n ; i >= 1 ; --i)sum2[i] = sum2[i+1] | a[i];for(int i = 1 ; i <= n ; ++i)if((tp = a[i] & ~(sum1[i-1] | sum2[i+1])) > mx)mx = tp, k = i;printf("%d",a[k]);for(int i = 1 ; i <= n ; ++i)if(i != k)printf(" %d",a[i]);printf("\n");return 0;
}
Codeforces Round #618 (Div. 1)-----A. Anu Has a Function相关推荐
- Codeforces Round #618 (Div. 2)-C. Anu Has a Function
Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the bitwise OR operation. For exa ...
- 【Codeforces Round#618 (Div. 2)】C. Anu Has a Function 题解
题目链接:C. Anu Has a Function 题目 Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the ...
- 寒假刷题13: Anu Has a Function Codeforces Round #618 (Div. 2) C
题目链接: Anu Has a Function 题目解析: 观察函数f(x,y)定义:(x|y)-y 即 取出来x里是1但是y里不是1的地方 也就是 x&(~y) (也可以列真值表) 因此题 ...
- Codeforces Round #618 (Div. 2)C、Anu Has a Function
C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- Codeforces #618 div.2 C. Anu Has a Function
C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- 贪心 ---- Codeforces Round #618 (Div. 2)B. Assigning to Classes+贪心[证明过程]
题目链接 题目大意:给你2∗n2*n2∗n个数,将这些数分成2个集合使得两个集合中位数的差值最小 解题思路:我懵了一个结论就是排序之后取中间的两个数然后就ac了 我们先对这些数字从小到大排序 证明:1 ...
- Codeforces Round #618 (Div. 2)-B. Assigning to Classes
Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...
- Codeforces Round #618 (Div. 2)-Non-zero
Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
最新文章
- Python 2.4 递归函数
- CF Gym102028G Shortest Paths on Random Forests
- 在git下搭建个人博客
- phpmyadmin不允许一个表创建多个主键的解决办法
- POJ2417 Discrete Logging | A,C互质的bsgs算法
- 抽象编程语言(APL)是什么?
- 【洛谷5284】[十二省联考2019] 字符串问题(后缀树优化建边)
- android分享数据到不同平台组件化
- Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript.
- 企业微信API学习笔记
- LVS 就是这么简单(数字后端物理验证篇)
- css 背景重复渐变_CSS3重复渐变[CSS3提示]
- 计算机固态硬盘256g,1t(后悔买256g的固态硬盘了)
- 分享:淘宝客完全开源程序。
- TypeScript基本语法(W3C)
- Mobile2.0不对等的战争
- docker,containerd,runc,docker-shim
- php中的preg_replace函数,PHP正则替换preg_replace函数如何使用
- amp; AMP; 会显示位 | Amp;aMp;amP;会原样显示
- 【Android探索】基于Android Studio平台的蓝牙遥控APP