Time Limit: 1sec Memory Limit:256MB
Description
小明是工厂的技术员,已知工厂的机器按品种分为3类,分别编号为1,2,3,小明的任务是找出每一类机器的最大产量。
给定一个整数N(1<=N<=10,000)代表机器的总数,并且给出每台机器的类型编号(1,..,3)以及产量(1,…,1,000,000),找出每类机器中最大的产量。
Input
第一行为T(1<=T<=20 ),代表用例个数。
接下来每个用例中,第一行为一个整数N,代表机器总数。
第2…N+1行:每行两个整数m,n,分别代表相对应的机器的类别和产量。
Output
按照类别号从小到大输出,每行包含类别号,以及该类别下的最大机器产量。 注意,可能有的类别下一台机器也没有,这种情况下不需要输出任何信息(参见Sample Input的第一个用例)。
Sample Input
Copy sample input to clipboard
2
2
1 2
2 5
10
1 5
3 7
3 3
2 4
3 5
2 3
1 7
2 6
3 4
1 2
Sample Output
1 2
2 5
1 7
2 6
3 7
O(∩_∩)O~~
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
int T, m, n;
cin >> T;
map<int, int> pro;
while (T--)
{
int N;
cin >> N;
pro.clear();
while (N--)
{
cin >> m >> n;
if (n > pro[m])
pro[m] = n;
}
for (map<int, int> ::iterator i = pro.begin(); i != pro.end(); i++)
cout << (*i).first << " " << (*i).second << endl;
}
return 0;
}
思路简单但操作略显复杂,来个简单的。
int main()
{
int T, m, n;
cin >> T;
vector<int> pro;
pro.resize(4);
while (T--)
{
for (int i = 0; i < 4; i++)
pro[i] = 0;
int N;
cin >> N;
while (N--)
{
cin >> m >> n;
if (n > pro[m])
pro[m] = n;
}
for (int i = 1; i < 4; i++)
if (pro[i])
cout << i << " " << pro[i] << endl;
}
return 0;
}