一个长度13的尺子,如果在1位置刻点可以量出1和12,13三种刻度.那么至少刻几个点,可以直接量出1-13所有的长度,分别刻在哪几个位置?

注:必须是直接量。即在尺子上能找出一个1-13任意的整数长度。

写了个没什么技术含量的dfs暴力求解。一个可行解是 1, 2, 6, 10。

 #include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; class Solution {
public:
vector<vector<int>> ruler(int n, vector<int> &minPath) {
dfs(, n, minPath);
return result;
}
vector<vector<int>> result;
vector<int> path;
void dfs(int start, int n, vector<int> &minPath) {
if (start == n + ) {
if (fullScale(path)) {
result.push_back(path);
if (path.size() < minLen) {
minLen = path.size();
minPath = path;
}
}
return;
}
for (int i = start; i <= n; i++) {
path.push_back(i);
dfs(i + , n, minPath);
path.pop_back();
}
}
bool fullScale(vector<int> path) {
if (path.size() < ) {
return false;
}
unordered_map<int, int> umap;
umap[]++;
umap[]++;
for (int i = ; i < path.size(); i++) {
for (int j = ; j < i; j++) {
if (path[i] - path[j] < ) {
umap[path[i] - path[j]]++;
umap[path[j]]++;
umap[path[i]]++;
umap[ - path[i]]++;
umap[ - path[j]]++;
}
if (umap.size() >= ) {
return true;
}
}
}
return false;
}
private:
int minLen = ;
}; int main() {
int n = ;
Solution solu;
vector<int> minPath;
vector<vector<int>> res = solu.ruler(n, minPath);
for (auto x : minPath) {
cout << x << ", ";
}
}

ref: https://en.wikipedia.org/wiki/Sparse_ruler

minimal sparse ruler problem 最少尺子刻度问题的更多相关文章

  1. [dfs] UVALive 3667 Ruler

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://ic ...

  2. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  3. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

    Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...

  4. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  5. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  6. 【区间覆盖问题】uva 10020 - Minimal coverage

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

  7. Object Tracking Benchmark

    Abstract 问题: 1)evaluation is often not suffcient 2)biased for certain types of algorthms 3)datasets ...

  8. MFC图形绘制——绘制直尺和坐标系

    一.实验目的 1.掌握建立MFC应用程序的方法: 2.掌握映射模式. 二.实验内容 1.在MFC中绘制直尺,直尺需要有刻度,类似于日常学生使用的透明塑料直尺,需要建立四个直尺,分别分布在屏幕客户区的上 ...

  9. Codeforces 480B Long Jumps 规律题

    题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 以下n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x ...

随机推荐

  1. Java Service Wrapper简介与使用(转)

    本文转自https://www.cnblogs.com/zcy_soft/p/3738947.html,写的非常好,珍藏一下 1. wrapper的意思? wrapper在此处理解为“包装”. 2. ...

  2. 窗口大小调整后处理事件jQuery插件ResizeEnd

    需要引入的文件: <script src="js/jquery.min.js"></script> <script src="js/jQue ...

  3. JavaScript数据结构-10.字典

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. docker最新版本如何自定义配置文件

    1 如果你想使用 /etc/default/docker文件配置你的docker 在 /etc/systemd/system/docker.service.d/docker.conf 添加下面---- ...

  5. 安装Postgresql之后,创建用户 配置rails

    登录 sudo su - postgres psql 1 创建Postgresql新用户,devpg是用户名,密码也是devpg, 不是超级管理员,拥有创建数据库权限,登录权限,继承拥有角色权限 cr ...

  6. 深入理解java集合框架之---------LinkedList

    日常开发中,保存一组数据使用的最多的就是 ArrayList, 其次就是 LinkedList 了. 我们知道 ArrayList 是以数组实现的,遍历时很快,但是插入.删除时都需要移动后面的元素,效 ...

  7. 在Linux上使用C语言编程获取IPv4地址及子网掩码

    在Linux上(如Ubuntu或CentOS), 获取某个Network Interface比如eth0的IP地址等信息,我们可以使用ifconfig或者ip addr show命令. $ ifcon ...

  8. PTA (Advanced Level) 1016 Phone Bills

    Phone Bills A long-distance telephone company charges its customers by the following rules: Making a ...

  9. Angular: 使用 RxJS Observables 来实现简易版的无限滚动加载指令

    我使用 angular-cli 来搭建项目. ng new infinite-scroller-poc --style=scss 项目生成好后,进入 infinite-scroller-poc 目录下 ...

  10. jQuery基础---动画效果

    内容摘要: 1.显示.隐藏 2.滑动.卷动 3.淡入.淡出 4.自定义动画 5.列队动画方法 6.动画相关方法 7.动画全局属性  发文不易,转载请注明出处~ 一.显示.隐藏 jQuery 中显示方法 ...