一个长度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. struts2 基本流程

    一.配置过程 1.在web.xml中配置过滤器 <filter> <filter-name>StrutsPrepareAndExecuteFilter</filter-n ...

  2. C 扩展库 - sqlite3 API CRUD

    CRUD struct student typedef struct STUDENT { unsigned int id; unsigned char name[16]; unsigned int a ...

  3. 使用Rectangle+ImageBrush来代替Image,解决图片模糊的问题

    <Rectangle Margin="0" Stroke="Black" HorizontalAlignment="Right" Wi ...

  4. leetcode1:线性表

    //定义二维数组int **array = new int*[row_num]; ;i<row_num;i++) { array[i] = new int[col_num]; } vector& ...

  5. iOS 性能优化套路

    ***  一级套路 ***  使用ARC管理内存- 防止内存泄露- 保证释放掉不再需要的内存,提高性能 在正确的地方使用 reuseIdentifier平时接触的需要考虑重用的视图有UICollect ...

  6. 行人检测4(LBP特征)

    参考原文: http://blog.csdn.net/zouxy09/article/details/7929531 http://www.cnblogs.com/dwdxdy/archive/201 ...

  7. Java代码生成图片验证码

    1.jsp页面显示验证码,src跳转到Controller里的:getImg_validate,reloadImg()作用是点击验证码就会换一张(重新请求getImg_validate) <ul ...

  8. fuzhou 1692 Key problem ***

    Problem 1692 Key problem Accept: 103    Submit: 553 Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  9. .NET 垃圾回收浅解

    在说明垃圾回收的实现机制之前,先说明一下垃圾回收存在的背景. 垃圾回收器(GC)是.NET平台中一个很重要的组成部分,.NET垃圾回收机制降低了编写程序的复杂程度,使程序员不用耗费精力去处理析构,成功 ...

  10. csharp: using wininet.dll

    /// <summary> /// Windows Embedded CE 6.0 R3 WinInet Functions /// https://technet.microsoft.c ...