+ 1

C++ algorithm help

I am preparing for the national-wide programming contest after being 4th in the regional. I saw an interesting problem: " There are n computers in a row. There are contestants from 3 citied: A B and C. If you how many contestants are from each city and where are contestants from A sitting, place the contestants from B and C in that way that two student form the same city cannot stay one to other. If there is no answer output -1 Example: 3 2 2(number of students from each city) 2 5 7(positions of the students from A) Output: BABCACA " My code: https://code.sololearn.com/cVhMY51t5yen/?ref=app

9th Mar 2021, 2:44 PM
CodeFu
CodeFu - avatar
8 Antworten
+ 3
Shadow Why do u put "int, char**". Sorry, I am putting a lot of effort on it, just need help somewhere :)
10th Mar 2021, 8:53 PM
CodeFu
CodeFu - avatar
+ 2
@Shadow In most cases it "Runtime error(executed code other than 0)" or wrong answer
9th Mar 2021, 4:53 PM
CodeFu
CodeFu - avatar
+ 2
Shadow can you help me, please?
9th Mar 2021, 7:55 PM
CodeFu
CodeFu - avatar
+ 2
Shadow Also,why do give value to variables using var{}
10th Mar 2021, 9:14 PM
CodeFu
CodeFu - avatar
+ 1
You posted the exact same question earlier: [Edit] Deleted Thread Please don't open duplicate threads. One thread is sufficient. At least delete the old one if you decide to repost your question. Besides, your code works for the sample input, and there is no other explanation. What is the issue?
9th Mar 2021, 3:51 PM
Shadow
Shadow - avatar
+ 1
Well, maybe you have figured something out on your own by now, but while your code has some minor issues, e.g. "arr" is only allocated for 'a' elements instead of "total" elements, the biggest problem is that there are just a lot of situations where none of your numerous conditionals match, although it should be possible to fill the gaps. Some cases: 1 1 3 2 1 1 3 4 3 3 3 1 3 5 If you handtraced any of the simpler ones, you would immeditaley see that your construction logic has gaps. If you have the time, I would suggest to "get back to the drawing board" and try to find a smarter approach that doesn't need as many hardcoded conditionals. That being said, I came up with an attempt of my own. It's probably not the ultimate solution either, but it should do the trick, although I haven't tested it extensively. Feel free to study the code if you want: https://code.sololearn.com/cSx07q93dnae/?ref=app
10th Mar 2021, 8:17 PM
Shadow
Shadow - avatar
+ 1
The main() function has at least two valid standard signatures: https://en.cppreference.com/w/cpp/language/main_function The int (usually called argc) and char** (usually called argv) arguments provide access to command line arguments, i.e. arguments that are passed into the program when it is started. This is helpful if your program is called from another process. https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean Since I don't need them on SoloLearn, I omit the names (C++ allows anonymous function parameters), but you might as well write: int main() { // ... }
10th Mar 2021, 9:04 PM
Shadow
Shadow - avatar
0
The curly braces are uniform initialization syntax. See https://mbevin.wordpress.com/2012/11/16/uniform-initialization/ for an explanation and https://softwareengineering.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax for some discussion regarding it. Writing just type identifier{}; will default-initialize the variable with whatever value is considered the default for the specific type, e.g. 0 for an integer or nullptr for pointers. C++ is not going to perform that default-initialization for you in all cases.
10th Mar 2021, 9:26 PM
Shadow
Shadow - avatar