Minimal sparse ruler problem minimum ruler scale problem

Source: Internet
Author: User

A ruler of length 13, if the 1-bit point can be 1 and 12, 133 kinds of scale. So at least a few points, you can directly measure the length of 1-13, which are engraved in which positions?

Note: Must be a direct quantity. That is, the ruler can find a 1-13 arbitrary integer length.

Wrote a no-tech solution to DFS violence. A viable solution is 1, 2, 6, 10.

1#include <iostream>2#include <vector>3#include <unordered_map>4 using namespacestd;5 6 classSolution {7  Public:8vector<vector<int>> Ruler (intN, vector<int> &Minpath) {9Dfs0, N, Minpath);Ten         returnresult; One     } Avector<vector<int>>result; -vector<int>path; -     voidDfsintStartintN, vector<int> &Minpath) { the         if(Start = = n +1) { -             if(Fullscale (path)) { - result.push_back (path); -                 if(Path.size () <Minlen) { +Minlen =path.size (); -Minpath =path; +                 } A             } at             return; -         } -          for(inti = start; I <= N; i++) { - Path.push_back (i); -DFS (i +1, N, Minpath); - Path.pop_back (); in         } -     } to     BOOLFullscale (vector<int>path) { +         if(Path.size () <4) { -             return false; the         } *unordered_map<int,int>Umap; $umap[ -]++;Panax Notoginsengumap[0]++; -          for(inti =0; I < path.size (); i++) { the              for(intj =0; J < I; J + +) { +                 if(Path[i]-PATH[J] < -) { AUmap[path[i]-path[j]]++; theumap[path[j]]++; +umap[path[i]]++; -umap[ --path[i]]++; $umap[ --path[j]]++; $                 } -                 if(Umap.size () >= -) { -                     return true; the                 } -             }Wuyi         } the         return false; -     } Wu Private: -     intMinlen = -; About }; $  - intMain () { -     intn = -; - solution Solu; Avector<int>Minpath; +vector<vector<int>> res =Solu.ruler (n, minpath); the      for(Auto X:minpath) { -cout << x <<", "; $     } the}

Ref:https://en.wikipedia.org/wiki/sparse_ruler

Minimal sparse ruler problem minimum ruler scale problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.