HDOJ 4277 USACO ORZ 搜索+剪枝
2015-09-07 22:40
253 查看
暴力搜索+轻微剪枝
固定第一个数的话可以减少2/3的时间…..
Total Submission(s): 4018 Accepted Submission(s): 1340
Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments.
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
Output
For each test case, output one integer indicating the number of different pastures.
Sample Input
1
3
2 3 4
Sample Output
1
Source
2012 ACM/ICPC Asia Regional Changchun Online
固定第一个数的话可以减少2/3的时间…..
USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4018 Accepted Submission(s): 1340
Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments.
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
Output
For each test case, output one integer indicating the number of different pastures.
Sample Input
1
3
2 3 4
Sample Output
1
Source
2012 ACM/ICPC Asia Regional Changchun Online
/* *********************************************** Author :CKboss Created Time :2015年09月07日 星期一 21时39分52秒 File Name :HDOJ4277.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; int n,sum; int a[20]; int seg[3]; int tmp[3]; set<int> st; void check() { for(int i=0;i<3;i++) tmp[i]=seg[i]; sort(tmp,tmp+3); if(tmp[0]+tmp[1]>tmp[2]) st.insert(tmp[0]*160000+tmp[1]); //if(st.count(tmp[0]*300000+tmp[1])==0) cout<<tmp[0]<<","<<tmp[1]<<","<<tmp[2]<<endl; } void dfs(int x) { if(x==n) { check(); return ; } for(int i=0;i<3;i++) { seg[i]+=a[x]; if(seg[i]*2<sum) dfs(x+1); seg[i]-=a[x]; } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d",&n); sum=0; for(int i=0;i<n;i++) { scanf("%d",a+i); sum+=a[i]; } sort(a,a+n); memset(seg,0,sizeof(seg)); st.clear(); seg[0]=a[0]; dfs(1); cout<<st.size()<<endl; } return 0; }
相关文章推荐
- codeforces 21A Jabber ID
- 数组和集合小总结
- print操作及重定向
- 给grub加密码
- 函数和方法
- SQL 中关于groupby的信息
- WCFRestFull空框架,套上业务逻辑实现就可以用了
- iOS开发 - 汉字转拼音
- uva 442 矩阵链乘
- Qt 信号槽机制解析二 分析原代码
- hdu 5001 Walk(概率dp+搜索,矩阵快速幂)
- ZOJ 3687 The Review Plan I(禁位排列)
- Java中的容器
- 常见算法笔试题
- Volley初次尝试
- surfaceView和View最本质的区别
- surfaceView和View最本质的区别
- myeclipse中配置mybatis的自动提示
- 关于Oracle修改IP地址引起的问题
- 刚安装的tomcat在 myeclipse中第一次启动时报错:Address already in use: JVM_Bind<null>:8080