USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 765 Accepted Submission(s): 253
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.
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)
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
1 //爆搜+set判重 ,实际还是hash判重,map判重有两个参数 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 using namespace std; 6 7 8 int n; 9 int arr[20]; 10 set <long long> sset; 11 12 void dfs(int a, int b, int c, int cur) 13 { 14 if (cur==n) 15 { 16 if (a>b||b>c||a>c) return; 17 if (a&&b&&c&&a+b>c) 18 { 19 long long t = 1000000000000LL*a+1000000LL*b+c; 20 sset.insert(t); 21 } 22 return; 23 } 24 dfs(a+arr[cur],b,c,cur+1); 25 dfs(a,b+arr[cur],c,cur+1); 26 dfs(a,b,c+arr[cur],cur+1); 27 } 28 29 int main() 30 { 31 int i,j,k,T; 32 cin>>T; 33 while(T--) 34 { 35 sset.clear(); 36 cin>>n; 37 for(i = 0 ; i < n ; i++ ) 38 cin>>arr[i]; 39 dfs(0,0,0,0); 40 cout<<sset.size()<<endl; 41 } 42 return 0; 43 }
1 //wa,测试没过 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 using namespace std; 6 7 8 int n; 9 int arr[20]; 10 set <long long> sset; 11 12 bool is_tir(int a, int b, int c) 13 { 14 int p = (a+b+c)>>1; 15 if(p>a&&p>b&&p>c) 16 return 1; 17 return 0; 18 } 19 20 void dfs(int a, int b, int c, int cur) 21 { 22 if (cur==n) 23 { 24 if (is_tir(a,b,c)) 25 { 26 long long t = 1000000000000LL*a+1000000LL*b+c; 27 sset.insert(t); 28 } 29 else 30 return; 31 } 32 dfs(a+arr[cur],b,c,cur+1); 33 dfs(a,b+arr[cur],c,cur+1); 34 dfs(a,b,c+arr[cur],cur+1); 35 } 36 37 int main() 38 { 39 int i,j,k,T; 40 cin>>T; 41 while(T--) 42 { 43 sset.clear(); 44 cin>>n; 45 for(i = 0 ; i < n ; i++ ) 46 cin>>arr[i]; 47 dfs(0,0,0,0); 48 cout<<sset.size()<<endl; 49 } 50 return 0; 51 }
1 //AC ,自己写的 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 using namespace std; 6 7 8 int n,sum; 9 int arr[20]; 10 set <long long> sset; 11 12 bool is_tir(int a, int b, int c) 13 { 14 int p = (sum+1)>>1;//必须加上1 ,想想2 3 4 15 if(p>a&&p>b&&p>c) 16 return 1; 17 return 0; 18 } 19 20 void dfs(int a, int b, int c, int cur) 21 { 22 if (cur==n) 23 { 24 if (is_tir(a,b,c)) 25 { 26 if(a>b||b>c||a>c) 27 return ; 28 long long t = 1000000000000LL*a+1000000LL*b+c; 29 sset.insert(t); 30 } 31 else 32 return; 33 } 34 else//ce(应该是下标越上界),加上else后就tle 35 { 36 dfs(a+arr[cur],b,c,cur+1); 37 dfs(a,b+arr[cur],c,cur+1); 38 dfs(a,b,c+arr[cur],cur+1); 39 } 40 } 41 42 int main() 43 { 44 int i,j,k,T; 45 cin>>T; 46 while(T--) 47 { 48 sum = 0; 49 sset.clear(); 50 cin>>n; 51 for(i = 0 ; i < n ; i++ ) 52 { 53 cin>>arr[i]; 54 sum += arr[i]; 55 } 56 dfs(0,0,0,0); 57 cout<<sset.size()<<endl; 58 } 59 return 0; 60 }
View Code
1 //TLE 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 using namespace std; 6 7 8 int n,sum; 9 int arr[20]; 10 set <long long> sset; 11 12 bool is_tir(int a, int b, int c) 13 { 14 int p = (sum+1)>>1; 15 if(p>a&&p>b&&p>c) 16 return 1; 17 return 0; 18 } 19 20 void dfs(int a, int b, int c, int cur) 21 { 22 if (cur==n) 23 { 24 if (is_tir(a,b,c)) 25 { 26 if(a>b) 27 swap(a,b); 28 if(a>c) 29 swap(a,c); 30 if(b>c) 31 swap(b,c); 32 long long t = 1000000000000LL*a+1000000LL*b+c; 33 sset.insert(t); 34 } 35 else 36 return; 37 } 38 else//ce(应该是下标越上界),加上else后就tle 39 { 40 dfs(a+arr[cur],b,c,cur+1); 41 dfs(a,b+arr[cur],c,cur+1); 42 dfs(a,b,c+arr[cur],cur+1); 43 } 44 } 45 46 int main() 47 { 48 int i,j,k,T; 49 cin>>T; 50 while(T--) 51 { 52 sum = 0; 53 sset.clear(); 54 cin>>n; 55 for(i = 0 ; i < n ; i++ ) 56 { 57 cin>>arr[i]; 58 sum += arr[i]; 59 } 60 dfs(0,0,0,0); 61 cout<<sset.size()<<endl; 62 } 63 return 0; 64 }
//小熊的位运算枚举超时算法,还不太懂 // Note:Your choice is C++ IDE #include <iostream> #include <set> #include <algorithm> using namespace std; struct Side{ long s[16]; long cnt; Side():cnt(0){memset(s, 0, sizeof s);} }; long val[16]; long sid1, sid2, sid3; long n; set<long> st; void Insert() { long hashVal = sid1*100000000 + sid2*10000 + sid3; if(st.find(hashVal) == st.end()) { st.insert(hashVal); } } int main() { // freopen("sjx.in", "r", stdin); long i, j, t; long m, sum; cin>>t; while(t--) { st.clear(); cin >> n; sum = 0; for(int ii=0; ii<n; ii++) { cin>>val[ii]; sum += val[ii]; } m = (1<<n) - 1;//n个1的二进制 Side sd1; for(i=1; i<m; i++) { sd1.cnt = 0; sid1 = 0; for(long k=0; k<n; k++) { if((i>>k)&1) { sd1.s[sd1.cnt++] = val[k]; } else { sid1 += val[k]; } } double three = sum/3.0; if((double)sid1 >= three) { long n = (1<<sd1.cnt) - 1; for(j=1; j<n; j++) { sid2 = sid3 = 0; for(long k=0; k<sd1.cnt; k++) { if((j>>k)&1) { sid2 += sd1.s[k]; if(sid2 > sid1) goto Imp; } else { sid3 += sd1.s[k]; if((double)sid3 > three) goto Imp; } } if(sid3 <= sid2 //&& sid2 <= sid1 && sid3+sid2 > sid1) Insert(); Imp:; } } } cout<<st.size()<<endl;//<<","<<clock()<<endl; } //while(1); return 0; }