Nias and Tug-of-War
时间: 1ms 内存:128M
描述:
Nias is fond of tug-of-war. One day, he organized a tug-of-war game and invited a group of friends to take part in.
Nias will divide them into two groups. The strategy is simple, sorting them into a row according to their height from short to tall, then let them say one and two alternately (i.e. one, two, one, two...). The people who say one are the members of the red team while others are the members of the blue team.
We know that the team which has a larger sum of weight has advantages in the tug-of-war. Now give you the guys' heights and weights, please tell us which team has advantages.
输入:
The first line of input contains an integer T, indicating the number of test cases.
The first line of each test case contains an integer N (N is even and 6 ≤ N ≤ 100).
Each of the next N lines contains two real numbers X and Y, representing the height and weight of a friend respectively.
输出:
One line for each test case. If the red team is more likely to win, output "red", if the blue team is more likely to win, output "blue". If both teams have the same weight, output "fair".
示例输入:
1
6
170 55
165.3 52.5
180.2 60.3
173.3 62.3
175 57
162.2 50
示例输出:
blue
提示:
参考答案(内存最优[1092]):
#include <stdio.h>
int main()
{
double a[110],b[110];
int n,q;
double red=0,blue=0,t,count;
scanf("%d",&n);
while(n--)
{
red=0,blue=0;
scanf("%d",&q);
for(int i=0; i<q; i++)
{
scanf("%lf%lf",&a[i],&b[i]);
}
for(int j=1; j<q; j++)
for(int i=0; i<q-1; i++)
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
count=b[i];
b[i]=b[i+1];
b[i+1]=count;
}
for(int i=0; i<q; i++)
{
if(i%2==0)
{
red=red+b[i];
}
else
{
blue=blue+b[i];
}
}
if(red>blue)
{
printf("red\n");
}
else if(red<blue)
{
printf("blue\n");
}
if(red==blue)
{
printf("fair\n");
}
}
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
#define eps 1e-7
struct P
{
double height;
double weight;
} p[101];
int comage(const void* e1,const void* e2)
{
return ((P*)e1)->height-((P*)e2)->height;
}
int main()
{
int T;
int n;
int i;
double red,blue;
cin>>T;
while(T--)
{
cin>>n;
red=0;
blue=0;
for(i=0; i<n; i++)
{
cin>>p[i].height>>p[i].weight;
}
qsort(p,n,sizeof(p[0]),comage);
for(i=0; i<n; i++)
{
if(i%2==0)
red+=p[i].weight;
else
blue+=p[i].weight;
}
if(fabs(red-blue)<eps)
cout<<"fair"<<endl;
else if(red>blue)
cout<<"red"<<endl;
else if(red<blue)
cout<<"blue"<<endl;
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。