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. 百度云盘建svnserver步骤

    安装tortoisesvn(略) 安装visualsvn(主server您可以使用)(略步骤)例如下面的安装文件夹后,: 注冊百度账号.进入百度云盘页面,建立目录,如mysvn. 下载百度云盘clie ...

  2. Lua 环境结构 --Linux

    curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 make linux ...

  3. java.lang.VerifyError

    public class VerifyErrorextends LinkageError Thrown when the "verifier" detects that a cla ...

  4. 跨容器Hybrid离线组件方案

    关键词:跨容器.Hybrid.离线 摘要:今天主要讨论的是离线组件跨容器方案,想了解在线页面如何跨webview容器,可以看 http://www.cnblogs.com/yexiaochai/p/5 ...

  5. Nio学习4——EchoServer在IO,NIO,NIO.2中的实现

    堵塞IO实现: public class PlainEchoServer { public void serve(int port) throws IOException { final Server ...

  6. android从中国天气网获取天气

    http://download.csdn.net/detail/sun6223508/8011669      里面的一切..可完全移植 版权声明:本文博主原创文章.博客,未经同意不得转载.

  7. ZOJ 3795 Grouping(Tarjan收缩点+DAG)

    Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-t ...

  8. Centos根据系统VPS安装SendMail组件使WordPress支持E-mail

    1.在putty在链接: yum install sendmail 2.启动SendMail: service sendmail start 3.检查SendMail是否在监听默认的25port: n ...

  9. .NET单元测试艺术(3) - 使用桩对象接触依赖

    List 3.1 抽取一个设计文件系统的类,并调用它 [Test] public bool IsValidLogFileName(string fileName) { FileExtensionMan ...

  10. Arcgis for Javascript之featureLayer图和属性互操作性

    说明:主要实现加载FeatureLayer并显示属性表,而要实现联动属性表与地图,首先,看看实施后的效果: 显示效果 如上图所看到的,本文章主要实现了下面几个功能:1.FeatureLayer属性表的 ...