题目解析
给定n个人的点赞情况 求出每个人点赞的不同标签的数量的前3名
如果有并列,则输出标签出现次数平均值最小的那个
还有要是人数不足3人,则用-补齐缺失
解题思路
利用set将给定第i个人的点赞去重
然后对象排序 排除指定的顺序
排序的时候
按照 不同点赞的数 从大到小 排序
如果有相同 那么按标签出现次数平均值最小的那个
出现次数平均值 =
所以平均值最小的那个 也就是总数最多的一个
代码
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class Main
{
static class edge implements Comparable<edge>
{
String name;
int zan, zong;
public edge(String name, int zan, int zong)
{
this.name = name;
this.zan = zan;
this.zong = zong;
}
@Override
public int compareTo(edge other)
{
if (other.zan != this.zan)
return other.zan - this.zan;
return this.zong - other.zong;
}
}
public static void main(String[] args) throws IOException
{
int n = sc.nextInt();
ArrayList<edge> ar = new ArrayList<edge>();
for (int i = 1; i <= n; i++)
{
String name = sc.next();
int k = sc.nextInt();
TreeSet<Integer> tr = new TreeSet<Integer>();
for (int j = 1; j <= k; j++)
{
int f = sc.nextInt();
tr.add(f);
}
ar.add(new edge(name, tr.size(), k));
}
Collections.sort(ar);
for (int i = 0; i < Math.min(n, 3); i++)
{
if (i != 0)
out.print(" ");
out.print(ar.get(i).name);
}
for (int i = n; i < 3; i++)
out.print(" -");
out.flush();
out.close();
}
static InputReader sc = new InputReader();
static PrintWriter out = new PrintWriter(System.out);
static class InputReader
{
private InputStream inputStream = System.in;
private byte[] buf = new byte[100000];
private int curChar;
private int numChars;
public int getchar()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = inputStream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int a = 0, b = 1;
int c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
{
b = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9')
{
a = (a << 1) + (a << 3) + (c ^ 48);
c = getchar();
}
return a * b;
}
public String next()
{
int c = getchar();
while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1)
{
c = getchar();
}
StringBuilder res = new StringBuilder();
do
{
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = getchar();
} while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1));
return res.toString();
}
}
}