Boys vs Girls (25)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.
输入描述:
Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.
输出描述:
For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM. If one such kind of student is missing, output “Absent” in the corresponding line, and output “NA” in the third line instead.
输入例子:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
输出例子:
Mary EE990830
Joe Math990112
6
题解:字符串和最值的查找,不断更新temp和temp1,分别找到男最低值和女最高值相减,注意题意条件判断。
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 200010
template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}
const int maxn=100001;
struct stu
{
string name;
char gen;
string id;
int sc;
}st[maxn];
int main()
{
int n,f1=0,f2=0;
cin>>n;
rep(i,0,n)
{
cin>>st[i].name>>st[i].gen>>st[i].id>>st[i].sc;
}
stu temp,temp2;
temp.sc=100,temp2.sc=0;
rep(i,0,n)
{
if((st[i].sc<temp.sc)&&(st[i].gen=='M'))
{
temp=st[i];
f1=1;
}
if((st[i].sc>temp2.sc)&&(st[i].gen=='F'))
{
temp2=st[i];
f2=1;
}
}
if(f2==1)
{
cout<<temp2.name<<" "<<temp2.id<<endl;
}
else
{
cout<<"Absent"<<endl;
}
if(f1==1)
{
cout<<temp.name<<" "<<temp.id<<endl;
}
else
{
cout<<"Absent"<<endl;
}
if(f1==1&&f2==1)
{
cout<<temp2.sc-temp.sc<<endl;
}
else
{
cout<<"NA"<<endl;
}
return 0;
}