Binary codes
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5647   Accepted: 2201

Description

Consider a binary string (b1…bN) with N binary digits. Given such a string, the matrix of Figure 1 is formed from the rotated versions of the string.

b1 b2 bN−1 bN
b2 b3 bN b1
bN−1 bN bN−3 bN−2
bN b1 bN−2 bN−1

Figure 1. The rotated matrix

Then rows of the matrix are sorted in alphabetical order, where ‘0’ is before ‘1’. You are to write a program which, given the last column of the sorted matrix, finds the first row of the sorted matrix.

As an example, consider the string (00110). The sorted matrix is

0 0 0 1 1
0 0 1 1 0
0 1 1 0 0
1 0 0 0 1
1 1 0 0 0

and the corresponding last column is (1 0 0 1 0). Given this last column your program should determine the first row, which is (0 0 0 1 1).

Input

The first line contains one integer N ≤ 3000, the number of binary digits in the binary string. The second line contains N integers, the binary digits in the last column from top to bottom.

Output

The first line contains N integers: the binary digits in the first row from left to right.

Sample Input

5
1 0 0 1 0

Sample Output

0 0 0 1 1

对由0,1组成的n个数。照题中的旋转,最后依据每行的字典序排序,组成n*n的矩阵,给出矩阵的最后1列,求矩阵

的第首行。

给出最后一列能够求出第0列,由于是按字典序排的,所以第0列肯定0在前,1在后。而第0列为0的相对位置在最后

1列不变。由于第0列都为0,又是按字典序排的。第0列为1也一样。依据第0列和最后一列就能够将相应关系求出。

也就是next数组。

比如例子的

0 0 0 1 1

0 0 1 1 0

0 1 1 0 0

1 0 0 0 1

1 1 0 0 0

next[ ]={1,2,4,0,3}

第0行第0列为0,第0行的第1列下次旋转后为第0列的第0行,所以第0行第1列为第0列的第1行为0,第1列的第0行

为第2列的第1行,为第0列的第2行。所以第0行的第2列为第0列的第2行为0,通过推理发现为next数组中元素递推

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5000+100;
int last[maxn];
int first[maxn];
int next[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d",&last[i]);
first[i]=last[i];
}
sort(first,first+n);
int cur=0;
int i;
for(i=0;i<n;i++)
{
if(first[i])
break;
while(last[cur]&&cur<n)
cur++;
next[i]=cur++;
}
cur=0;
for(i=i;i<n;i++)
{
while(last[cur]==0&&cur<n)
cur++;
next[i]=cur++;
}
int k=0;
for(int i=0;i<n-1;i++)
{
printf("%d ",first[k]);
k=next[k];
}
printf("%d\n",first[k]);
}
return 0;
}

poj 1147 Binary codes的更多相关文章

  1. poj 2541 Binary Witch

    Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS   Memory Limit: 65536K       Descript ...

  2. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  3. poj 1430 Binary Stirling Numbers

    Binary Stirling Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1761   Accepted ...

  4. Poj 2499 Binary Tree(贪心)

    题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...

  5. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  6. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  7. POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)

    题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...

  8. POJ 2541 Binary Witch(逆序KMP,好题)

    逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...

  9. POJ 2499 Binary Tree

    题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...

随机推荐

  1. 大四实习准备4_java内部类

    2015-4-30 [昨天又可耻地休息了一天,懒劲比较大啊.跟懒劲有直接关系,这几天对幸福的感知也黯淡了,对未来的幸福不是那么渴望了.表现在对亲情和爱情上. 我想生活的本意是积极进取.茁壮生长并时常感 ...

  2. bzoj题解

    bzoj1012 线段树水题 bzoj1207 LIS水题 bzoj2190 挡住,即 bzoj1601 已被删除的水题? bzoj1607 线段树细节题,搞清楚特判即可 bzoj1008 快速幂妥妥 ...

  3. UVa 201 Squares

    题意: 给出这样一个图,求一共有多少个大小不同或位置不同的正方形. 分析: 这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度 ...

  4. Django提供后台接口的跨域问题

    --> Django跨域 当使用Django仅用来开发后端接口,为前端提供JSON数据的时候,不可避免的要接受前端的POST请求.虽然Django以其强大易用的特定使用很广泛,但在跨域问题上却让 ...

  5. 陈正冲老师讲c语言void关键字

    1. void a void的字面意思是“空类型”,void *则为“空类型指针”,void *可以指向任何类型的数据. void几乎只有“注释”和限制程序的作用,因为从来没有人会定义一个void变量 ...

  6. js模拟键盘按键事件

    var WshShell = new ActiveXObject('WScript.Shell') WshShell.SendKeys('{ }'); 说明:大括号内的是键盘上的按键如: 空格:{ } ...

  7. hive内部表与外部表区别

    1.在Hive里面创建一个表: hive> create table wyp(id int,    > name string,    > age int,    > tele ...

  8. 【原】Storm配置

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  9. Log Explorer使用说明

    一.介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复.你可以浏览日志.导出数据.恢复被修改或者删除的数据(包括执行过update,delete,drop和trunca ...

  10. bzoj 3529 [Sdoi2014]数表(莫比乌斯反演+BIT)

    Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a ...