**Div 1 450: The one with houses**

Ok, so you have N houses numbered from 0 to N-1 (1 to N in the problem, but I don't care). You have to build M roads connecting pairs of different cities such that if their numbers are a and b, |a-b| must not exceed K. Also, the number of roads connect to every house must be even.

Sounds complicated, until you note that the constraints are sort of low. Specially for K (less than 9).

Here is a dynamic programming sketch: First of all, imagine we were building roads in ascending order of house numbers. Which means that you first decide what roads to build for house 0, then house 1, etc.

In reality, let's focus only on roads connecting the current house to houses with a number smaller than its number - The roads connecting it to larger numbers will be dealt with when you build the roads of those houses. There will be at most K houses you can connect to.

How about remembering the

*parity*of all of the relevant houses? Including the current one, that's only (K+1) parities to remember. The total number of different parity combinations to remember is 2^(K+1).

Thus we can just do a dynamic programming solution that remembers the parity of the last K+1 houses and then builds roads between the current house and houses with smaller numbers.

... There is a catch, and it caused me a huge delay. You have to be careful not to be counting road combinations twice. To fix this, just include also the number of the last lesser-numbered house you built a road to, and make sure to never build roads to houses with a smaller number again.

Some care is needed. When you move on to a new house, you have to make sure that the house K units behind has an even parity, because there will be no way to fix an odd parity anymore.

constintMOD=1000000007;//Ihatelonglong.Itissoridiculous,whydidn'ttheychangeittojustlong//already!?typedeflonglongint64;#definelongint64structDengklekBuildingRoads{

intK,N;

longmem[1<<9][31][31][9];

//M*N*N

intindent;

longrec(intmask,intM,inthouse,intik){

long&res=mem[mask][M][house][ik];

if(res==-1){

res=0;

if(M==0){

//Basecase:nomoreroadstobuild

if(mask==0){

res=1;

}else{

res=0;

}

}elseif(house==N){

res=0;//couldnotbuildenoughroads(anotherbasecase)

}elseif(ik==K){

//buildnomoreroadsbetweenthishouseandthingsbeforeit

//makesure0-thprevioushouseiseven!

if((mask&1)==0){

res=rec(mask>>1,M,house+1,0);

}

}else{

into=house-K+ik;

if(o>=0){

//buildaroadbetweenhouseandik?

res=0;

intnmask=mask^(1<<K)^(1<<ik);

res+=rec(nmask,M-1,house,ik);

//don'tbuildmoreroadsbetweenthesehouses

res+=rec(mask,M,house,ik+1);

res%=MOD;

}else{

res=rec(mask,M,house,ik+1);

}

}

}

returnres;

}

intnumWays(intN,intM,intK)

{

this->K=K;

this->N=N;

memset(mem,-1,sizeof(mem));

returnrec(0,M,0,0);

}};#undeflong

**Div 1 300: The one with the chains**

You have a set of up to 50 strings containing three characters each. The characters can be . or digits. Concatenate the strings together in any order, then pick a contiguous substring of digits and calculate the sum of these digits. Find the largest possible sum.

I came up with something horribly complicated for this problem. In retrospect, I can now think of a simple way to implement the same idea.

First of all, there is a special case in which the best substring will be completely inside one of the strings in the input. For example ".9." might be the best score if all the other combinations deal with a smaller sum.

Assuming you take case of that special case, then we have to deal with the case in which the string is composed of at least two links (two strings from the array). Then let's just bruteforce for all pairs of different strings that could be the left and right extreme. Let us define the score of a left extreme: The sum of the digits that are contiguous to the right side of a string. The score of a right extreme is the same but backwards (The right extreme scores of "..9", "567" and ".6." are 9, 18 and 0, respectively).

We would just need to pick the best strings for the middle part of the string. Note that all of these strings must contain no '.', thus we can just use them all (as we want the maximum result).

structDengklekMakingChains{

intgradeRight(strings)

{

intp=s.size()-1;

intscore=0;

while((p>=0)&&(s[p]!='.')){

score+=(s[p]-'0');

p--;

}

returnscore;

}

intgradeLeft(strings)

{

reverse(s.begin(),s.end());

returngradeRight(s);

}

intgradeAll(strings)

{

intbest=0;

intscore=0;

s+='.';

for_each(ch,s){

if(*ch=='.'){

best=std::max(best,score);

score=0;

}else{

score+=(*ch-'0');

}

}

returnbest;

}

intmaxBeauty(vector<string>chains)

{

intbest=0;

for(inti=0;i<chains.size();i++){

//Considerindividuallinks:

best=std::max(score,gradeAll(chains[i]));

for(intj=0;j<chains.size();j++){

if(i!=j){//makesuretoavoidusingthesamestringtwice!

intscore=gradeLeft(chains[i])+gradeRight(chains[j]);

for(intk=0;k<chains.size();k++){

if(k!=i&&k!=j){

strings=chains[k];

if(count(s.begin(),s.end(),'.')==0){//canuseinthemiddle

//useit

score+=gradeAll(s);

}

}

}

best=std::max(best,score);

}

}

}

returnbest;

}};//

Making sure you don't pick the same two extremes at the same time is what caused me to first do something very complicated and also caused a great deal of mistakes during the match. Without care, you can fail a test case like {"9.9", "1"} by picking the first string twice.

Challenge

I knew that 300 was tricky, but decided to go get launch during the challenge phase instead. The codes in 300 were so complicated I would not have found the mistakes anyway,

Outcome

I finished typing this once I learned that I passed both problems. Yay.

## No comments :

Post a Comment