团体程序设计天梯赛 L2-019 悄悄关注 (25分)(java)
新浪微博上有个“悄悄关注”,一个用户悄悄关注的人,不出现在这个用户的关注列表上,但系统会推送其悄悄关注的人发表的微博给该用户。现在我们来做一回网络侦探,根据某人的关注列表和其对其他用户的点赞情况,扒出有可能被其悄悄关注的人。
输入格式:
输入首先在第一行给出某用户的关注列表,格式如下:
人数N 用户1 用户2 …… 用户N
其中N是不超过5000的正整数,每个用户i(i=1, …, N)是被其关注的用户的ID,是长度为4位的由数字和英文字母组成的字符串,各项间以空格分隔。
之后给出该用户点赞的信息:首先给出一个不超过10000的正整数M,随后M行,每行给出一个被其点赞的用户ID和对该用户的点赞次数(不超过1000),以空格分隔。注意:用户ID是一个用户的唯一身份标识。题目保证在关注列表中没有重复用户,在点赞信息中也没有重复用户。
输出格式:
我们认为被该用户点赞次数大于其点赞平均数、且不在其关注列表上的人,很可能是其悄悄关注的人。根据这个假设,请你按用户ID字母序的升序输出可能是其悄悄关注的人,每行1个ID。如果其实并没有这样的人,则输出“Bing Mei You”。
输入样例1:
10 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao
8
Magi 50
Pota 30
LLao 3
Ammy 48
Dave 15
GAO3 31
Zoro 1
Cath 60
输出样例1:
Ammy
Cath
Pota
输入样例2:
11 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao Pota
7
Magi 50
Pota 30
LLao 48
Ammy 3
Dave 15
GAO3 31
Zoro 29
输出样例2:
Bing Mei You
用链表记录的数据,最后一个测试点超时了。TAT
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String fo[] = in.readLine().split(" ");
Tool tool = new Tool();
int n = Integer.parseInt(in.readLine());
for(int i=0;i
{
String s[] = in.readLine().split(" ");
Node node = new Node(s[0], s[1]);
tool.add(node);
}
int average = tool.getSum()/n;
ArrayList id = new ArrayList<>();
ArrayList realId = new ArrayList<>();
id = tool.getId(average);
for(int i=0;i
{
int count = 0;
for(int j=1;j<=Integer.parseInt(fo[0]);j++)
{
if(id.get(i).equals(fo[j]))
{
break;
}
count++;
if(count == Integer.parseInt(fo[0]))
{
realId.add(id.get(i));
}
}
}
String rId[] = new String[realId.size()];
for(int i=0;i
{
rId[i] = realId.get(i);
}
Arrays.sort(rId);
for(int i=0;i
{
System.out.println(rId[i]);
}
if(rId.length==0)
{
System.out.println("Bing Mei You");
}
}
}
class Tool
{
Node head = new Node("", "");
public void add(Node node)
{
Node temp = head;
while(true)
{
if(temp.next == null)
{
break;
}
temp = temp.next;
}
temp.next = node;
}
public int getSum()
{
Node temp = head.next;
int sum = 0;
while(true)
{
if(temp == null)
{
break;
}
sum+=Integer.parseInt(temp.num);
temp = temp.next;
}
return sum;
}
public ArrayList getId(int average)
{
ArrayList id = new ArrayList<>();
Node temp = head.next;
while(true)
{
if(temp == null)
{
break;
}
if(Integer.parseInt(temp.num)>average)
{
id.add(temp.name);
}
temp = temp.next;
}
return id;
}
}
class Node
{
String name;
String num;
Node next;
public Node(String name, String num)
{
this.name = name;
this.num = num;
}
}