UVALive - 3667 Ruler
来源:互联网 发布:淘宝怎么设置客服旺旺 编辑:程序博客网 时间:2024/06/09 01:31
题意:给出n种距离,设计一个有m个刻度的尺子,使得每个刻度都可以直接量出,要求在m尽量小的情况下尺子的总长度尽量短,第一个必须是0,输出保证m<=7
思路:首先先确定出最小的m,Cm(2)解出可能的最小的m,那么接着开始枚举m,每次得到的一个解都是有给出的n种距离和已有的解的出来的,那么这个解又能得到与之前得出来的解的差和与最大尺度差的两种解,还有几个地方需要减枝,比如解是严格的递增的,还有如果超过最大的尺度那么这个解就是不合格的,回溯处理,还要注意必须是出现的才能用来组成解
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int n,d[55],ans,a[8];int cnt[1000005],vis[55];bool dfs(int u){ if (u == ans){ for (int i = 1; i <= n-1; i++) if (!vis[i]) return 0; return 1; } for (int i = 1; i <= u-1; i++) for (int j = 1; j <= n-1; j++) if (!vis[j]){ int dd = a[i] + d[j]; if (dd <= a[u-1]) continue; if (dd >= d[n]) continue; a[u] = dd; queue<int> q; for (int k = 1; k <= u-1; k++){ dd = a[u] - a[k]; if (cnt[dd] && !vis[cnt[dd]]){ vis[cnt[dd]] = 1; q.push(cnt[dd]); } } dd = d[n] - a[u]; if (cnt[dd] && !vis[cnt[dd]]){ vis[cnt[dd]] = 1; q.push(cnt[dd]); } if (dfs(u+1)) return 1; while (!q.empty()){ vis[q.front()] = 0; q.pop(); } } return 0;}int main(){ int t = 0; while (scanf("%d",&n) != EOF && n){ for (int i = 1; i <= n; i++) scanf("%d",&d[i]); sort(d+1,d+1+n); n = unique(d+1,d+1+n) - d - 1; memset(cnt,0,sizeof(cnt)); for (int i = 1; i <= n; i++) cnt[d[i]] = i; ans = 2; while (ans*(ans-1)/2 < n) ans++; a[1] = 0; memset(vis,0,sizeof(vis)); while (!dfs(2)) ans++; printf("Case %d:\n",++t); printf("%d\n",ans); a[ans] = d[n]; for (int i = 1; i <= ans; i++) printf("%d ",a[i]); puts(""); } return 0;}
0 0
- UVALive - 3667 Ruler
- [dfs] UVALive 3667 Ruler
- UVALive 3667 Ruler (模拟)
- UVALive 3667 Ruler(回溯搜索)
- LA 3667 Ruler
- la 3667 ruler
- LA 3667 Ruler IDA* .
- Golomb ruler
- uvalive3667 ruler
- LA 3667 Ruler 两种不同形式的搜索来解决
- uva 1377 - Ruler(BFS)
- (intermediate) UVA 1377 - Ruler
- 递归 example ruler
- UVA 1377 Ruler(dfs)
- 金额刻度尺Ruler
- uvalive 3667(dfs)
- UVALive 3667 dfs
- Transportation HDU 3667 UVALive
- iOS:”NSURL中使用string拼接有时候会使NSURL成nil“解决方案
- zookeeper的使用场景
- cocos2d-x + TiledMap 详解
- quartz spring 版本兼容问题
- Socket之shutdown()用法
- UVALive - 3667 Ruler
- SRM 555
- CSDN 博客没有千里之外了,而是显示了排名
- poj 3176 Cow Bowling 数塔dp
- Chrome 浏览器 extension 指南
- LA 3644 X-Plosives / 并查集
- Java中的Calendar方法
- 实时开发框架Meteor API解读系列<二>Core
- 七款代码味道识别工具【简介】