Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Competitive - Algorithm for max number of superior characters
#include <vector> #include <iostream> #include <algorithm> #include <numeric> using namespace std; int maxsup(const vector<int>& charsCount) { const int length = accumulate(charsCount.cbegin(), charsCount.cend(), 0); const int optMaxSup = (length - 1) / 2; int charsUpToLimitLevelCount = 0; int limitLevel = 0; for (; charsUpToLimitLevelCount <= length - optMaxSup && limitLevel < (int)charsCount.size(); ++limitLevel) { charsUpToLimitLevelCount += charsCount[limitLevel]; } const int supAtLimitLevel = optMaxSup + charsUpToLimitLevelCount - length; charsUpToLimitLevelCount -= charsCount[--limitLevel]; const int matchedSupCount = min(supAtLimitLevel, max(charsUpToLimitLevelCount - 1, 0)); return optMaxSup - (supAtLimitLevel - matchedSupCount); } int main() { cout << maxsup({ 0,0,0,0,0 }) << endl; cout << maxsup({ 0,0,0,0,1 }) << endl; cout << maxsup({ 0,0,0,2,1 }) << endl; cout << maxsup({ 15,7,0,0,0 }) << endl; cout << maxsup({ 5,4,3,2,1 }) << endl; // aaaaabbbbcccdde -> abacacacadbdbeb - 7 // 1 2 3 4 5 6 7 return 0; }
run
|
edit
|
history
|
help
0
default
IBE FT ParseFileName
Strings
new
lab1
karma legacy "variant" with attr_cast
postfix eval in c++ - multidigits decimal
C++ string format
#9
get_number_of_threads, windows