minimal sparse ruler problem 最少尺子刻度问题
一个长度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 最少尺子刻度问题的更多相关文章
- [dfs] UVALive 3667 Ruler
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://ic ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- 【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- Object Tracking Benchmark
Abstract 问题: 1)evaluation is often not suffcient 2)biased for certain types of algorthms 3)datasets ...
- MFC图形绘制——绘制直尺和坐标系
一.实验目的 1.掌握建立MFC应用程序的方法: 2.掌握映射模式. 二.实验内容 1.在MFC中绘制直尺,直尺需要有刻度,类似于日常学生使用的透明塑料直尺,需要建立四个直尺,分别分布在屏幕客户区的上 ...
- Codeforces 480B Long Jumps 规律题
题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 以下n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x ...
随机推荐
- php实现函数可变参数列表
使用func_get_args().func_num_args().func_get_arg() 可以构造一个可变参数列表的函数. 首先大致介绍以上三个函数. (1)array func_get_ar ...
- shiro学习笔记_0500_授权
1,授权:给身份认证通过的人,授予他可以访问某些资源的权限. 2,权限粒度:分为粗粒度和细粒度. 粗粒度:例如对 user 的 crud,也就是通常所说的对表的操作. 细粒度:对表中记录的操作.如 只 ...
- com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote method解决方法
报错日记: Caused by: com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote method: getUserAuthLeve ...
- python-多进程类封装
#!/usr/bin/python import multiprocessing,time class ClockProcess(multiprocessing.Process): def __ini ...
- linux mint19 解决docker必须使用sudo问题
1 安装完docker 使用时,提示权限不够 ~$ docker info Got permission denied while trying to connect to the Docker da ...
- 创建第一个WCF服务
创建WCF服务 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序. 2.建立完成后如下图所示: 3.删除系统生成的两个文件IService1.cs与Service1.s ...
- C++11 并发(一道笔试题目)
题目:编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. #include < ...
- 图解ARP协议(四)代理ARP原理与实践(“善意的欺骗”)
一.代理ARP概述 我:当电脑要访问互联网上的服务器,目标MAC是什么? 很多小伙伴在刚学习网络协议的时候,经常这样直接回应:不就是服务器的MAC嘛! 这时我会反问:那电脑怎么拿到这个服务器的MAC地 ...
- js 获取当前标签 jquery1.11.4
.<input type="checkbox" onchange='allstu(this);return false;' /> 2.<input type=&q ...
- PHP的一些语句 if...else...elseif - Switch - while - for
条件语句用于基于不同条件执行不同的动作 PHP 条件语句 在您编写代码时,经常会希望为不同的决定执行不同的动作.您可以在代码中使用条件语句来实现这一 点. 在 PHP 中,我们可以使用以下条件语句: ...