poj 1147 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的更多相关文章
- poj 2541 Binary Witch
Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS Memory Limit: 65536K Descript ...
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- poj 1430 Binary Stirling Numbers
Binary Stirling Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1761 Accepted ...
- Poj 2499 Binary Tree(贪心)
题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)
题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...
- POJ 2541 Binary Witch(逆序KMP,好题)
逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...
- POJ 2499 Binary Tree
题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...
随机推荐
- Android 之 内存管理-查看内存泄露(三)
概述 在android的开发中,要时刻主要内存的分配和垃圾回收,因为系统为每一个dalvik虚拟机分配的内存是有限的,在google的G1中,分配的最大堆大小只有16M,后来的机器一般都为24M,实在 ...
- windows ssh RPi 2B
/************************************************************************* * windows ssh RPi 2B * 声明 ...
- 多线程程序设计学习(9)worker pattern模式
Worker pattern[工作模式]一:Worker pattern的参与者--->Client(委托人线程)--->Channel(通道,里边有,存放请求的队列)--->Req ...
- JDBC 与ODBC的区别
一.ODBC(Open DataBase Connectivity : 开放数据库连接) ODBC 总体结构 应用程序 执行处理并调用odbc函数,提交sql语 ...
- Hibernate配置文件解释
Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性每个Hibernate配置文件对应一个Configuration对象Hibernate配置文件可以有两种格式: ...
- HttpServerUtility类
HttpServerUtility是一个工具类,为了在后台处理请求方便获取到一些常用的类型,Asp.net将很多常用的东西封装到这里. 比如可以使用其进行URL编码解码, HTML编码解码等. // ...
- 手把手教你WEB套打程序开发
WEB套打可选方案不多,理想的更少,利用免费控件Lodop+JavaScript实现精确套打,算是较为经典的选择.这种方案其实比较简单,利用一个htm文件就可以实现模板设计过程,几乎是“空手套”式的开 ...
- LightOJ 1422 Halloween Costumes 区间dp
题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...
- Mac,WIN下支撑 IPV6的 sftp客户端
transmit 这样的话就可以使用 ipv6了,教育网的优势体现出来了,window下得 支撑 ipv6的sftp客户端 Bitvise SSH 和 WINSCP
- ASIHTTPRequest 编码问题
今天在模拟登陆时,中文的用户名一直登陆不上,对用户名进行了各种转码还是不能解决. 在这个问题上一直卡了半个多小时,最终才发现根本不是用户名的编码问题,而是使用的第三方网络插件的ASIHTTPReque ...