Hi,
I am trying very hard to understand the logic/theory of the solution to the
problem TopCoder "Jewelry - 2003 TCO Online Round 4". Here is the problem
statement:-
Problem Statement
You have been given a list of jewelry items that must be split amongst
two people: Frank and Bob. Frank likes very expensive jewelry. Bob doesn't
care how expensive the jewelry is, as long as he gets a lot of jewelry.
Based on these criteria you have devised the following policy: •1) Each
piece of jewelry given to Frank must be valued greater than or equal to
each piece of jewelry given to Bob. In other words, Frank's least expensive
piece of jewelry must be valued greater than or equal to Bob's most
expensive piece of jewelry.
•2) The total value of the jewelry given to Frank must exactly equal the
total value of the jewelry given to Bob.
•3) There can be pieces of jewelry given to neither Bob nor Frank.
•4) Frank and Bob must each get at least 1 piece of jewelry.
Given the value of each piece, you will determine the number of different
ways you can allocate the jewelry to Bob and Frank following the above
policy. For example: values = {1,2,5,3,4,5}
Valid allocations are: Bob Frank
1,2 3
1,3 4
1,4 5 (first 5)
1,4 5 (second 5)
2,3 5 (first 5)
2,3 5 (second 5)
5 (first 5) 5 (second 5)
5 (second 5) 5 (first 5)
1,2,3,4 5,5
Note that each '5' is a different piece of jewelry and needs to be
accounted for separately. There are 9 legal ways of allocating the jewelry
to Bob and Frank given the policy, so your method would return 9.
Here is the C++ solution:-
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define FOREACH(i,c) for(__typeof((c).begin())
i=(c).begin();i!=(c).end();++i)
typedef long long LL;
const int INF = 1000000000;
typedef vector<int> VI;
template<class T> inline int size(const T&c) { return c.size(); }
char buf1[1000];
string i2s(int x) {
sprintf(buf1,"%d",x);
return buf1;
}
const int MAXN = 30;
const int MAX = 30000;
int n;
VI v;
LL B[MAXN+1][MAX+1]; // [n pocz][sum]
LL F[MAXN+1][MAX+1];
LL nk[MAXN+1][MAXN+1];
void cnk() {
nk[0][0]=1;
FOR(k,1,MAXN) nk[0][k]=0;
FOR(n,1,MAXN) {
nk[n][0]=1;
FOR(k,1,MAXN) nk[n][k] = nk[n-1][k-1]+nk[n-1][k];
}
}
void calc(LL T[MAXN+1][MAX+1]) {
T[0][0] = 1;
FOR(x,1,MAX) T[0][x]=0;
FOR(ile,1,n) {
int a = v[ile-1];
FOR(x,0,MAX) {
T[ile][x] = T[ile-1][x];
if(x>=a) T[ile][x] +=T[ile-1][x-a];
}
}
}
struct Jewelry {
// MAIN
long long howMany(vector <int> vv) {
v = vv;
n = size(v);
cnk();
sort(v.begin(),v.end()); //,greater<int>());
calc(F);
sort(v.begin(),v.end());
calc(B);
LL res = 0;
int done=0;
while(done<n) {
int p=done;
while(p<n && v[p]==v[done]) ++p;
int c=p-done;
FOR(u,1,c) {
int uu = u * v[done];
FOR(x,uu,MAX) {
res += B[done][x-uu] * F[n-done-u][x] * nk[c][u];
}
}
done=p;
}
return res;
}
};
Can some genius DP programmer make me understand the logic of this
solution??
Regards,
Rajesh Kumar
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/CALBPbNH_J2qV7chRLABDb0NKe%3DQeyo_LfA7U3bcQJvw9g3j91A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.