by sohel » Mon Nov 08, 2004 12:47 pm
I am also using the technique Verdan used and also made ammendments to my code with the modification that little joey added,
but I am getting TLE.
I will post my code here, cos it's relatively small..
btw I am with those who are against 'posting of codes' but finding no other alternative, I resorted to it.
[c]
// Name : Cat
// Number : 10715
// Author : Sohel Hafiz
// Date : 2004-11-08
// Algorithm : Backtracking
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
double d;
double r;
int index;
};
node N[105];
int t;
int A[105];
int yes;
void dfs(int u, int d, double sum) {
if(yes) return;
if( sum > 0.504 ) return;
int i;
if( sum >= 0.496 && sum <= 0.504) {
for(i=0;i<d;i++) {
if( i ) printf(" ");
printf("%d", A[i] + 1);
}
printf("\n");
yes = 1;
return;
}
if( u == t) return;
A[d] = N[u].index ;
dfs(u+1, d+1, sum + N[u].r);
dfs(u+1, d, sum);
}
bool comp(const node &a, const node &b) {
return a.r > b.r;
}
int main() {
int i;
double total;
while(1) {
scanf("%d",&t);
total = 0;
if( t == 0 ) break;
for(i=0;i<t;i++) {
scanf("%lf", &N[i].d);
total += N[i].d;
N[i].index = i;
}
for(i=0;i<t;i++) {
N[i].r = N[i].d / total;
}
sort( N, N + t, comp);
yes = 0;
dfs(0, 0, 0);
}
return 0;
}
[/c]
Some help will be appreciated.