Moderator: Board moderators
mafattah wrote:Anybody knows a solution for problem J in the last ICPC World Finals that is more efficient than O(n^2 * 2^n)? Namely, checking all possible subsets?
mafattah wrote:Also, does anybody know how much vectors are less efficient than arrays, in case I only use .size() and operator[]?
#include <stdio.h>
int sol,best,n,k;
int c[20];
int comm[10][2],t;
int nbits[1<<20];
int getsign(int a) {
if (nbits[a] == 1)
return 0;
if (nbits[a]&1)
return 1;
return -1;
}
void doit(int pos, int sel, int bitsel, int sum) {
if (n-pos<k-sel)
return;
if (k == sel) {
for (int i=0; i<t; i++)
if (comm[i][0]&bitsel)
sum += getsign(comm[i][0]&bitsel)*comm[i][1];
if (best<sum) {
best = sum;
sol = bitsel;
}
return;
}
doit(pos+1,sel+1,bitsel|(1<<pos),sum+c[pos]);
doit(pos+1,sel,bitsel,sum);
}
int cnt_bits(int a) {
if (!a)
return 0;
if (nbits[a])
return nbits[a];
return nbits[a] = cnt_bits(a>>1)+(a&1);
}
int main() {
int scen = 0;
for (int i=(1<<20)-1; i>=0; i--)
cnt_bits(i);
while(scanf("%d %d",&n,&k)==2 && (n+k)) {
for (int i=0; i<n; i++)
scanf("%d",&c[i]);
scanf("%d",&t);
for (int i=0; i<t; i++) {
comm[i][0] = 0;
int cnt;
scanf("%d",&cnt);
for (int j=0; j<cnt; j++) {
int b;
scanf("%d",&b);
comm[i][0] |= 1<<(b-1);
}
scanf("%d",&comm[i][1]);
}
best = -1;
doit(0,0,0,0);
printf("Case Number %d\n",++scen);
printf("Number of Customers: %d\n",best);
printf("Locations recommended:");
for (int i=0; i<n; i++)
if (sol&(1<<i))
printf(" %d",i+1);
puts("\n");
}
return 0;
}
Per wrote:By the way, regarding the problem set: despite us not doing as good as we had hoped, I really enjoyed the problem set, it was definitely one of the better ones I've seen (and I've seen quite a few by now).
shahriar_manzoor wrote:Voronoi diagram was not required for B. I guess some teams did not touch it considering that they need to implement V diagram.

Users browsing this forum: No registered users and 1 guest