Skip the Class
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 374 Accepted Submission(s): 222
Problem Description
Finally term begins. luras loves school so much as she could skip the class happily again.(wtf?)
Luras will take n lessons in sequence(in another word, to have a chance to skip xDDDD).
For every lesson, it has its own type and value to skip.
But the only thing to note here is that luras can't skip the same type lesson more than twice.
Which means if she have escaped the class type twice, she has to take all other lessons of this type.
Now please answer the highest value luras can earn if she choose in the best way.
Luras will take n lessons in sequence(in another word, to have a chance to skip xDDDD).
For every lesson, it has its own type and value to skip.
But the only thing to note here is that luras can't skip the same type lesson more than twice.
Which means if she have escaped the class type twice, she has to take all other lessons of this type.
Now please answer the highest value luras can earn if she choose in the best way.
Input
The first line is an integer T which indicates the case number.
And as for each case, the first line is an integer n which indicates the number of lessons luras will take in sequence.
Then there are n lines, for each line, there is a string consists of letters from 'a' to 'z' which is within the length of 10,
and there is also an integer which is the value of this lesson.
The string indicates the lesson type and the same string stands for the same lesson type.
It is guaranteed that——
T is about 1000
For 100% cases, 1 <= n <= 100,1 <= |s| <= 10, 1 <= v <= 1000
And as for each case, the first line is an integer n which indicates the number of lessons luras will take in sequence.
Then there are n lines, for each line, there is a string consists of letters from 'a' to 'z' which is within the length of 10,
and there is also an integer which is the value of this lesson.
The string indicates the lesson type and the same string stands for the same lesson type.
It is guaranteed that——
T is about 1000
For 100% cases, 1 <= n <= 100,1 <= |s| <= 10, 1 <= v <= 1000
Output
As for each case, you need to output a single line.
there should be 1 integer in the line which represents the highest value luras can earn if she choose in the best way.
there should be 1 integer in the line which represents the highest value luras can earn if she choose in the best way.
Sample Input
2 5 english 1 english 2 english 3 math 10 cook 100 2 a 1 a 2
Sample Output
115 3
Source
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6015
中文版题目:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=748&pid=1001
开学第一场BC。
翘课的话肯定是翘价值最大的那两节。
注意:上课的顺序就是输入的顺序,所以用map保存每门课的最大价值和第二大价值,然后求和就可以了。
AC代码:
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int T,n;
cin>>T;
while(T--)
{
cin>>n;
map<string,int>first,second;
first.clear();
second.clear();
string name;
int length;
for(int i=0;i<n;i++)
{
cin>>name>>length;
if(first[name]<length)
{
second[name]=first[name];
first[name]=length;
}
else if(second[name]<length)
{
second[name]=length;
}
}
int ans=0;
map<string,int>::iterator it;
for(it=first.begin();it!=first.end();it++)
{
ans+=it->second;
}
for(it=second.begin();it!=second.end();it++)
{
ans+=it->second;
}
cout<<ans<<endl;
}
return 0;
}
此题题解及代码。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n;
void datamaker_pre()
{
srand(time(0));
freopen("Skip the Class_pre.in", "w", stdout);
casenum = 1000; printf("%d\n", casenum);
for (casei = 1; casei <= casenum; ++casei)
{
int n = rand() % 10 + 1; printf("%d\n", n);
for (int i = 1; i <= n; ++i)
{
int l = rand() % 3 + 1;
for (int j = 0; j < l; ++j)printf("%c", rand() % 5 + 'a');
int v = rand() % 1000 + 1;
printf(" %d\n", v);
}
}
}
void datamaker()
{
srand(time(0));
freopen("Skip the Class.in", "w", stdout);
casenum = 1000; printf("%d\n", casenum);
for (casei = 1; casei <= casenum; ++casei)
{
int n = rand() % 100 + 1; printf("%d\n", n);
for (int i = 1; i <= n; ++i)
{
int l = rand() % 3 + 1;
for (int j = 0; j < l; ++j)printf("%c", rand() % 5 + 'a');
int v = rand() % 1000 + 1;
printf(" %d\n", v);
}
}
}
int main()
{
//datamaker_pre(); return 0;
//freopen("Skip the Class_pre.in", "r", stdin); freopen("Skip the Class_pre.out", "w", stdout);
//datamaker(); return 0;
//freopen("Skip the Class.in", "r", stdin); freopen("Skip the Class.out", "w", stdout);
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
map<string, int> first, second;
first.clear();
second.clear();
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
char s[12]; int v;
scanf("%s%d", s, &v);
gmax(second[s], v);
if (second[s] > first[s])swap(second[s], first[s]);
}
int sum = 0;
for (auto it : first)sum += it.second;
for (auto it : second)sum += it.second;
printf("%d\n", sum);
}
return 0;
}
/*
【分析】
这道题其实就是对每种字符串保留最大的两个价值就好。这里用map实现比较方便。
*/