Problem Statement
Today, a large live concert is going to take place. Some interprets (called “idols”) are going to perform at the concert. Different idols have different names.
There are M distinct songs that can be included in the concert. The songs are numbered 0 through M-1. Song i can only be performed by the idol s[i]. Including this song in the concert will increase the happiness of the audience by h[i].
We have to choose the set list for this concert. Our goal is to make the audience as happy as possible. However, the concert time is limited and therefore each idol can only perform at most one song.
You are given the vector h and the vector s with M elements each. Find the set of songs that should be played at the concert. The set of songs must have the following properties:
Each idol will perform at most one song.
After hearing the songs, the happiness of the audience will increase by the largest amount possible.
Return the largest possible amount of happiness the audience can gain.
Definition
Class:
LiveConcert
Method:
maxHappiness
Parameters:
vector , vector
Returns:
int
Method signature:
int maxHappiness(vector h, vector s)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Notes
The value M is not given explicitly. You can determine M as the length of h.
Constraints
M will be between 1 and 100, inclusive.
h and s will contain exactly M elements each.
All numbers in h will be between 1 and 100, inclusive.
All strings in s will have between 1 and 10 characters, inclusive.
For each valid i, each character in s[i] will be a lowercase letter (‘a’-‘z’).
Examples
0)
{10,5,6,7,1,2}
{“ciel”,”bessie”,”john”,”bessie”,”bessie”,”john”}
Returns: 23
The optimal concert will contain three songs:
“ciel” will sing the song number 0, which will give the audience 10 happiness
“bessie” will sing the song number 3, which will give the audience 7 happiness
“john” will sing the song number 2, which will give the audience 6 happiness
The total amount of happiness will be 10+7+6 = 23.
1)
{3,3,4,3,3}
{“a”,”a”,”a”,”a”,”a”}
Returns: 4
There is a single idol, thus the concert will consist of a single song. The optimal choice is the song that gives 4 happiness.
2)
{1,2,3,4,5,6,7,8,9,10,100}
{“a”,”b”,”c”,”d”,”e”,”e”,”d”,”c”,”b”,”a”,”abcde”}
Returns: 140
3)
{100}
{“oyusop”}
Returns: 100
4)
{100,100,100,100,100,100,100,100,100,100,100,100,100}
{“haruka”,”chihaya”,”yayoi”,”iori”,”yukiho”,”makoto”,”ami”,”mami”,”azusa”,”miki”,”hibiki”,”takane”,”ritsuko”}
Returns: 1300
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
My Solution
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <map>
using namespace std;
class LiveConcert
{
public:
int maxHappiness(vector <int> h, vector <string> s);
};
int LiveConcert::maxHappiness(vector <int> h, vector <string> s)
{
int M = h.size();
map<string, int> mem;
for (int i = 0; i < M; i++)
{
if (mem.find(s[i]) == mem.end()) //未找到
mem[s[i]] = h[i];
else
{
if (h[i] > mem[s[i]])
mem[s[i]] = h[i];
}
}
int cnt = 0;
for (map<string, int>::iterator it = mem.begin(); it != mem.end(); it++)
cnt += it->second;
return cnt;
}