HDOJ-1557(枚举或搜索)
来源:互联网 发布:微盘交易源码 阴极铜 编辑:程序博客网 时间:2024/06/07 04:48
题目中每个团体加入或不加入联盟一共有2^20种可能性,数据规模很小,直接枚举即可:
#include <stdio.h>int main(){ int test, i, k, limit, mask, sum; int N, arr[20], cnt[20], threshold; scanf("%d", &test); while(test--){ threshold = 0; scanf("%d", &N); for(i = 0; i < N; ++i){ scanf("%d", &arr[i]); threshold += arr[i]; cnt[i] = 0; } threshold = threshold / 2 + 1; for(k = 1, limit = 1 << N; k < limit; ++k){ sum = 0; for(i = 0; (mask = 1 << i) <= k; ++i){ if(k & mask) sum += arr[i]; } if(sum < threshold) continue; for(i = 0; (mask = 1 << i) <= k; ++i){ if((k & mask) && sum - arr[i] < threshold) ++cnt[i]; } } printf("%d", cnt[0]); for(i = 1; i < N; ++i) printf(" %d", cnt[i]); puts(""); } return 0;}用DFS代替手动枚举还可以写成:
#include <stdio.h>int N, arr[20], cnt[20], threshold;int stack[20], size;void dfs(int i, int sum){ if(i < 0){ if(sum >= threshold){ int k = 0; for(; k < size; ++k){ if(sum - arr[stack[k]] < threshold) ++cnt[stack[k]]; } } return; } dfs(i - 1, sum); stack[size++] = i; dfs(i - 1, sum + arr[i]); --size;}int main(){ int test, i; scanf("%d", &test); while(test--){ threshold = 0; scanf("%d", &N); for(i = 0; i < N; ++i){ scanf("%d", &arr[i]); threshold += arr[i]; cnt[i] = 0; } threshold = threshold / 2 + 1; size = 0; dfs(N-1, 0); printf("%d", cnt[0]); for(i = 1; i < N; ++i) printf(" %d", cnt[i]); puts(""); } return 0;}上面不加剪枝的DFS和枚举实际上都是遍历所有的可能性,但从结果上来看,DFS还是快一些,毕竟枚举的时候每次都还要扫描2遍全部团体是否加入,而DFS时已经将这一信息记录下来了
0 0
- HDOJ-1557(枚举或搜索)
- HDU/HDOJ 1015(dfs或暴力枚举)
- 搜索(枚举) 1
- 搜索(枚举)2
- 搜索(枚举)3
- UVALive 6432 In uence(枚举+dfs或记忆化搜索+bitset)
- HDoj 搜索 (转)留着用
- 折半枚举(双向搜索)
- noip1997 三角形 (搜索,枚举)
- 折半枚举(双向搜索)
- HDOJ 题目1576 A/B(枚举)
- hdoj--5625--Clarke and chemistry(枚举)
- hdoj 1015 safecracker(暴力枚举)
- SDUTOJ 2773 小P的故事——神奇的Dota 枚举或背包或搜索
- poj 2362 hdoj 1518 Square(搜索)
- hdoj--1495--非常可乐(搜索+隐式图)
- HDOJ-3791(二叉搜索树)
- hdoj 1254 推箱子(双重搜索)
- Jade模板引擎入门教程
- MFC vfw视频捕获
- 浅谈网站关键词选取的规则和注意事项
- Hide C# winform App Window When Started by Task Scheduler
- 黑马程序员——————————面向对象5
- HDOJ-1557(枚举或搜索)
- 黑马程序员——————————多线程
- 分享我的Linux使用经验
- 设计模式——装饰模式
- 05 - 数据操作一:文件读写与XML解析、SharedPreferences
- 笔记本在Win7/Win8/win8.1下安装OS X 10.9.3懒人版通用教程
- STM32串口一直进中断解决方法
- Linux安装JDK详细步骤
- C语言控制台窗口图形界面编程(五):文本移动