Understanding the dp on tree - c++

I was recently solving a problem from Codeforces. After giving it a lot of tries I was not able to get how in tree dp the matrix calculation works in the editorial solution. The following is the code where I have added comments to the parts I don't understand in it.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int f[2][10010][110];//0 max 1 min
char s[10010];
int tr[10010][2],size,n,fa[10010],p,m,minn,pre;
void dfs(int x)
{
//cout<<x<<" "<<f[0][x][0]<<endl;
if (!tr[x][0]) return;
int l=tr[x][0],r=tr[x][1];
dfs(l),dfs(r);
/*The part which gets complicated need help why and how this calculation works*/
for (int i=0;i<=minn;i++)
for (int j=0;i+j<=minn;j++)
{
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
}
}
int main()
{
scanf("%s",s+1);
scanf("%d%d",&p,&m);
memset(f[0],-63,sizeof(f[0]));
memset(f[1],63,sizeof(f[1]));
/* Why we have used min of the two and how does it handle both condition */
minn=min(p,m);
n=strlen(s+1);
size=1;pre=size;
for (int i=1;i<=n;i++)
{
if (s[i]=='('||s[i]=='?')
{
tr[pre][tr[pre][0]?1:0]=++size;
fa[size]=pre;
pre=size;
}
else if (s[i]==')') pre=fa[pre];
else f[0][size][0]=f[1][size][0]=s[i]-'0',pre=fa[pre];
}
dfs(1);
printf("%d",f[0][1][minn]);
}
The part where I get lost is this
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
I always struggle with such types of problems. Can someone give the link to approach such problems.

Which part of the lines don't you understood? I take one line
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
and rewrite it
const int index_max = 0;
int y = i+j + (p<m? 1: 0); // in your code p<m is cast to int, true=1, false=0
int old_max = f[index_max][x][y];
int next_value = f[index_max][l][i] + f[index_max][r][j]:
f[index_max][x][y] = max(old_max, next_value);
You are looking for the maximum of the next_values of your double-loop. As l, r are fixed the next_values are sums of values in two rows.
Similar for the other 3 lines.

Related

Some questions regarding 2d Vectors, C++

This 2d vector is being used to hold a game-board for minesweeper. I want to create a 2d vector of struct cell, which has several "state" variables all holding information needed to construct the game board (I am creating a basic minesweeper game to run on the command line, very rudimentary, just want to get a better grasp of classes). First of all, what am I doing wrong when trying to pass the vector to the void function? And then how would I be able to access the separate variables to read and write to them? I know this may be unusual (could solve using arrays) but I'd like to do it a little differently. I have looked through various forums but people don't seem to use this approach. Thanks guys.
EDIT:
What I'm trying to accomplish with the vector of cell's is basically 3 vectors in 1 so that I can simultaneously use the information in the different states to check whether various conditions have been met when a player makes a move (i.e. check whether there is a mine there, or whether that spot has already been opened/marked/unmarked etc.) Please let me know if the code below doesn't allow for what I want to accomplish.
code:
#include <iostream>
#include <vector>
using namespace std;
void gameboard(vector<vector<int>> &stateboard)
struct cell
{
int state; //( 0 hidden, 1 revealed, 2 marked)
int value; //(-1 mine, 0 no surrounding, # > 0
bool isMine;
};
void gameboard(vector<vector<int>> &stateboard)
{
}
int main()
{
int columns = 10;
int rows = 10;
vector <vector<cell> > gameboard(rows, vector<cell>(columns));
gameboard(&gameboard);
return 0;
}
sorry guys, this piece of code doesn't even begin to resemble the outline I have in Xcode, I was just trying to present the question in an easier to follow manner and threw this together.
new code:
#include <iostream>
#include <vector>
using namespace std;
struct cell
{
int state; //( 0 hidden, 1 revealed, 2 marked)
int value; //(-1 mine, 0 no surrounding, # > 0
bool isMine;
};
void game_state(vector<vector<cell>> &stateboard)
{
}
int main()
{
int columns = 10;
int rows = 10;
vector <vector<cell> > gameboard(rows, vector<cell>(columns));
game_state(gameboard);
return 0;
}
I guess having the same name for a function and vector was throwing Xcode off, which is why I made game board a reference originally but now I see why that was stupid. Now that this works, how can i specifically read and write to just the bool isMine variable? I'm not asking for you to do it completely but a basic line of code showing me how to access that specific part would be a greatly help me. Am I conceptualizing this incorrectly?
hope it helps you:
#include <iostream>
#include <vector>
// your columns and rows are equal,
//and they should no change, so i think better to do them const
const int BOARD_SIZE = 10;
struct cell {
int state;
int value;
bool isMine;
};
void game_state(std::vector < std::vector <cell > > &stateboard) {
}
int main (){
std::vector < std::vector <cell > > gameboard;
//I give more preference to initialize matrix like this
gameboard.resize(BOARD_SIZE);
for (int x = 0; x < BOARD_SIZE; x++) {
gameboard[x].resize(BOARD_SIZE);
for (int y = 0; y < BOARD_SIZE; y++) {
// and this is an example how to use bool is mine
// here all cells of 10x10 matrix is false
// if you want place mine in a first cell just change it
// to gameboard[0][0].isMine = true;
gameboard[x][y].isMine = false;
}
}
game_state(gameboard);
return 0;
}

