USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
找出N个二进制数,每个数有B位,使得各数两两之间“海明距离”至少为D。(若有多组解,输出字典序最小的。)
海明距离是指:两个二进制数不同二进制位的个数。
【数据范围】
1<=N<=64
1<=B<=8
1<=D<=7
【输入格式】
一行三个数N、B、D,空格分割。
【输出格式】
从小到大输出,每行输出十个数。
【输入样例】
16 7 3
【输出样例】
0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
又是一道枚举,贪心即可。
最多256个数,从0开始扫(从小到大),能取的就取了,并且把所有与之距离小于D的数“删掉”。这样一来,取的就只可能是最优解了。由于本题不可能无解(题目都没说无解咋办,当然有解。。),而且删数的时间也就是256,因此总时间比62500稍大,常熟大约是8,完全可以接受。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
第二次AC。
第一次提交没过,忘了USACO要求的输出末尾换行。。
------------------------------------------------------------------------------------------------------------------------------------------------
【代码】
/*
ID: icedrea1
PROB: hamming
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; void DtoB(int num)
{
int bit[],l=;
while(num) { bit[l++]=num&; num>>=; }
for(int i=l;i<;++i) cout<<;
for(int i=l-;i>=;--i) cout<<bit[i];
}
void look()
{
ifstream in("hamming.out"); int num;
while(in>>num)
{
DtoB(num);
cout<<endl;
} in.close();
} // just a look -----------------------------------------------------------
// go~! int N,B,D;
bool have[]; int num[],sum; int dis(int x,int y)
{
int s=;
while(x||y) { s+=(x^y)&; x>>=; y>>=; }
return s;
} void del(int x)
{
for(int y=;y<(<<B);++y)
if(dis(x,y)<D) have[y]=false;
} void print(ostream &out)
{
for(int i=;i<sum-;++i) out<<num[i]<<(i%==?"\n":" ");
out<<num[sum-]<<endl;
} int main()
{
//look(); return 0;
ifstream in("hamming.in");
ofstream out("hamming.out"); in>>N>>B>>D;
for(int x=;x<(<<B);++x) have[x]=true;
for(int x=;x<(<<B) && sum<N;++x)
{
if(!have[x]) continue;
num[sum++]=x; del(x);
} if(sum<N) out<<"not enough!"<<endl; else print(out); in.close();
out.close();
return ;
}
USACO Section2.1 Hamming Codes 解题报告 【icedream61】的更多相关文章
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
holstein解题报告 --------------------------------------------------------------------------------------- ...
- USACO Section2.2 Preface Numbering 解题报告 【icedream61】
preface解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.3 Controlling Companies 解题报告 【icedream61】
concom解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Money Systems 解题报告 【icedream61】
money解题报告------------------------------------------------------------------------------------------- ...
- USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Longest Prefix 解题报告 【icedream61】
prefix解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...
- COGS2287 [HZOI 2015]疯狂的机器人
[题目描述] 现在在二维平面内原点上有一只机器人 他每次操作可以选择向右走,向左走,向下走,向上走和不走(每次如果走只能走一格) 但是由于本蒟蒻施展的大魔法,机器人不能走到横坐标是负数或者纵坐标是负数 ...
- JSON对象与XML相互转换工具类
依赖jar <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId ...
- mac home brew install go
mac利器home brew安装Go 首先你得需要安装home brew和ruby环境(因为home brew依赖ruby) 如果没有请自行到链接安装 准备好之后就开始安装go了 brew updat ...
- Mybatis之批量更新操作
更新单条记录 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 UPDATE course SET name ...
- 安装juicer
由于我第一次安装 JUICER时遇到了很多问题,现在把这些问题都记录下来,给同样第一次安装使用的同学一点借鉴. 前面已经安装了Torch3和Tracter,这都是为安装Juicer做的准备,现在安装J ...
- LIS的string用法
题目链接 使用的是string里的find函数 stl大法好 #include<iostream> #include<cstdio> #include<cstring&g ...
- 第一个AngularJS控制器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C#注释语句
C#注释语句 注释就是在程序中标记.说明某个程序段的作用.注释语句不会被执行. 一.单行注释 // 这是一行注释 二.多行注释 /* 这是多行注释 第一行 第二行 ...... ...
- (排班表二)后台动态绘制Grid表格
后台动态绘制值班表(Grid表格 列名不固定) 要求:表头除了值班人姓名,还要显示日期,及每天的星期值,用斜杠‘/’分隔.即:几号/星期几 最终实现的效果:根据查询的年月显示每个值班人查询月份每天的值 ...