题目解析
给定n个人的邮箱以及分数
求一共发出了面值多少元的PAT代金券 以及 排名为前K名的同学信息,允许并列
解题思路
在读入的时候计算当前能获得多少代金券,并且累加
再按分数降序,邮箱升序的排序
最后输出的时候,如果当前这个人的成绩 与 前一个人的成绩 不相等, 那么就需要更新排名。且保证输出的这个排名要 比 最低排名的数 小
注: Java怎么样都是TLE的 150ms......
代码
import java.io.*;
import java.util.*;
public class Main
{
static class edge implements Comparable<edge>
{
String email; // 邮箱
int score; // 成绩
public edge(String email, int score)
{
this.email = email;
this.score = score;
}
@Override
public int compareTo(edge other)
{
if (score != other.score) return other.score - score; // 成绩降序
return email.compareTo(other.email); // 邮箱升序
}
}
static int get(int x, int g)
{
if (x >= g) return 50; // [G, 100] 区间内,可以得到50的
if (x >= 60) return 20; // [60, G) 区间内,可以得到20的
return 0; // 否则没有
}
public static void main(String[] args)
{
int n = sc.nextInt();
int g = sc.nextInt();
int k = sc.nextInt();
edge shu[] = new edge[n + 10];
shu[0] = new edge("", 0);
int ans = 0; // 总面值
for (int i = 1; i <= n; i++)
{
String email = sc.next();
int score = sc.nextInt();
shu[i] = new edge(email, score);
ans += get(score, g); // 总面值 + 当前发出的
}
Arrays.sort(shu, 1, n + 1); // 排序
out.println(ans);
int cnt = 0; // 名次
for (int i = 1; i <= n; i++)
{
if (shu[i].score != shu[i - 1].score) cnt = i; // 如果当前成绩 不等于 前一个人的成绩, 那么就需要更新排名
if (cnt > k) break; // 如果 名次大于最低名次,则需要退出循环
out.println(cnt + " " + shu[i].email + " " + shu[i].score);
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
c++
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10;
struct edge
{
string email;
int score;
} shu[N];
int get(int x, int g)
{
if(x >= g) return 50;
if(x >= 60) return 20;
return 0;
}
bool cmp(edge a, edge b)
{
if(a.score != b.score) return a.score > b.score;
return a.email < b.email;
}
int main()
{
int n, g, k; cin >> n >> g >> k;
int ans = 0;
for(int i = 1; i <= n; i ++)
{
cin >> shu[i].email >> shu[i].score;
ans += get(shu[i].score, g);
}
sort(shu + 1, shu + n + 1, cmp);
cout << ans << endl;
int res = 0, cnt = 0;
for(int i = 1; i <= n; i ++)
{
res++;
if(shu[i].score != shu[i - 1].score) cnt = res;
if(res > k && cnt == res) break;
cout << cnt << " " << shu[i].email << " " << shu[i].score << endl;
}
return 0;
}