写一段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 21:02:07 浏览: 171
以下是Java实现代码:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Map<String, Integer> cityDays = new TreeMap<>();
Map<String, Integer> cityFortunes = new HashMap<>();
Map<String, String> billionaireCities = new HashMap<>();
for (int i = 0; i < n; i++) {
String name = scanner.next();
String city = scanner.next();
int fortune = scanner.nextInt();
billionaireCities.put(name, city);
cityFortunes.put(city, cityFortunes.getOrDefault(city, 0) + fortune);
}
int m = scanner.nextInt();
int k = scanner.nextInt();
for (int i = 0; i < k; i++) {
int day = scanner.nextInt();
String name = scanner.next();
String destCity = scanner.next();
String origCity = billionaireCities.get(name);
int fortune = cityFortunes.get(origCity);
if (cityDays.get(origCity) == null) {
cityDays.put(origCity, 0);
}
if (cityDays.get(destCity) == null) {
cityDays.put(destCity, 0);
}
cityDays.put(origCity, cityDays.get(origCity) + day - cityDays.get(destCity));
cityDays.put(destCity, day);
cityFortunes.put(origCity, fortune - cityFortunes.getOrDefault(origCity, 0));
cityFortunes.put(destCity, cityFortunes.getOrDefault(destCity, 0) + fortune);
billionaireCities.put(name, destCity);
}
List<String> result = new ArrayList<>();
for (Map.Entry<String, Integer> entry : cityDays.entrySet()) {
String city = entry.getKey();
int days = entry.getValue();
if (days > 0) {
result.add(city + " " + days);
}
}
for (String s : result) {
System.out.println(s);
}
}
}
```
解释一下代码:
首先读取输入的数据,使用Map<String, Integer> cityDays记录每个城市的首富天数,Map<String, Integer> cityFortunes记录每个城市的首富总财富,Map<String, String> billionaireCities记录每个亿万富翁所在的城市。
接着,遍历每个亿万富翁,将其所在城市的首富总财富加入到cityFortunes中。然后,读取每个亿万富翁的旅行记录,更新cityDays、cityFortunes和billionaireCities。对于每条旅行记录,先计算出旅行前该亿万富翁所在城市的首富天数,然后将其更新为旅行前最后一次到达该城市的天数减去旅行天数,将目的地城市的首富天数更新为旅行天数,更新原城市和目的地城市的首富总财富,并将该亿万富翁所在城市更新为目的地城市。
最后,遍历cityDays,将首富天数大于0的城市加入到结果列表中,并按照字典序排序输出。
阅读全文