Jump to content

Can someone explain this to me?

Hi people, here's the deal. I have a seminar paper to do, and I have done it but the only thing left to do is to explain the solution (I found it online to be honest) to my profesor. I tryed to explain it today but that ended no so well because I'm not 100% sure what the code does so I explaind as much as I could but that was not enogh so the prof. told me to return tomorow and explain it to her again since I was a bit angry and could not concentrate to my fullest.

 

So I ask you nicely if you can explain this code to my, what it does and how can I explain it to her?

 

I've done it in C since it must be written in C, but I have a solution in C++. It is the same, just that C++ has sort() function and in C I put selection sort, and I need a structure in C to replace pair in C++, other parts are totaly the same.

 

Here's the task:

teleporters.pdf

 

Here's the textual solution I found, but I think that the code doesn't folow this solution in particular, I coul'd be wrong though.

iarsc org.pdf

I also have this solution (page 37)

IOI2008Booklet1.0.pdf

 

 

I understand the first half of the code

And finaly here is the code I've got:

#include <algorithm>#include <iostream>#include <cassert>#include <cstdio>#include <cstring>#define NMAX 2000010using namespace std;int lookup[NMAX];pair<int,int> tel[NMAX];int N,M;char visited[NMAX];int compute_len(int st){	int i,len;	i = st;	len = 0;	while(!visited[i]){		visited[i] = 1;		if(i >= N-1) break;		i = lookup[tel[i+1].second];		++len;	}	return len;}int path;int cyc[NMAX];int C;int main(){	int i;	scanf("%d%d",&N,&M);	tel[0] = make_pair(0,-1);	for(i = 1; i <= N; ++i){		scanf("%d%d",&tel[i].first,&tel[i].second);		tel[N+i] = make_pair(tel[i].second,tel[i].first);	}	N*=2;	++N;	sort(tel,tel+N);	for(i = 0; i < N; ++i){		lookup[tel[i].first] = i;//what is lookup and what do we do with it?	}	path = compute_len(0);	for(i = 0; i < N; ++i){		if(visited[i]) continue;		cyc[C++] = compute_len(i);//what is this cyc array do, how do I explain it?	}	sort(cyc,cyc+C);	while(M-- && C){		path += cyc[--C]+2;//here is cyc again, why do we just add numbers to current path, what is the meaning of 2 here?	}//and the while part, I don't understand because I don't get those things before probably	++M;	while(M--){		if(C){			path += cyc[--C]+2;			continue;		}		path++;		cyc[C++] = 1;	}	printf("%d\n",path);	return 0;}

So if you guys can help me that would be awesome so I can earn some points and pass this damn exam finaly!

Link to comment
Share on other sites

Link to post
Share on other sites

like if you don't know what all this even means (I want to get into programming tho)

this isnt facebook

Love is patient, love is kind. It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs. Love does not delight in evil but rejoices with the truth. It always protects, always trusts, always hopes, always perseveres.1 Corinthians 13:4

 

Link to comment
Share on other sites

Link to post
Share on other sites

I know C, I understand it since I'm learning it in coledge. But this is not my code so I don't quite know the details of what the person wanted to do with some parts of the code.

Link to comment
Share on other sites

Link to post
Share on other sites

Its hard to read someone's code. Its best to write it by yourself. 

I want to help but task is too long that I dont have time to read it whole.

Try to write it from scratch alone and than ask for specific things. 
Pozdrav! :)

Link to comment
Share on other sites

Link to post
Share on other sites

Its hard to read someone's code. Its best to write it by yourself. 

I want to help but task is too long that I dont have time to read it whole.

Try to write it from scratch alone and than ask for specific things. 

Pozdrav! :)

Pozdrav brate moj!  :lol:

I've done that, I've marked the specific things I don't understand in the code. The start of the code is just reading from standard input and making a array. The function returns how many teleports I've passed going trough the Egypt.

 

In the text of the task is a lot of nonsence, the important bit is the EXAMPLE. 

Also the text solutions are not important, I just put them there so if anybody needs them then there they are  :)

Link to comment
Share on other sites

Link to post
Share on other sites

Pozdrav brate moj!  :lol:

I've done that, I've marked the specific things I don't understand in the code. The start of the code is just reading from standard input and making a array. The function returns how many teleports I've passed going trough the Egypt.

 

In the text of the task is a lot of nonsence, the important bit is the EXAMPLE. 

Also the text solutions are not important, I just put them there so if anybody needs them then there they are  :)

As I said I must read whole task to know how how it should work and I cant do that :/

I read in your profile that you are going to be programmer? If you want to become programmer you should try to write it by yourself :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×