How to solve the Candy problem?
2015-01-09 15:34
513 查看
The problem is:
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
We can use the piece of code to solve the problem:
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
We can use the piece of code to solve the problem:
namespace { template <typename I> size_t candy(I begin, I end) { int ret = 0; int assignment = 1; while ( begin < end ) { I i = begin; int sum = 0; // what is the loop invariant? size_t n = std::distance(begin, end); if (n > 1) { if (*i < *(i + 1)) { while ((i + 1) < end && *i < *(i + 1)) { sum += assignment; ++assignment; ++i; } } else if (*i > *(i + 1)) { I j = i; while ((i + 1) < end && *i > *(i + 1)) { sum += assignment; --assignment; ++i; } if (assignment > 1) { int v = assignment - 1; v *= (static_cast<int>(i - j) - 1); sum -= v; } else if (assignment < 1) { int v = 1 - assignment; v *= static_cast<int>(i - j); sum += v; } assignment = 1; } else { while ((i + 1) < end && *i == *(i + 1)) { sum += assignment; assignment = 1; ++i; } } } else { sum += assignment; ++i; } ret += sum; begin = i; } return static_cast<size_t>(ret); } }
相关文章推荐
- How to solve problem caused when the CRT library and MFC libraries are linked in the wrong order
- How to solve the problem : "You have been logged on with a temporary profile"
- How to solve the SVDI SN Number Display Problem
- How to solve "The installation source for this product is not available" problem?
- How to solve the problem "A project with an Output Type of Class Library cannot be started directly "
- Weblogic Server in the Chinese show how to solve the problem hash
- How to solve the problem witch is the system send many emails when cron have something wrong
- how to solve the problem which is the imail software can not send email in wan,but can send email in lan.
- how to solve the problem we met?
- How to solve the issue of RSARTE's starting problem
- how to solve the problem when you have chmod the directory to 777 but the apache still can not write file
- How to solve "one or more projects in the solution were not loaded correctly" problem?
- How To Solve The Problem : Unstable Ubuntu ADSL Connection
- How to Solve a Mathematics Problem?
- how to fix the problem when you install matlab on *nix-- "libXp.so.6 : cannot open "
- How to solve “add/remove operation is impossible, because the code element 'Cxxx' is read only”
- How to solve the roll crusher roller wear leather
- how to solve aliasing problem
- How to fix the Android AVD no target problem
- How to handle crash problem?(2.Finding crash information using the MAP file 2)