Inconsistency between int and bool

I just implemented breadth first search in c++ and instead of declaring a vector as bool, I declared it as an int. This lead to a very odd observation. When I used int, the code printed the following:
1
32763
-524268732
Throughout the entire code, I don't provide any such value to variable as the 2nd and 3rd node receive, so I assume that they are just garbage values, but why do garbage values even come up, when I'm initialising the vector to be full of zeroes ??? You may check the code to be that below:
#include <iostream>
#include <queue>
using namespace std;
queue<int> neigh;
vector< vector<int> > graph(3);
vector<int> flag(3, 0);
int main(void)
{
graph[0].push_back(1); graph[0].push_back(2);
graph[1].push_back(0); graph[1].push_back(2);
graph[2].push_back(0); graph[3].push_back(1);
neigh.push(0);
while(!neigh.empty())
{
int cur = neigh.front();
neigh.pop();
flag[cur] = 1;
for(int i = 0, l = graph[cur].size();i < l;i++)
{
if(!flag[graph[cur][i]])
neigh.push(graph[cur][i]);
}
}
for(int i = 0;i < 3;i++)
{
cout << flag[i] << endl;
}
}
Alright, then I changed just a single line of code, line number 7, the one where I declare and initialise the flag vector.
Before:
vector<int> flag(3, 0);
After:
vector<bool> flag(3, false);
And voila! The code started working:
1 //The new output
1
1
So, my question is, what is the problem with the code in the first place ? I believe it may be some kind of error I made, or possibly that its only by chance that my bfs implementation works at all... So, what is the truth, SO? What is my (possible) mistake ?
You are accessing your vector out of bounds here:
graph[3].push_back(1);
At this moment, graph only has three elements. This leads to undefined behaviour.

How to read matrix recursively c++

Can anyone help me with understanding recursion? I want to read a matrix recursively, and I don't really know how.
I made up a plan of transforming iterative agorithms to recursive ones in the following steps:
Repetitive structure-> if followed by recall of function somewhere in the body
Going from "top" to 0 and the going back to be alike the iterative way (i'm new to the concept, the idea may be dumb)
My function to read a matrix recursively (not working) is as follows:
void read_m(int n,int m)
{
if(n)
{
if(m)
{
read_m(n,m-1);
}
f>>v[n][m];
read_m(n-1,m);
}
}
I know what the mistake is, what I do not know is how should I solve this.
Thanks in advance.
P.S. n and m are width and height
Using recursion is probably not the greatest way to do this here but assuming you want it and you want the 'f' to execute for all the elements, it isn't going to work when n is 0 as it is inside the if(n) block.
first you should move f>>v[n][m];out if (n) block or it will not fill entire row with index 0.
second assuming that you keep data row by row you should read data before going to recursion, also to prevent double read you should only decrease rows when full column is read.
void read_m(int n,int m)
{
f>>v[width-1-n][height-1-m]; //suppose that width and height defined elsewhere
if(m)
{
read_m(n,m-1);
}
else if(n)
{
read_m(n-1,m);
}
}
if it is column by column you should change m and n in your code.
#include<fstream>
using namespace std;
ifstream f("recurs.in");
ofstream g("recurs.out");
int a[20][20],n,m;
void read(int i, int j)
{
if(j>1)
read(i,j-1);
else if(i>1)
read(i-1,j);
f>>a[i][j];
}
int main()
{
int i,j;
f>>n>>m;
read(n,m);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
g<<a[i][j]<<' ';
g<<endl;
}
}
Working

