[C + +] 2017 MediaTek Cup programming challenge game title "Captain Jack's Troubles"

Source: Internet
Author: User
Tags stdin

The topic is as follows.

rules

Captain Jack had a good luck and got a lot of gold. But he immediately began to worry, because how to divide the gold coins, has always been a difficult thing, every time Captain Jack has a headache for several days.

With respect to coins, the Pirate's Guild is like this:

  • Each action, the captain will be based on the performance of each pirate victory, after the reward, to give you the gold.
  • At the time of the spoils, the pirates would stand in a row, and the captain would send a bag of gold to everyone. The number of coins in the bag is small, but the captain guarantees that each pirate will have at least one gold medal.
  • After getting the coins, the next two pirates will compare each other. If one of the credits is higher, he must have more gold coins, and if two people score the same, their gold must be the same. Otherwise, the Pirates feel unfair, to riot, and even revolt.

How can we use the least amount of gold to make everyone happy? It's not easy to lead these pirates.

I heard that there was a man in the game who was going to be the king of the Pirates, and Captain Jack believed he could solve the problem.

Enter a description

The Execute.stdin file exists in the current directory of the program, and the program obtains the input data from the Execute.stdin.
N (n<100) decimal positive integers are stored in the Execute.stdin, and the numbers are opened with an empty space.
Each number represents the credit of a pirate, with a value range of (1, 100).

Output Description

Outputs a positive integer that represents the minimum number of gold coins that are satisfactory to all pirates.

Algorithm Ideas
    • Find out all the local minimums in the sequence, assign a gold coin to the crew in the corresponding position, and then add 1 to the coin on each side.

    • There is a local maximum between the two local minimum values, such as a sequence ... 11 12 13 14 12 11 ... , if the two sides of the 11 is the local minimum, 14 is the local maximum, then from left to right, 11 to 13 are assigned 1 2 3 gold coins, from right to left, 11 to 12 are allocated 1 2 gold coins respectively, 14 of the position if the allocation of 3 gold coins can not meet more than 13 of the conditions, so 14 of the position allocated 4 gold coins. Therefore, the local maximum value corresponds to a larger number of gold coins when the local maximum value is incremented from both sides to the local max position.

    • For the leftmost and most right position of the sequence, if the first number is less than/greater than the second number, then it is the local minimum/large value, and if the last number is less than/greater than the second number, it is a local minimum/large value;

Source
#include <iostream> #include <fstream> #include <vector>using std::vector;// Vector as function parameter and return value notation void money (const vector <int> & value); vector <int> findlocalmin (const vector <    Int> & VEC); int main () {/* Opens the file for credit value information */Std::ifstream file;    File.Open ("Execute.stdin");    int temp;  Vector <int> value;    Store credit value if (!file) {std::cout << "error!";          } while (file >> temp) {value.push_back (temp);        } file.close ();    /* Calculate the number of coins */money (value);    Std::cin.get (); return 0;}    void money (const vector <int> &value) {//Remove duplicate number int length = Value.size ();    Vector <int> NewValue;    Newvalue.push_back (Value[0]); for (int i = 1; i < length; ++i) {if (Value[i]! = Value[i-1]) {Newvalue.push_back (value  [i]);    Extract the number of distinct}}//find local minimum position vector <int> Minlocalpos = findlocalmin (NewValue); Allocate coins (corresponding to non-repeatingsequence) int len = Newvalue.size ();  Vector <int> Gold;  Place gold coins (corresponding to the original sequence) vector <int> newgold (len, 1); Place gold coins (corresponding to non-repeating sequences) if (Minlocalpos[0] > 0)//start with decrement {for (int k = minlocalpos[0]-1; k >= 0;--k  ) {Newgold[k] = newgold[k+1] + 1; On the basis of the latter, add one} for (int i = 1; i < minlocalpos.size (); ++i) {for (int j = Minlocalpos[i -1] + 1; J < Minlocalpos[i]; ++J)//Previous {if (Newvalue[j] > Newvalue[j-1]) {Newgold[j] = newgold[j-  1] + 1;        Add one} to the previous (int j = Minlocalpos[i]-1; j > minlocalpos[i-1];--j)//from the back forward {//If the gradient in this direction is greater if ((Newvalue[j] > newvalue[j + 1]) && (newgold[j + 1]+1 > Newgold[j]  ) {Newgold[j] = newgold[j + 1] + 1;    Add a}}} int end = Minlocalpos[minlocalpos.size ()-1] on the basis of the latter; if (len-1 > End)///end is monotonically increasing {for (int i = end + 1; i < Len; ++i) {newgold[i] = newgold[i-1]  + 1; Add one}/*for (int k = 0; k < newgold.size (); ++k) {std::cout << newgold[k] &lt on the previous basis    ;< "";    }*///Allocate coins (corresponding to the original value sequence) int j = 0;    int i = 0;            while (I < length) {if (value[i] = = Newvalue[j]) {gold.push_back (newgold[j]);        ++i;        } else {++j;    }}//calculate gold and int sum = 0;        for (int k = 0; k < gold.size (); ++k) {//std::cout << gold[k] << "";    Sum + = Gold[k]; } std::cout << sum;}    Find the local minimum position vector <int> findlocalmin (const vector & VEC) {vector <int> minlocalpos;    int len = Vec.size ();        if (len = = 1) {minlocalpos.push_back (0);    return minlocalpos; }//Len >= 2 if (Vec[0] < vec[1]) {minlocalpos.push_back (0); } for (int i = 1; i < len-1; ++i) {if ((Vec[i] < vec[i-1]) && (Vec[i] < Vec[i + 1])        ) {minlocalpos.push_back (i);    }} if (Vec[len-1] < vec[len-2]) {minlocalpos.push_back (len-1); } return Minlocalpos;}
Run results

[C + +] 2017 MediaTek Cup programming challenge game title "Captain Jack's Troubles"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.