题目链接:

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&category=0" style="color:rgb(33,93,198)">Root

option=com_onlinejudge&Itemid=8&page=submit_problem&problemid=1668&category=" style="color:rgb(33,93,198)">  



Regionals 2006 >> 

option=com_onlinejudge&Itemid=8&category=242" style="color:rgb(33,93,198)">Asia
- Beijing

3667 - Ruler

Time limit: 3.000 seconds

题目意思:

有n个长度须要量。问如何设计尺子刻度,使得每一个长度都在两个刻度之间。在满足刻度数最小的情况下。要求尺子长度越短越好,最開始的刻度为0.

解题思路:

dfs

由于最多的刻度是7,并且可以计算出最小的刻度数。由于当刻度数m确定后。最多可以測量的长度数是固定的C(m,2).

要满足刻度最少且长度最短,能够如果最后一个刻度在长度最长的位置。

然后递增暴搜。

代码:

  1. //#include<CSpreadSheet.h>
  2.  
  3. #include<iostream>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<sstream>
  7. #include<cstdlib>
  8. #include<string>
  9. #include<string.h>
  10. #include<cstring>
  11. #include<algorithm>
  12. #include<vector>
  13. #include<map>
  14. #include<set>
  15. #include<stack>
  16. #include<list>
  17. #include<queue>
  18. #include<ctime>
  19. #include<bitset>
  20. #include<cmath>
  21. #define eps 1e-6
  22. #define INF 0x3f3f3f3f
  23. #define PI acos(-1.0)
  24. #define ll __int64
  25. #define LL long long
  26. #define lson l,m,(rt<<1)
  27. #define rson m+1,r,(rt<<1)|1
  28. #define M 1000000007
  29. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  30. using namespace std;
  31.  
  32. #define Maxn 55
  33. #define Maxm 1100000
  34. int hav[Maxm],sa[Maxn],n,ans;
  35. bool vis[Maxn];
  36. int dis[Maxn];
  37.  
  38. bool dfs(int cur)
  39. {
  40. if(cur==ans)
  41. {
  42. for(int i=1;i<n;i++) //前n-1个长度都可以測量
  43. if(!vis[i])
  44. return false;
  45. return true;
  46. }
  47. for(int i=1;i<cur;i++)
  48. {
  49. for(int j=1;j<n;j++)
  50. {
  51. if(!vis[j])
  52. {
  53. int dd=dis[i]+sa[j];//当前刻度
  54. if(dd<=dis[cur-1]) //比之前大
  55. continue;
  56. if(dd>=sa[n])//要比最大小
  57. continue;
  58. dis[cur]=dd;
  59.  
  60. queue<int>myq; //记录标记的长度。回溯时返回
  61. for(int k=1;k<cur;k++) //增加当前刻度后,新增的可以出的长度
  62. {
  63. int temp=dis[cur]-dis[k];
  64. if(hav[temp]&&!vis[hav[temp]])
  65. {
  66. vis[hav[temp]]=true;
  67. myq.push(hav[temp]);
  68. }
  69. }
  70. int la=sa[n]-dis[cur]; //最后一段
  71. if(hav[la]&&!vis[hav[la]])
  72. {
  73. vis[hav[la]]=true;
  74. myq.push(hav[la]);
  75. }
  76. if(dfs(cur+1))
  77. return true;
  78. while(!myq.empty())
  79. {
  80. la=myq.front();
  81. myq.pop();
  82. vis[la]=false;
  83. }
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. int main()
  90. {
  91. //freopen("in.txt","r",stdin);
  92. //freopen("out.txt","w",stdout);
  93. int cas=0;
  94.  
  95. while(~scanf("%d",&n)&&n)
  96. {
  97. for(int i=1;i<=n;i++)
  98. scanf("%d",&sa[i]);
  99. sort(sa+1,sa+n+1);
  100. n=unique(sa+1,sa+n+1)-sa-1;
  101. memset(hav,0,sizeof(hav));
  102. for(int i=1;i<=n;i++)
  103. hav[sa[i]]=i;
  104.  
  105. dis[1]=0;
  106. ans=2;
  107. while(ans*(ans-1)/2<n)
  108. ans++;
  109.  
  110. memset(vis,0,sizeof(vis));
  111. while(!dfs(2))
  112. ans++;
  113.  
  114. printf("Case %d:\n%d\n",++cas,ans);
  115. printf("%d",dis[1]);
  116. dis[ans]=sa[n];
  117.  
  118. for(int i=2;i<=ans;i++)
  119. printf(" %d",dis[i]);
  120. putchar('\n');
  121.  
  122. }
  123. return 0;
  124. }

[dfs] UVALive 3667 Ruler的更多相关文章

  1. LA 3667 Ruler 搜索

    题意: 给出\(n\)个长度,要设计一个有\(m\)个刻度的刻度尺,刻度尺的刻度从\(0\)开始. 使得任意一个长度都能被该刻度尺度量出来. 首先要使\(m\)最小,在\(m\)最小的前提下尺子的长度 ...

  2. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  3. UVALive - 6436、HYSBZ - 2435 (dfs)

    这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做 道路修建(HYSBZ - 2435) 在 W 星球上有n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道 ...

  4. UVALive 4998 Simple Encryption --DFS

    题意: 给出K1,求一个12位数(不含前导0)K2,使得K1^K2 mod (10^12) = K2. 解法: 求不动点问题. 有一个性质: 如果12位数K2满足如上式子的话,那么K2%1,K2%10 ...

  5. UVALive 4997 ABCD Tiles --DFS

    题意: NxN的地图,上面有A颜色的瓷砖以及一些空格点,要用B,C,D颜色去填充这些空格,只能十字形的填充,还要保证共角或共边的格子不能是相同颜色,求一种字典序最小的填充方法,如果不能,输出" ...

  6. UVALive 6663 Count the Regions --离散化+DFS染色

    题意:给你n(n<=50)个矩形(左上角坐标和右下角坐标),问这些矩形总共将平面分成多少个部分.坐标值可能有1e9. 分析:看到n和坐标的范围,容易想到离散化,当时就没想到离散化以后怎么判断区域 ...

  7. UVALive 6450 Social Advertising DFS解法

    题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...

  8. UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)

    题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有 ...

  9. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

随机推荐

  1. CSS布局整理

    目录 常用居中方法 水平居中 垂直居中 单列布局 二列&三列布局 float+margin position+margin 圣杯布局(float+负margin) 双飞翼布局(float+负m ...

  2. Java:一个简捷的可分页的ResultSet实现

    内容 前言 JDBC和分页 和具体数据库相关的实现方法 另一种繁琐的实现方法 使用Vector进行分页 一个新的Pageable接口及其实现 Pageable的使用方法 总结 参考资料 关于作者 前言 ...

  3. Masonry基础API

    Masonry基础API mas_makeConstraints()    添加约束 mas_remakeConstraints()  移除之前的约束,重新添加新的约束 mas_updateConst ...

  4. 执行join_paired_ends.py报错Cannot find fastq-join

    通过 conda 安装 qiime 1后,在执行join_paired_ends.py时报错: burrito.util.ApplicationNotFoundError: Cannot find f ...

  5. 搭建FileZilla

    FileZilla是C/S架构的,有服务端和客户端 客户端下载地址https://www.filezilla.cn/download/client 安装,一般就下一步下一步了. 服务端下载:https ...

  6. c++类简介

    C++类(Class)总结   一.C++类的定义     C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性  protected: // ...

  7. CAD得到0层上的所有实体(com接口VB语言)

    主要用到函数说明: IMxDrawSelectionSet::Select 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARIA ...

  8. ThinkPHP---案例1登录登出和添加部门

    配置文件分3类:系统配置文件,分组配置文件,应用配置文件 ①系统配置文件ThinkPHP/Conf/convention.php: ②分组 / 模块 /平台配置文件Home/Conf/config.p ...

  9. 这段代码很Pythonic | 相见恨晚的 itertools 库

    前言 最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更 ...

  10. Extjs中Store小总结

    http://blog.csdn.net/without0815/article/details/7798170 1.什么是store? Store类似于一个本地仓库(即数据存储器),包括有 Arra ...