A weather station measured the temperature for n days.
The number of measurements may differ from day to day.
The first line gives n.
Each of the next n lines gives one day: the number of measurements k that day and the k temperatures, separated by spaces.
The day on the first of these lines is day 1, and each later line is one day later.
The swing of a day is the highest temperature measured that day minus the lowest temperature measured that day.
A day with only one measurement has a swing of 0.
Print three lines.
On the first line, print the number of the day with the largest swing and that swing. If several days tie, choose the earliest one.
On the second line, print the highest temperature over the n days and the number of the day it first appeared.
On the third line, print the lowest temperature over the n days and the number of the day it last appeared.
Separate the two numbers on each line with one space.
Input
1 ≤ n ≤ 2,000, 1 ≤ k ≤ 24, -50 ≤ temperature ≤ 50 (integers)
Output
Three lines: day number and swing, highest temperature and day number, lowest temperature and day number
Example 1 · stdin
4
3 3 -2 7
1 5
4 -4 6 1 6
2 -4 0
stdout
3 10
7 1
-4 4
Example 2 · stdin
3
3 -5 -9 -3
2 -12 -7
2 -3 -1
stdout
1 6
-1 3
-12 2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
For the maximum and minimum of a list: Python max(a) / min(a), JS/TS Math.max(...a) / Math.min(...a), Java Math.max(x, y) inside a loop, C# a.Max() (LINQ) or Math.Max, C++ std::minmax_element (both at once), Craykon Mathf.Max / Mathf.Min inside a loop. C has none, so you compare yourself. The first trap is the starting value. Starting the highest temperature at 0 leaves 0 as the answer when every temperature is below zero, and starting the lowest at 0 leaves 0 when every temperature is above zero. Even starting at the range end -50 and asking with >, if every temperature is -50 nothing ever changes and the day number stays 0. Start with the first temperature, or with a value outside every temperature (a None check in Python, JS/TS -Infinity, Java Integer.MIN_VALUE, C# int.MinValue, C INT_MIN). Craykon has no such end-value constants, so start with the day's first temperature, or also ask a "not seen yet" marker (day number 0). The second trap is which one you keep on a tie. Asking > does not change on an equal value, so the first one stays; asking >= (<= for the minimum) changes even on an equal value, so the last one stays. Here the swing and the highest temperature keep the first one and the lowest temperature keeps the last one, so the comparison signs differ. Third, each day has a different number of temperatures, so you must read the whole line before moving to the next day.
Pythonmain.py · 16 lines
n = int(input())best_day = best_swing = Nonehi = hi_day = lo = lo_day = Nonefor day in range(1, n + 1): temps = list(map(int, input().split()))[1:] swing = max(temps) - min(temps) if best_swing is None or swing > best_swing: # 같으면 이른 날을 남긴다 best_day, best_swing = day, swing for t in temps: if hi is None or t > hi: # 처음 나온 날: 같은 값이면 바꾸지 않는다 hi, hi_day = t, day if lo is None or t <= lo: # 마지막으로 나온 날: 같은 값이어도 바꾼다 lo, lo_day = t, dayprint(best_day, best_swing)print(hi, hi_day)print(lo, lo_day)
JavaScriptmain.js · 15 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let bestDay = 0, bestSwing = -1;// 처음 값을 0으로 두면 모두 영하일 때 틀린다. 아무 기온보다 바깥인 Infinity에서 시작한다let hi = -Infinity, hiDay = 0, lo = Infinity, loDay = 0;for (let day = 1; day <= n; day++) { const temps = lines[day].trim().split(/\s+/).map(Number).slice(1); const swing = Math.max(...temps) - Math.min(...temps); if (swing > bestSwing) { bestDay = day; bestSwing = swing; } // 같으면 이른 날 for (const t of temps) { if (t > hi) { hi = t; hiDay = day; } // 처음 나온 날 if (t <= lo) { lo = t; loDay = day; } // 마지막으로 나온 날 }}console.log(`${bestDay} ${bestSwing}\n${hi} ${hiDay}\n${lo} ${loDay}`);
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let bestDay = 0, bestSwing = -1;// 처음 값을 0으로 두면 모두 영하일 때 틀린다. 아무 기온보다 바깥인 Infinity에서 시작한다let hi = -Infinity, hiDay = 0, lo = Infinity, loDay = 0;for (let day = 1; day <= n; day++) { const temps: number[] = lines[day].trim().split(/\s+/).map(Number).slice(1); const swing: number = Math.max(...temps) - Math.min(...temps); if (swing > bestSwing) { bestDay = day; bestSwing = swing; } // 같으면 이른 날 for (const t of temps) { if (t > hi) { hi = t; hiDay = day; } // 처음 나온 날 if (t <= lo) { lo = t; loDay = day; } // 마지막으로 나온 날 }}console.log(`${bestDay} ${bestSwing}\n${hi} ${hiDay}\n${lo} ${loDay}`);
JavaMain.java · 29 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); int bestDay = 0, bestSwing = -1; // 0이 아니라 int의 끝값에서 시작하면 어떤 기온이 와도 첫 값이 바로 들어간다 int hi = Integer.MIN_VALUE, hiDay = 0, lo = Integer.MAX_VALUE, loDay = 0; for (int day = 1; day <= n; day++) { StringTokenizer st = new StringTokenizer(in.readLine()); int k = Integer.parseInt(st.nextToken()); int dayHi = Integer.MIN_VALUE, dayLo = Integer.MAX_VALUE; for (int i = 0; i < k; i++) { int t = Integer.parseInt(st.nextToken()); dayHi = Math.max(dayHi, t); dayLo = Math.min(dayLo, t); if (t > hi) { hi = t; hiDay = day; } // 처음 나온 날 if (t <= lo) { lo = t; loDay = day; } // 마지막으로 나온 날 } if (dayHi - dayLo > bestSwing) { bestDay = day; bestSwing = dayHi - dayLo; } // 같으면 이른 날 } System.out.println(bestDay + " " + bestSwing); System.out.println(hi + " " + hiDay); System.out.println(lo + " " + loDay); }}
C#Main.cs · 29 lines
using System;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int bestDay = 0, bestSwing = -1; // 0이 아니라 int의 끝값에서 시작하면 어떤 기온이 와도 첫 값이 바로 들어간다 int hi = int.MinValue, hiDay = 0, lo = int.MaxValue, loDay = 0; for (int day = 1; day <= n; day++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int dayHi = int.MinValue, dayLo = int.MaxValue; for (int i = 1; i < p.Length; i++) { int t = int.Parse(p[i]); dayHi = Math.Max(dayHi, t); dayLo = Math.Min(dayLo, t); if (t > hi) { hi = t; hiDay = day; } // 처음 나온 날 if (t <= lo) { lo = t; loDay = day; } // 마지막으로 나온 날 } if (dayHi - dayLo > bestSwing) { bestDay = day; bestSwing = dayHi - dayLo; } // 같으면 이른 날 } Console.WriteLine($"{bestDay} {bestSwing}"); Console.WriteLine($"{hi} {hiDay}"); Console.WriteLine($"{lo} {loDay}"); }}
C++main.cpp · 23 lines
#include <algorithm>#include <iostream>#include <vector>int main() { int n, k; std::cin >> n; int bestDay = 0, bestSwing = -1; int hi = 0, hiDay = 0, lo = 0, loDay = 0; // hiDay가 0이면 아직 아무 기온도 보지 않은 것 for (int day = 1; day <= n; day++) { std::cin >> k; std::vector<int> temps(k); for (int &t : temps) std::cin >> t; // minmax_element는 가장 작은 것과 가장 큰 것을 한 번에 찾는다 auto [mn, mx] = std::minmax_element(temps.begin(), temps.end()); if (*mx - *mn > bestSwing) { bestDay = day; bestSwing = *mx - *mn; } // 같으면 이른 날 for (int t : temps) { if (hiDay == 0 || t > hi) { hi = t; hiDay = day; } // 처음 나온 날 if (loDay == 0 || t <= lo) { lo = t; loDay = day; } // 마지막으로 나온 날 } } std::cout << bestDay << ' ' << bestSwing << '\n' << hi << ' ' << hiDay << '\n' << lo << ' ' << loDay << '\n';}
Cmain.c · 24 lines
#include <limits.h>#include <stdio.h>int main(void) { int n, k, t; int bestDay = 0, bestSwing = -1; /* 0이 아니라 int의 끝값(INT_MIN, INT_MAX)에서 시작한다 */ int hi = INT_MIN, hiDay = 0, lo = INT_MAX, loDay = 0; scanf("%d", &n); for (int day = 1; day <= n; day++) { scanf("%d", &k); int dayHi = INT_MIN, dayLo = INT_MAX; for (int i = 0; i < k; i++) { scanf("%d", &t); if (t > dayHi) dayHi = t; if (t < dayLo) dayLo = t; if (t > hi) { hi = t; hiDay = day; } /* 처음 나온 날 */ if (t <= lo) { lo = t; loDay = day; } /* 마지막으로 나온 날 */ } if (dayHi - dayLo > bestSwing) { bestDay = day; bestSwing = dayHi - dayLo; } /* 같으면 이른 날 */ } printf("%d %d\n%d %d\n%d %d\n", bestDay, bestSwing, hi, hiDay, lo, loDay); return 0;}
Craykonmain.cray · 38 lines
n = Console.ReadLine().ToNumber();bestDay = 0;bestSwing = -1;hi = 0;hiDay = 0; // 0이면 아직 아무 기온도 보지 않은 것lo = 0;loDay = 0;for (day = 1; day <= n; day++){ p = Console.ReadNumbers(); // 그날의 첫 기온에서 시작한다(0에서 시작하면 모두 영하인 날에 틀린다) dayHi = p[1]; dayLo = p[1]; for (i = 1; i < p.Count; i++) { t = p[i]; dayHi = Mathf.Max(dayHi, t); dayLo = Mathf.Min(dayLo, t); if (hiDay == 0 || t > hi) { hi = t; hiDay = day; // 처음 나온 날 } if (loDay == 0 || t <= lo) { lo = t; loDay = day; // 마지막으로 나온 날 } } if (dayHi - dayLo > bestSwing) { bestDay = day; // 같으면 이른 날을 남긴다 bestSwing = dayHi - dayLo; }}Console.WriteLine(bestDay + " " + bestSwing);Console.WriteLine(hi + " " + hiDay);Console.WriteLine(lo + " " + loDay);