HDU5014Number Sequence(贪心)

题目链接

题目大意:

给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而且使得t = A0^B0 + Ai + 1 ^ Bi + 1 + ... + An ^ Bn 最大。

解题思路:

对于一个数字进行异或,要求结果最大的话,那么取这个数字的二进制互补数字是最好的情况,而且能够发现每次找到一个数字和相应的互补的数字都会是一段区间。就这样一段一段区间的去寻找每一个点相应的最好的匹配点。

代码:

#include <cstdio>
#include <cstring> typedef long long ll;
const int N = 1e5 + 5;
const int M = 20; int num[N];
int Map[N];
int n;
ll t[M]; void init () { t[0] = 1;
for (int i = 1; i <= M; i++)
t[i] = t[i - 1] * 2;
} int main () { init();
while (scanf ("%d", &n) == 1) { for (int i = 0; i <= n; i++)
scanf ("%d", &num[i]); int rear = n;
int front;
ll ans = 0;
// printf ("%lld\n", t[M - 1]);
while (rear >= 0) { for (int i = 0; i < M; i++)
if (t[i] > rear) {
front = t[i] - rear - 1;
break;
} for (int i = 0; i < (rear - front + 1) / 2; i++) { Map[rear - i] = front + i;
Map[front + i] = rear - i;
} if (rear == front)
Map[rear] = front;
rear = front - 1;
} for (int i = 0; i <= n; i++)
ans += num[i] ^ Map[num[i]];
printf ("%lld\n", ans);
for (int i = 0; i < n; i++)
printf("%d ", Map[num[i]]);
printf ("%d\n", Map[num[n]]);
}
return 0;
}

HDU5014Number Sequence(贪心)的更多相关文章

  1. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  2. hdu4915 Parenthese sequence 贪心O(n)解法(new)

    hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  3. hdu_5783_Divide the Sequence(贪心)

    题目链接:hdu_5783_Divide the Sequence 题意: 给你一个数列,让你分尽可能多的段,并且保证每一段的前缀和都不小于0 题解: 从后往前xjb贪心就行了 #include< ...

  4. hdu 5783 Divide the Sequence 贪心

    Divide the Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  5. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  6. hdu 6047 Maximum Sequence 贪心

    Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...

  7. 【BZOJ1345】[Baltic2007]序列问题Sequence 贪心+单调栈

    [BZOJ1345][Baltic2007]序列问题Sequence Description 对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和a ...

  8. CF3D Least Cost Bracket Sequence 贪心

    Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...

  9. 南理第八届校赛同步赛-F sequence//贪心算法&二分查找优化

    题目大意:求一个序列中不严格单调递增的子序列的最小数目(子序列之间没有交叉). 这题证明贪心法可行的时候,可以发现和求最长递减子序列的长度是同一个方法,只是思考的角度不同,具体证明并不是很清楚,这里就 ...

随机推荐

  1. Caused by: org.springframework.beans.factory.BeanCreationException

    1.错误原因 2014-7-13 17:36:57 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one J ...

  2. Memcahce(MC)系列(三)Memcached它PHP转让

    由PHP转让Memcahce,首先,需要在server安装Memcache,如何安装Memcache这不是本文的重点, 大约memcache安装,谁的朋友有兴趣,请参阅这里:http://blog.c ...

  3. CodeForces 396C 树状数组 + DFS

    本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...

  4. .net与Java的WebService互调

    本文记录一下.net与Java是如何进行Web Service的互相调用的. 1.准备工作 MyEclipse 10 JDK 1.6.0_13 Visual Studio 2012 .net fram ...

  5. Android开发学习总结——Android开发的一些相关概念(转)

    一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 Ÿ 3G指的是第三代移 ...

  6. Android 应用程序窗口显示状态操作(requestWindowFeature()的应用)

     我们在开发程序是常常会须要软件全屏显示.自己定义标题(使用button等控件)和其它的需求,今天这一讲就是怎样控制Android应用程序的窗口显示. 首先介绍一个重要方法那就是requestWi ...

  7. C# Socket TCP Server & Client & nodejs client

    要调试公司某项目里的一个功能,因为要准备测试环境,趁这个机会重温了一下Socket(全还给老师了 -_-#),做个备份. C# Server static void Main(string[] arg ...

  8. Afinal载入网络图片及下载文件用法

    Afinal高速开发框架使用起来很方便.以下将解说怎样利用Afinal载入网络图片及下载文件: 先看效果图: 注意:使用Afinal前需加入Afinal的jar,能够在这里下载:http://down ...

  9. SDUT OJ 2463 学校password你必须学会科学计划

    #include<iostream> #include<string.h> #include<stdio.h> #define N 10010 #define M ...

  10. Mapxtreme C#鹰眼地图

    Demo演示程序下载地址: http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找:EagelEyeMap.r ...