Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Arya has N balls arranged in a row. Some balls are colored and some are not. There are some M types of colors in Arya’s world and color balls have colors out of only these given M colors. Arya decided to color the remaining balls and put all the adjacent balls with the same color in 1 group. For example, lets say after coloring the rows of balls have these colors : {1, 2, 2, 3, 3, 3, 1, 1, 4, 5}. Then Arya can put them into following 6 groups : {1}, {2, 2}, {3, 3, 3}, {1, 1}, {4} and {5}. Arya wants the number of groups to be exactly K. Now the coloring also has some cost associated. So as already told that there are M colors, coloring each ball i with color j costs C(i, j). Arya want to use minimum paint for this task. You need to help her. It is guaranteed that we can paint the balls such that K groups are formed. Input Format:

The first line contains three integers, n, m and k (1 <=k<=n<= 100, 1<=m≤100)-the number of balls, number of colors and number of target groups

The second line contains n integers x1, x2, ..., xn (0<=xi<=m), the initial colors of the balls, xi equals to 0 if the ball number i is uncolored, otherwise the i-th ball has color xi.

Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes C(i, j) (1 <c(i,j)s 109) - the cost to color i-th ball with j. c(i, j)'s are specified even for the initially colored balls, but such balls still can't be colored.

output format:

print a single integer, minimum amount of paint needed balls.

sample input 1:

3 2 2

0 0 0

1 2

3 4

5 6

sample output 1:

10

i am not able figure out how is given and understand question

What I have tried:

I have tried searching for answers on the web but only the question is available not the answer
Posted
Updated 3-Apr-22 11:30am
v2
Comments
0x01AA 3-Apr-22 13:28pm    
"I have tried searching for answers"....
Then it is time to try to solve the problem by thinking about and not only search for the answer ;)
merano99 3-Apr-22 19:57pm    
How the value 10 is calculated in the example is unclear. How exactly is the example calculated?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You need to visit some Learn C++ tutorial to learn the language and analyze the task.
Every sentence has some information which must result in some code. Like you have colored balls. So I would create such code

C++
enum Color {
 red,
 green,
};

struct Ball {
  enum Color color; // color of the ball
};
 
Share this answer
 
Comments
Patrice T 3-Apr-22 15:04pm    
enum Color {
 red,
 green, // you should remove the comma on this line
};
CPallini 4-Apr-22 2:40am    
The trailing comma (useful in some contexs) is actually allowed:
https://en.cppreference.com/w/cpp/language/enum
When asking a question about a problem on a challenge site, it is a good idea to give the link : https://www.chegg.com/homework-help/questions-and-answers/arya-n-balls-arranged-row-balls-colored--m-types-colors-arya-s-world-color-balls-colors-gi-q95223529[^]
Quote:
I have tried searching for answers on the web but only the question is available not the answer

As programmer, your job is to create solutions to problems you get, nto finding all cooked solutions on internet.
Searching internet for solutions is a bad idea because you need solution for the exact same question, and this works only for the most simple problems or for standard well known problems like "A* search algorithm".
A* search algorithm - Wikipedia[^]

First you need to get familiar with the problem. Solving the problem by hand is usually a good approach.
- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
Program Development by Stepwise Refinement[^]
 
Share this answer
 
You can start by following the instructions verbatim:
C++
int main()
{
	// open file for reading
	ifstream datei("input.txt");
	if (!datei.is_open()) {
		cerr << "Error when opening the file!" << endl;
		exit(-1);
	}

    // first line
	unsigned n, m, k;

	if (datei >> n >> m >> k) {
		cout << "n=" << n << "\n";
		cout << "m =" << m << "\n";
		cout << "k=" << k << "\n";
	}
	else {
		cout << "Error reading first line!\n";
		exit(-1);
	}

    // check limits
	if (k < 1) { ... }
	if (k > n) { ... }
	if (n > 100) { ... }
	if ((m < 1) || (m > 100)) { ...	}

	// second line
	vector<int> ball;
	ball.resize(n);
	for (unsigned i=0; i < n; i++) {
		if (!(datei >> ball[i])) { 
 	
    ... 	

	return 0;
}

It is a combinatorial optimization problem, but the description is not clear.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900