Oh no… more lemmings!! And in Lemmings Planet a huge battle is being fought between the two great rival races: the green lemmings and the blue lemmings. Everybody was now assigned to battle and they will fight until one of the races completely dissapears: the Deadly War has begun!
Every single lemming has a power measure that describes its ability to fight. When two single lemmings fight with each one, the lemming with more power survives and the other one dies. However, the power of the living lemming gets smaller after the fight, exactly in the value of the power of the lemming that died. For example, if we have a green lemming with power 50 and a blue lemming with power 40, the blue one dies and the green one survives, but having only power 10 after the battle (50-40=10). If two lemmings have the same power when they fight, both of them die.
In the fight between the two races, there are a certain number of battlefields. Each race assigns one lemming for each battlefield, starting with the most powerful. So for example, if a race has 5 lemmings with power 50 50 40 40 30 and we have 3 battlefields, then a lemming with power 50 will be assigned to battlefield 1, another with 50 power will be assigned to battlefield 2 and last a lemming with power 40 will go to battlefield 3. The other race will do the same.
The Deadly War is processed by having each race send its best soldiers as described to the battlefields, making a battle round. Then, all battles process at the same time, and some of the lemmings will emerge victorious (but with less power) and some of them will die. The surviving ones will return to their race’s army and then a new round will begin, with each race sending again its best remaining soldiers to the battlefields. If at some point a race does not have enough soldiers to fill all battlefields, then only the ones with soldiers from both races will have a fight.
The Deadly War ends when one of the races has no more lemmings or when both of them disappear at the same time. For example, imagine a war with 2 battlefields and a green army with powers 20 10 and a blue army with powers 10 10 15. The first round will have 20 vs 15 in battlefield 1 and 10 vs 10 in battlefield 2. After these battles, green race will still have a power 5 lemming (that won on battlefield 1) and blue race will have one with power 10 (that did not fight). The ones in battlefield 2 died, since they both had the same power. Afer that comes a second round, and only battlefield 1 will have a fight, being 5 vs 10. The blue lemming wins, killing the last green soldier and giving the victory do the blue race!
But in the real battle, will victory be green or blue?
Given the number of battefields and the armies of both races, your task is to discover which race will win the Deadly War and show the power of the surviving soldiers.
Input
The first line of input contains a single number N, representing the number of test cases that follow (1 ≤ N ≤ 100).
Each test case starts with a line with three space-separated integers, B, SG and SB, representing respectively the number of battlefields available, the number of lemmings in the green army and the number of lemmings in the blue army (1 ≤ B, SG, SB ≤ 100000).
Than follow exactly SG lines, each one with an integer indicating the power of one single lemming of the green army, followed by SB lines, each one with an integer indicating the power of one single lemming of the blue army. This power is a positive integer smaller than 101.
The lemmings in each army do not need to come in any particular order.
Output
For each test case you should print:
• ‘green and blue died’ if both races died in the same round
• ‘green wins’ if the green army won the Deadly War, followed by one line for each surviving soldier (in descending order)
• ‘blue wins’ if the blue army won the Deadly War, followed by one line for each surviving soldier (in descending order)
There should also be a blank line beetween test cases.
Sample Input
3
5 1 1
10
10
2 2 3
20
10
10
10
15
3 5 5
50
40
30
40
50
50
30
30
20
60
Sample Output
green and blue died
blue wins
5
green wins
10
10
问题链接:UVA978 Lemmings Battle!
问题简述:(略)
问题分析:
给定2个队及其每个队员的战斗力,共有b个战场,每轮分别选出b个队员对决,赢的队员进入下一轮(战斗力要减去输方的战斗力),直到其中一个队赢或者打平。
这是一个匹配问题,可以使用2个优先队列存放队员。
程序说明:
这个题解用2种不同容器来实现,各有技巧不同,本质上没有多大差异。用STL的set实现不常见,读一下代码也是好的,但是考虑到数据有重复,需要使用multiset。用STL的优先队列实现是常见的做法。值得尝试的是用数组+sort实现,数据规模比较小的情况下,时间效果应该更好。
参考链接:(略)
题记:(略)
AC的C++语言程序(STL集合)如下:
/* UVA978 Lemmings Battle! */
#include
using namespace std;
int main()
{
int n, b, sg, sb, a;
scanf("%d", &n);
while(n--) {
multiset<int> setg, setb;
scanf("%d%d%d", &b, &sg, &sb);
for(int i = 1; i <= sg; i++) {
scanf("%d", &a);
setg.insert(a);
}
for(int i = 1; i <= sb; i++) {
scanf("%d", &a);
setb.insert(a);
}
while(!setg.empty() && !setb.empty()) {
vector<pair<int, int> > v;
for(int i = 1; i <= b && !setg.empty() && !setb.empty(); i++) {
set<int>::iterator green = setg.end(), blue = setb.end();
green--, blue--;
if(*blue > *green) v.push_back(make_pair(0, *blue - *green));
else if(*blue < *green) v.push_back(make_pair(1, *green - *blue));
setg.erase(green);
setb.erase(blue);
}
for(int i = 0; i < (int)v.size(); i++)
if(v[i].first == 0) setb.insert(v[i].second);
else setg.insert(v[i].second);
}
if(setg.empty() && setb.empty()) printf("green and blue died\n");
else if(setg.empty()) {
printf("blue wins\n");
for(multiset<int>::reverse_iterator iter = setb.rbegin(); iter != setb.rend(); iter++)
printf("%d\n", *iter);
} else if(setb.empty()) {
printf("green wins\n");
for(multiset<int>::reverse_iterator iter = setg.rbegin(); iter != setg.rend(); iter++)
printf("%d\n", *iter);
}
if(n) printf("\n");
}
return 0;
}
AC的C++语言程序(STL优先队列)如下:
/* UVA978 Lemmings Battle! */
#include
using namespace std;
int main()
{
int n, b, sg, sb, a;
scanf("%d", &n);
while(n--) {
priority_queue<int> pqb, pqg;
scanf("%d%d%d", &b, &sg, &sb);
for(int i = 1; i <= sg; i++) {
scanf("%d", &a);
pqg.push(a);
}
for(int i = 1; i <= sb; i++) {
scanf("%d", &a);
pqb.push(a);
}
while(!pqg.empty() && !pqb.empty()) {
vector<pair<int, int> > v;
for(int i = 1; i <= b && !pqg.empty() && !pqb.empty(); i++) {
int green = pqg.top(), blue = pqb.top();
if(blue > green) v.push_back(make_pair(0, blue - green));
else if(blue < green) v.push_back(make_pair(1, green - blue));
pqb.pop();
pqg.pop();
}
for(int i = 0; i < (int)v.size(); i++)
if(v[i].first == 0) pqb.push(v[i].second);
else pqg.push(v[i].second);
}
if(pqg.empty() && pqb.empty()) printf("green and blue died\n");
else if(pqg.empty()) {
printf("blue wins\n");
while(!pqb.empty()) {printf("%d\n", pqb.top()); pqb.pop();}
} else if(pqb.empty()) {
printf("green wins\n");
while(!pqg.empty()) {printf("%d\n", pqg.top()); pqg.pop();}
}
if(n) printf("\n");
}
return 0;
}