SRM 217 1A 2013.12.2
DIV1
200 - Problem Statement
You are playing a computer game and a bigfight is planned between two armies. You and your computer opponent will lineup your respective units in two rows, with each of your units facing exactlyone of your opponent's units and vice versa. Then, each pair of units, who faceeach other will fight and the stronger one will be victorious, while the weakerone will be captured. If two opposing units are equally strong, your unit willlose and be captured. You know how the computer will arrange its units, and mustdecide how to line up yours. You want to maximize the sum of the strengths ofyour units that are not captured during the battle. You will be given a vector<int> you and a vector <int> computer that specify the strengths ofthe units that you and the computer have, respectively. The return value shouldbe an int, the maximum total strength of your units that are not captured.
Definition
Class:
PlayGame
Method:
saveCreatures
Parameters:
vector <int>, vector <int>
Returns:
int
Method signature:
int saveCreatures(vector <int> you,vector <int> computer)
(be sure your method is public)
Constraints
-
you and computer will have the same numberof elements.
-
you and computer will contain between 1 and50 elements, inclusive.
-
Each element of you and computer will bebetween 1 and 1000, inclusive.
Examples
0)
{5, 15, 100, 1, 5}
{5, 15, 100, 1, 5}
Returns: 120
Your units with strengths of 100 and 15,along with one of your strength 5 units, can avoid capture.
1)
{1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000}
{1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000,1000, 1000, 1000}
Returns: 0
All units are equally strong, so all yourunits are captured.
2)
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
Returns: 99
You assign your weakest unit to thecomputer's strongest unit. Then you assign all your other units in such a waythat each of your units has a strength one higher than the opposing unit. Soall your units except the weakest one avoid capture.
3)
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Returns: 65
All your units can win.
4)
{651, 321, 106, 503, 227, 290, 915, 549,660, 115,
491,378, 495, 789, 507, 381, 685, 530, 603, 394,
7,704, 101, 620, 859, 490, 744, 495, 379, 781,
550,356, 950, 628, 177, 373, 132, 740, 946, 609,
29,329, 57, 636, 132, 843, 860, 594, 718, 849}
{16, 127, 704, 614, 218, 67, 169, 621, 340,319,
366,658, 798, 803, 524, 608, 794, 896, 145, 627,
401,253, 137, 851, 67, 426, 571, 302, 546, 225,
311,111, 804, 135, 284, 784, 890, 786, 740, 612,
360,852, 228, 859, 229, 249, 540, 979, 55, 82}
Returns: 25084
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
Code 111.97 points
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class PlayGame
{
public: int saveCreatures(vector<int>you, vector<int> computer)
{
intans = 0;
sort(you.begin(),you.end());
reverse(you.begin(),you.end());
sort(computer.begin(),computer.end());
reverse(computer.begin(),computer.end());
vector<int>::iteratorj=computer.begin();
for(vector<int>::iteratori=you.begin();i<you.end();i++)
{
while((j!=computer.end()) && ((*j)>=(*i)))
{
j++;
}
if(j==computer.end()) break;
ans+=(*i);
j++;
}
returnans;
}
};