写一段Java实现Input In the first line you are given the number n of billionaires (1 ≤ n ≤ 10000). The following n lines contain information about these people: their names, cities where they were staying at the beginning of the period, and their fortunes. In the next line you are given the number m of days in the period for which you have the information (1 ≤ m ≤ 50000) and the number k of travels of the billionaires (0 ≤ k ≤ 50000). The following k lines contain the list of travels in the following format: the number of the day (from 1 to m−1), the name of the person, and the city of destination. You may assume that billionaires depart late at night and arrive to the destination city on the next day's morning. They cannot make more than one travel each day. The numbers of days in the list are not decreasing. All names of people and cities consist of at most 20 English letters; you must take into consideration the case of the symbols. The fortunes are in the range from 1 to 100 billions (one billion is a thousand million). Output In each line of the output give the name of a city and, after a space, the number of days during which this city was the first with respect to the sum of fortunes of the billionaires staying there. Leave out those cities for which there were no such days. The cities must be sorted alphabetically (with the usual symbol order: ABC...Zabc...z).
时间: 2024-01-29 19:02:13 浏览: 106
以下是Java实现:
```java
import java.util.*;
public class Billionaires {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<String, List<Billionaire>> billionairesByCity = new HashMap<>();
for (int i = 0; i < n; i++) {
String name = sc.next();
String city = sc.next();
long fortune = sc.nextLong();
Billionaire billionaire = new Billionaire(name, city, fortune);
if (!billionairesByCity.containsKey(city)) {
billionairesByCity.put(city, new ArrayList<>());
}
billionairesByCity.get(city).add(billionaire);
}
int m = sc.nextInt();
int k = sc.nextInt();
for (int i = 0; i < k; i++) {
int day = sc.nextInt();
String name = sc.next();
String city = sc.next();
List<Billionaire> billionaires = billionairesByCity.get(city);
for (Billionaire billionaire : billionaires) {
if (billionaire.name.equals(name)) {
billionaire.addTravel(day);
break;
}
}
}
List<String> cities = new ArrayList<>(billionairesByCity.keySet());
Collections.sort(cities);
for (String city : cities) {
List<Billionaire> billionaires = billionairesByCity.get(city);
Map<Integer, Long> wealthByDay = new HashMap<>();
for (Billionaire billionaire : billionaires) {
for (int day : billionaire.travels) {
wealthByDay.put(day, wealthByDay.getOrDefault(day, 0L) + billionaire.fortune);
}
}
int maxDays = 0;
for (int day : wealthByDay.keySet()) {
long wealth = wealthByDay.get(day);
for (int i = day + 1; i <= m; i++) {
long futureWealth = wealthByDay.getOrDefault(i, 0L);
if (futureWealth > wealth) {
break;
}
if (i - day > maxDays) {
maxDays = i - day;
}
}
}
if (maxDays > 0) {
System.out.println(city + " " + maxDays);
}
}
}
static class Billionaire {
String name;
String city;
long fortune;
List<Integer> travels = new ArrayList<>();
public Billionaire(String name, String city, long fortune) {
this.name = name;
this.city = city;
this.fortune = fortune;
}
public void addTravel(int day) {
travels.add(day);
}
}
}
```
首先,我们读入$n$个亿万富翁的信息,并将它们按照城市分组。我们使用`Map`将每个城市映射到一个亿万富翁列表。对于每个亿万富翁,我们将其添加到其所在城市的列表中。
接下来,我们读入$k$个亿万富翁的旅行记录。对于每个旅行记录,我们找到相应的亿万富翁,并将旅行记录添加到其旅行列表中。
然后,我们对于每个城市,计算每个亿万富翁在每一天的财富总和,并找出对于每个城市,在哪些连续的天数内,其财富总和比其他城市的财富总和更高。我们使用一个`Map`将每个天数映射到该天数的财富总和。对于每个天数,我们计算从该天数开始的最长连续天数,使得该城市的财富总和在这段时间内最高。我们使用一个变量`maxDays`来记录所有城市的最长连续天数。
最后,我们按照字典序对城市进行排序,并输出每个城市的最长连续天数。如果某个城市没有连续的天数使得其财富总和最高,则不输出该城市。
阅读全文