minimal sparse ruler problem 最少尺子刻度问题

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

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

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

 1 #include <iostream>
 2 #include <vector>
 3 #include <unordered_map>
 4 using namespace std;
 5
 6 class Solution {
 7 public:
 8     vector<vector<int>> ruler(int n, vector<int> &minPath) {
 9         dfs(0, n, minPath);
10         return result;
11     }
12     vector<vector<int>> result;
13     vector<int> path;
14     void dfs(int start, int n, vector<int> &minPath) {
15         if (start == n + 1) {
16             if (fullScale(path)) {
17                 result.push_back(path);
18                 if (path.size() < minLen) {
19                     minLen = path.size();
20                     minPath = path;
21                 }
22             }
23             return;
24         }
25         for (int i = start; i <= n; i++) {
26             path.push_back(i);
27             dfs(i + 1, n, minPath);
28             path.pop_back();
29         }
30     }
31     bool fullScale(vector<int> path) {
32         if (path.size() < 4) {
33             return false;
34         }
35         unordered_map<int, int> umap;
36         umap[13]++;
37         umap[0]++;
38         for (int i = 0; i < path.size(); i++) {
39             for (int j = 0; j < i; j++) {
40                 if (path[i] - path[j] < 13) {
41                     umap[path[i] - path[j]]++;
42                     umap[path[j]]++;
43                     umap[path[i]]++;
44                     umap[13 - path[i]]++;
45                     umap[13 - path[j]]++;
46                 }
47                 if (umap.size() >= 14) {
48                     return true;
49                 }
50             }
51         }
52         return false;
53     }
54 private:
55     int minLen = 13;
56 };
57
58 int main() {
59     int n = 13;
60     Solution solu;
61     vector<int> minPath;
62     vector<vector<int>> res = solu.ruler(n, minPath);
63     for (auto x : minPath) {
64         cout << x << ", ";
65     }
66 }

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

时间: 2024-08-01 10:45:08

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

[dfs] UVALive 3667 Ruler

题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668 option=com_onlinejudge&Itemid=8&cate

uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】

Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test ca

HDU 5876 Sparse Graph BFS 最短路

Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are notadjacent in G. Now you are given an undirected graph G of N n

HDU 5876:Sparse Graph(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G

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

可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选择起点在s的最长区间; 2.选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分:  Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinat

UVA Minimal coverage (贪心)

Description  Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the num

HDU 2489 Minimal Ratio Tree(数据结构-最小生成树)

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a

Codeforces 480B Long Jumps 规律题

题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 下面n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x , 某2个刻度的距离为y 要添加最少几个刻度. 问: 最少的刻度个数 输出标记的位置. 思路: 分类讨论一下.. 若本身尺子里就有x.y就输出0 若只有x 或只有y就输出一个刻度. 若2个都没有就: 1.加1个刻度ans,这个ans是距离某个刻度距离为x的,然后看一下是否有距离ans为y的刻度,若有

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

一.实验目的 1.掌握建立MFC应用程序的方法: 2.掌握映射模式. 二.实验内容 1.在MFC中绘制直尺,直尺需要有刻度,类似于日常学生使用的透明塑料直尺,需要建立四个直尺,分别分布在屏幕客户区的上.下.左.右四个边界.尺子需要有刻度,那客户区上端的尺子距离,应该有厘米.5毫米.1毫米刻度,刻度用竖线显示,长度分别为7毫米.6毫米.5毫米,外观类似于学生直尺,右端留出一公分,防止4个尺子碰在一起. 2.画出一坐标系,给出x坐标变化范围.y坐标变化范围,画出坐标轴,并在坐标轴上标出刻度.原点,要