Project Euler 18 - C++ Passing Array By Reference

See: http://projecteuler.net/problem=18 for a better explanation of the problem than I could give here.
I like my algorithm; It's not brute-force. I look at all of the 3x3 triangles at the bottom of the pyramid to calculate which route is best, then assign that value back to the row. That's why my function is called ConsolidateBottomThreeRows -- I'll end up calculating the best path from the bottom up, assigning to the top-most of the bottom 3 rows each time, creating a smaller and smaller pyramid until my top row contains the answer.
My issue isn't a question of algorithm, simply syntax. I can't figure out [yes, even with Googling] how to get my arrays passed correctly. Here is my output when I try to compile:
C:\MyApps\Euler>g++ Prob_18.cpp -o Prob_18
Prob_18.cpp: In function 'void ConsolidateBottomThreeRows(int*, int*, int*)':
Prob_18.cpp:17:20: error: request for member 'size' in 'top', which is of non-class type 'int*'
And my code:
#include <iostream>
using namespace std;
int CrunchSmallTriangle(int top[], int middle[], int bottom[])
{
int biggest=0;
if(middle[0]+bottom[0] > biggest) biggest=middle[0]+bottom[0];
if(middle[0]+bottom[1] > biggest) biggest=middle[0]+bottom[1];
if(middle[1]+bottom[0] > biggest) biggest=middle[1]+bottom[0];
if(middle[1]+bottom[1] > biggest) biggest=middle[1]+bottom[1];
return biggest+top[0];
}
void ConsolidateBottomThreeRows(int top[], int middle[], int bottom[])
{
int SmallTop[0], SmallMiddle[2], SmallBottom[3];
for(int x=0;x<top.size();x++)
{
SmallTop[0]=top[x];
SmallMiddle[0]=middle[x];
SmallMiddle[1]=middle[x+1];
SmallBottom[0]=bottom[x];
SmallBottom[1]=bottom[x+1];
SmallBottom[2]=bottom[x+2];
top[x]=CrunchSmallTriangle(SmallTop, SmallMiddle, SmallBottom);
}
}
int main()
{
int row1[1]={75};
int row2[2]={95,64};
int row3[3]={17,47,82};
int row4[4]={18,35,87,10};
int row5[5]={20,4,82,47,65};
int row6[6]={19,1,23,75,3,34};
int row7[7]={88,2,77,73,7,63,67};
int row8[8]={99,65,4,28,6,16,70,92};
int row9[9]={41,41,26,56,83,40,80,70,33};
int row10[10]={41,48,72,33,47,32,37,16,94,29};
int row11[11]={53,71,44,65,25,43,91,52,97,51,14};
int row12[12]={70,11,33,28,77,73,17,78,39,68,17,57};
int row13[13]={91,71,52,38,17,14,91,43,58,50,27,29,48};
int row14[14]={63,66,4,68,89,53,67,30,73,16,69,87,40,31};
int row15[15]={4,62,98,27,23,9,70,98,73,93,38,53,60,4,23};
ConsolidateBottomThreeRows(row13, row14, row15);
ConsolidateBottomThreeRows(row11, row12, row13);
ConsolidateBottomThreeRows(row9, row10, row11);
ConsolidateBottomThreeRows(row7, row8, row9);
ConsolidateBottomThreeRows(row5, row6, row7);
ConsolidateBottomThreeRows(row3, row4, row5);
ConsolidateBottomThreeRows(row1, row2, row3);
cout<<row1[0];
}
Any help is greatly appreciated. Thanks!
Arrays do not have a size() method! You will either need to pass the size to the function as a separate parameter, or use a container (such as std::vector).
Firstly, I agree with the other answers that switching from raw C-arrays to std::array<> or std::vector<> would be the best move. However, that being said, as chris commented, because your C-arrays are statically sized, making ConsolidateBottomThreeRows into a function template makes the following possible as well:
template<std::size_t TopN, std::size_t MidN, std::size_t BotN>
void ConsolidateBottomThreeRows(int (&top)[TopN],
int (&middle)[MidN],
int (&bottom)[BotN])
{
int SmallTop[1], SmallMiddle[2], SmallBottom[3];
for (std::size_t x = 0; x != TopN; ++x)
{
SmallTop[0] = top[x];
SmallMiddle[0] = middle[x];
SmallMiddle[1] = middle[x + 1];
SmallBottom[0] = bottom[x];
SmallBottom[1] = bottom[x + 1];
SmallBottom[2] = bottom[x + 2];
top[x] = CrunchSmallTriangle(SmallTop, SmallMiddle, SmallBottom);
}
}
Note that your declaration of SmallTop has the wrong dimensions.
You need to replace top.size() with a size passed in to the function. Better yet, don't use arrays and use vectors instead. Then your top.size() will work.

