| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
public class Solver { |
| 6 |
|
| 7 |
|
| 8 |
public static final long bruteForce(int[] offsets, int[] lengths, int chunk, int minLists, int maxTotalLength){ |
| 9 |
|
| 10 |
int bits=offsets.length; |
| 11 |
int max=(1<<bits)-1; |
| 12 |
|
| 13 |
for(long i=0; i<=max; i++){ |
| 14 |
long x=evaluate(offsets, lengths, chunk, i); |
| 15 |
} |
| 16 |
|
| 17 |
assert(false); |
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
public static final void findWorstGreedy(final int[] offsets, final int[] lengths, |
| 23 |
final int chunk, final int[] lists, int[] r){ |
| 24 |
assert(r!=null && r.length==2); |
| 25 |
|
| 26 |
long min=Long.MAX_VALUE; |
| 27 |
int worstIndex=-1; |
| 28 |
for(int i=0; i<lists.length; i++){ |
| 29 |
long value=valueOfElement(offsets, lengths, 1f, chunk, lists, i); |
| 30 |
if(value<min){ |
| 31 |
if(min<EARLY_TERMINATION_SCORE){ |
| 32 |
r[0]=i; |
| 33 |
r[1]=(value<Integer.MIN_VALUE ? Integer.MIN_VALUE : value>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)value); |
| 34 |
return; |
| 35 |
} |
| 36 |
min=value; |
| 37 |
worstIndex=i; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
r[0]=worstIndex; |
| 42 |
r[1]=(min<Integer.MIN_VALUE ? Integer.MIN_VALUE : min>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)min); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public static final void findWorstGreedy(final int[] offsets, final int[] lengths, |
| 47 |
final float[] weights, final int chunk, final int[] lists, int[] r){ |
| 48 |
assert(r!=null && r.length==2); |
| 49 |
|
| 50 |
long min=Long.MAX_VALUE; |
| 51 |
int worstIndex=-1; |
| 52 |
for(int i=0; i<lists.length; i++){ |
| 53 |
|
| 54 |
long value=valueOfElement(offsets, lengths, weights[i], chunk, lists, i); |
| 55 |
if(value<min){ |
| 56 |
if(min<EARLY_TERMINATION_SCORE && i!=0){ |
| 57 |
r[0]=i; |
| 58 |
r[1]=(value<Integer.MIN_VALUE ? Integer.MIN_VALUE : value>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)value); |
| 59 |
|
| 60 |
return; |
| 61 |
} |
| 62 |
min=value; |
| 63 |
worstIndex=i; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
r[0]=worstIndex; |
| 68 |
r[1]=(min<Integer.MIN_VALUE ? Integer.MIN_VALUE : min>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)min); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public static long valueOfElement(final int[] offsets, final int[] lengths, float keyWeight, |
| 73 |
final int chunk, final int[] lists, int index){ |
| 74 |
|
| 75 |
final int numlists=lists.length; |
| 76 |
if(numlists<1){return 0;} |
| 77 |
|
| 78 |
final int prospect=lists[index]; |
| 79 |
if(lengths[prospect]==0){return -999999;} |
| 80 |
|
| 81 |
long valuep=POINTS_PER_LIST+(POINTS_PER_LIST*2/lists.length)+((POINTS_PER_LIST*10)/lengths[prospect]); |
| 82 |
long valuem=POINTS_PER_SITE*lengths[prospect]; |
| 83 |
|
| 84 |
if(prospect==0 || (prospect==offsets.length-1)){ |
| 85 |
valuep+=BONUS_POINTS_FOR_END_LIST; |
| 86 |
} |
| 87 |
|
| 88 |
if(numlists==1){ |
| 89 |
valuep+=(POINTS_FOR_TOTAL_LIST_WIDTH+POINTS_PER_BASE1)*chunk; |
| 90 |
return ((long)(valuep*keyWeight))+valuem; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
final int first=lists[0]; |
| 95 |
final int last=lists[numlists-1]; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
final int offL=(prospect==first ? -1 : offsets[lists[index-1]]); |
| 110 |
final int offP=offsets[prospect]; |
| 111 |
final int offR=(prospect==last ? offsets[offsets.length-1]+1 : offsets[lists[index+1]]); |
| 112 |
assert(offL<=offP); |
| 113 |
assert(offP<=offR); |
| 114 |
assert(offL<offR) : "\noffsets.length="+offsets.length+", lengths.length="+lengths.length+"\n"+ |
| 115 |
", chunk="+chunk+", lists.length="+lists.length+", index="+index+"\n"+ |
| 116 |
", offL="+offL+", offR="+offR+", prospect="+prospect+", first="+first+", last="+last+"\n"+ |
| 117 |
", valuep="+valuep+", valuem="+valuem+", weight="+keyWeight+"\n"+ |
| 118 |
"offsets = "+Arrays.toString(offsets)+"\tlengths = "+Arrays.toString(lengths)+"\nlists = "+Arrays.toString(lists)+"\n"; |
| 119 |
|
| 120 |
int oldLeftSpace=offP-offL; |
| 121 |
int oldRightSpace=offR-offP; |
| 122 |
int newSpace=offR-offL; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
long spaceScore=((oldLeftSpace*oldLeftSpace+oldRightSpace*oldRightSpace)-(newSpace*newSpace))*MULT_FOR_SPACING_PENALTY; |
| 129 |
assert(spaceScore>0) : "\n"+spaceScore+", "+oldLeftSpace+", "+oldRightSpace+", "+newSpace+"\n"+ |
| 130 |
Arrays.toString(offsets)+"\nprospect="+prospect+"\n"; |
| 131 |
valuep+=spaceScore; |
| 132 |
|
| 133 |
int uniquelyCovered; |
| 134 |
if(prospect==first){ |
| 135 |
uniquelyCovered=offR-offP; |
| 136 |
}else if(prospect==last){ |
| 137 |
uniquelyCovered=offP-offL; |
| 138 |
}else{ |
| 139 |
int a=offL+chunk; |
| 140 |
int b=offR-a; |
| 141 |
uniquelyCovered=(b>0 ? b : 0); |
| 142 |
} |
| 143 |
|
| 144 |
if(prospect==first || prospect==last){ |
| 145 |
valuep+=(POINTS_PER_BASE1+POINTS_FOR_TOTAL_LIST_WIDTH)*uniquelyCovered; |
| 146 |
}else{ |
| 147 |
valuep+=POINTS_PER_BASE1*uniquelyCovered; |
| 148 |
} |
| 149 |
|
| 150 |
return ((long)(valuep*keyWeight))+valuem; |
| 151 |
} |
| 152 |
|
| 153 |
public static int[] toBitList(final int key){ |
| 154 |
final int numlists=Integer.bitCount(key); |
| 155 |
final int[] lists=new int[numlists]; |
| 156 |
for(int i=0, ptr=0; ptr<numlists; i++){ |
| 157 |
if((masks32[i]&key)!=0){ |
| 158 |
lists[ptr]=i; |
| 159 |
ptr++; |
| 160 |
} |
| 161 |
} |
| 162 |
return lists; |
| 163 |
} |
| 164 |
|
| 165 |
public static int[] toBitList(final long key){ |
| 166 |
final int numlists=Long.bitCount(key); |
| 167 |
assert(numlists>0); |
| 168 |
final int[] lists=new int[numlists]; |
| 169 |
for(int i=0, ptr=0; ptr<numlists; i++){ |
| 170 |
if((masks[i]&key)!=0){ |
| 171 |
lists[ptr]=i; |
| 172 |
ptr++; |
| 173 |
} |
| 174 |
} |
| 175 |
return lists; |
| 176 |
} |
| 177 |
|
| 178 |
public static long evaluate(int[] offsets, int[] lengths, final int chunk, final long key){ |
| 179 |
|
| 180 |
long score=0; |
| 181 |
|
| 182 |
final int[] lists=toBitList(key); |
| 183 |
final int numlists=lists.length; |
| 184 |
|
| 185 |
final int first=lists[0]; |
| 186 |
final int last=lists[numlists-1]; |
| 187 |
|
| 188 |
score+=numlists*POINTS_PER_LIST; |
| 189 |
for(int i=0; i<numlists; i++){ |
| 190 |
int list=lists[i]; |
| 191 |
score+=POINTS_PER_SITE*lengths[list]; |
| 192 |
} |
| 193 |
if(first==0){score+=BONUS_POINTS_FOR_END_LIST;} |
| 194 |
if(last==offsets.length-1){score+=BONUS_POINTS_FOR_END_LIST;} |
| 195 |
|
| 196 |
score+=(POINTS_FOR_TOTAL_LIST_WIDTH*(offsets[last]-offsets[first]+chunk)); |
| 197 |
|
| 198 |
|
| 199 |
for(int i=1; i<numlists; i++){ |
| 200 |
int list1=lists[i-1]; |
| 201 |
int list2=lists[i]; |
| 202 |
int space=offsets[list2]-offsets[list1]; |
| 203 |
int uncovered=space>chunk ? space-chunk : 0; |
| 204 |
|
| 205 |
score+=MULT_FOR_SPACING_PENALTY*(space*space); |
| 206 |
score-=POINTS_PER_BASE1*uncovered; |
| 207 |
} |
| 208 |
|
| 209 |
if(first>0){ |
| 210 |
long x=offsets[first]; |
| 211 |
score+=MULT_FOR_SPACING_PENALTY*(x*x); |
| 212 |
score-=POINTS_PER_BASE1*x; |
| 213 |
} |
| 214 |
|
| 215 |
if(last<(offsets.length-1)){ |
| 216 |
long x=offsets[offsets.length-1]-offsets[last]; |
| 217 |
score+=MULT_FOR_SPACING_PENALTY*(x*x); |
| 218 |
score-=POINTS_PER_BASE1*x; |
| 219 |
} |
| 220 |
|
| 221 |
return score; |
| 222 |
} |
| 223 |
|
| 224 |
public static final int BASE_POINTS_PER_SITE=-50; |
| 225 |
public static long POINTS_PER_SITE=-50; |
| 226 |
|
| 227 |
public static final long MULT_FOR_SPACING_PENALTY=-30; |
| 228 |
|
| 229 |
public static long EARLY_TERMINATION_SCORE=(POINTS_PER_SITE*2000); |
| 230 |
|
| 231 |
public static final long POINTS_PER_LIST=30000; |
| 232 |
public static final long POINTS_PER_BASE1=6000; |
| 233 |
public static final long POINTS_PER_BASE2=1000; |
| 234 |
public static final long BONUS_POINTS_FOR_END_LIST=40000; |
| 235 |
public static final long POINTS_FOR_TOTAL_LIST_WIDTH=5500; |
| 236 |
|
| 237 |
public static final long[] masks=new long[64]; |
| 238 |
public static final int[] masks32=new int[32]; |
| 239 |
static{ |
| 240 |
for(int i=0; i<masks.length; i++){masks[i]=(1L<<i);} |
| 241 |
for(int i=0; i<masks32.length; i++){masks32[i]=(1<<i);} |
| 242 |
} |
| 243 |
} |