Usaco ORZTime
limit:5000/1500 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3809 Accepted Submission (s): 1264
Problem descriptionlike Everyone, cows enjoy variety. Their current fancy are new shapes for pastures. The old rectangular shapes is out of favor; New geometries is the favorite.
I. M. Hei, the lead cow pasture architect, was in charge of creating a triangular pasture surrounded by nice white fence RA Ils. 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.
Pastures look different if at least one side of both pastures have different lengths, and each pasture should not being de Generation.
Inputthe first line was an integer T (t<=15) indicating the number of test cases.
The first line of all 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)
Outputfor each test case, output one integer indicating the number of different pastures.
Sample Input
132 3 4
Sample Output
1
Source2012 ACM/ICPC Asia Regional Changchun Online
Test instructions: give you n edge, form a triangle, the number of triangles to be composed. Two triangles three sides of the same count.
Puzzle: Violence enumerates the first edge, then two-way enumeration of the second side, with map to weight.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < string> #include <map> #include <cstdlib> #include <cmath> #include <vector> #include <set > #include <queue> #define MP (x, y) make_pair (x, Y) using namespace std;typedef pair<int,int > ppi;map< ppi,int> mp;int a[20];int b[20];int sum;int all;int bn;int ans;int smallest;int largest;void dfs (int i,int csum) {i F (csum>smallest) return; if (Csum!=0&&all-csum<=largest&&mp.find (MP (Csum,all-csum)) ==mp.end ()) {///founding && weighing ans++ ; MP[MP (csum,all-csum)]=1; } if (i==bn) return; DFS (I+1,csum+b[i]); DFS (i+1,csum);} int main () {int n; int t; scanf ("%d", &t); while (t--) {mp.clear (); scanf ("%d", &n); sum=0; ans=0; for (int i=0; i<n; i++) {scanf ("%d", a+i); Sum+=a[i]; } int m= (1<<n)-1; for (int i=1; i<m; i++) {///enum first edge all=0; bn=0; for (int j=0; j<n; J + +) {///second and third side and if ((1<<J) &i) {all+=a[j]; B[BN++]=A[J]; }} Largest=sum-all; if (Largest>=all) continue; SMALLEST=ALL/2; DFS (0,0); } printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4277 Usaco ORZ (brute Force + bidirectional enumeration)