C++ program to compute lcm of numbers between 1 to 20 (project euler )

as the title explains this is a program to find lcm of numbers between 1 to 20. i found an algorithm to do this, here's the link
http://www.cut-the-knot.org/Curriculum/Arithmetic/LCM.shtml
there is a java applet on the webpage that might explain the algorithm better
Problem: i wrote the code compiler shows no error but when i run the code the program goes berserk, i guess may be some infinite loopig but i can't figure it out for the life of me. i use turbo c++ 4.5 so basically if anyone can look at the code and help me out it would be great . thanks in advance
Algorithm:
say we need to find lcm of 2,6,8
first we find the least of the series and add to it the number above it, i.e the series become
4,6,8
now we find the least value again and add to it the intitial value in the column i.e 2
6,6,8
so the next iteration becomes
8,6,8
8,12,8
10,12,8
10,12,16
12,12,16
14,12,16
14,18,16
16,18,16
18,18,16
18,18,24
20,18,24
20,24,24
22,24,24
24,24,24
as you can see at one point all numbers become equal which is our lcm
#include<iostream.h>
/*function to check if all the elements of an array are equal*/
int equl(int a[20], int n)
{
int i=0;
while(n==1&&i<20)
{
if (a[i]==a[i+1])
n=1;
else
n=0;
i++;
}
return n;
}
/*function to calculate lcm and return that value to main function*/
int lcm()
{
int i,k,j,check=1,a[20],b[20];
/*loading both arrays with numbers from 1 to 20*/
for(i=0;i<20;i++)
{
a[i]=i+1;
b[i]=i+1;
}
check= equl(a,1);
/*actual implementation of the algorith*/
while(check==0)
{
k=a[0]; /*looks for the least value in the array*/
for(i=0;i<20;i++)
{
if(a[i+1]<k)
{
k=a[i+1]; /*find the least value*/
j=i+1; /*mark the position in array */
}
else
continue;
}
a[j]=k+b[j]; /*adding the least value with its corresponding number*/
check= equl(a,1);
}
return (a[0]);
/*at this point all numbers in the array must be same thus any value gives us the lcm*/
}
void main()
{
int l;
l=lcm();
cout<<l;
}
In this line:
a[j]=k+b[j];
You use j but it is unitialized so it's some huge value and you are outside of the array bounds and thus you get a segmentation fault.
You also have some weird things going on in your code. void main() and you use cout without either saying std::cout or using namespace std; or something similar. An odd practice.
Also don't you think you should pass the arrays as arguments if you're going to make lcm() a function? That is int lcm(int a[], int b[]);.
You might look into using a debugger also and improving your coding practices. I found this error within 30 seconds of pasting your code into the compiler with the help of the debugger.
Your loop condition is:
while(n==1&&i<20)
So your equl function will never return 1 because if n happens to be 1 then the loop will just keep going and never return a 1.
However, your program still does not appear to return the correct result. You can split the piece of your code that finds the minimum element and replace it with this for cleanliness:
int least(int a[], int size){
int minPos = 0;
for(int i=0; i<size ;i++){
if (a[i] < a[minPos] ){
minPos = i;
}
}
return minPos;
}
Then you can call it by saying j = least(a, 20);. I will leave further work on your program to you. Consider calling your variables something meaningful instead of i,j,k,a,b.
Your equl function is using array indices from 0-20, but the arrays only have 1-19
j in lcm() is uninitialized if the first element is the smallest. It should be set to 0 at the top of the while loop
In the following code, when i=19, you are accessing a[20], which is out of the bounds of the array. Should be for(i=0;i<19;i++)
for(i=0;i<20;i++) {
if(a[i+1]<k)
You are not actually using the std namespace for the cout. this should be std::cout<<l
Your are including iostream.h. The standard is iostream without the .h, this may not work on such an old compiler tho
instead of hard-coding 20 everywhere, you should use a #define. This is not an error, just a style thing.
The following code does nothing. This is the default behavior
else
continue;