B - 売上の少ない店 / Store with the Lowest Sales 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
Calculate the sum of sales levels for each store, and output the store with the smallest index among those with the minimum sum. In the provided code, we check whether each store satisfies this condition by comparing it with all other stores.
Analysis
First, sum up the digits in each string. For example, if the string is 203, that store’s sum is \(2+0+3=5\).
For store \(i\) to be the answer, it must satisfy both of the following conditions:
- Among the stores with a smaller index, there is no store with a sum less than or equal to store \(i\)’s sum.
- Among the stores with a larger index, there is no store with a sum strictly less than store \(i\)’s sum.
The comparison conditions differ because, in case of a tie, the store with the smaller index takes precedence.
For example, if the sums for each store are \([5,3,3]\), the answer is store \(2\). Store \(3\) also has the minimum sum, but since store \(2\) with the same sum comes before it, store \(3\) is not the answer.
If we only check whether the sum is the minimum, it may lead to an incorrect answer depending on how ties are handled. Therefore, we compare with preceding stores using “less than or equal to” and with succeeding stores using “strictly less than”.
This all-pairs store comparison takes \(O(H^2)\), which is sufficiently fast since \(H \leq 10\). Even with a naive approach, there is no need to worry about the time limit.
Algorithm
Since array indexing in the code is \(0\)-based, store the sum of store \(i+1\) in s[i].
- Read each string, convert each character to its numerical value, and add it to
s[i]. - For each index \(i\), perform the following check:
- Set
oktotrue. - If there is a store with \(j<i\) that satisfies
s[j] <= s[i], setoktofalse. - If there is a store with \(j>i\) that satisfies
s[j] < s[i], setoktofalse. - If
okremainstrue, output \(i+1\) and terminate.
- Set
No store before or after the one that passes this check has a smaller sum. Additionally, no store before it has the same sum. Therefore, the output will be “the store with the minimum sum, and with the smallest index among those.”
Complexity
- Time Complexity: \(O(HW+H^2)\)
It takes \(O(HW)\) to calculate the sums, and \(O(H^2)\) to compare each store with all other stores. - Space Complexity: \(O(H+W)\)
It uses \(O(H)\) space for the array storing the sums and \(O(W)\) space for the string being read.
Implementation Notes
A digit character
ccan be converted to an integer usingc - '0'. Add each digit individually rather than converting the entire string into an integer.Compare with preceding stores using
<=and with succeeding stores using<. This distinction is crucial for breaking ties in favor of the store with the smaller index.The array indices are \(0\)-based, but store indices are \(1\)-based, so the output is
i + 1.Source Code
// 問題文から独立に実装: 各店について「自分より前に自分以下の店がなく、後ろに自分より小さい店がない」か調べる
#include <iostream>
#include <string>
#include <vector>
int main() {
int h, w; std::cin >> h >> w;
std::vector<int> s(h, 0);
for (int i = 0; i < h; ++i) { std::string r; std::cin >> r; for (char c : r) s[i] += c - '0'; }
for (int i = 0; i < h; ++i) {
bool ok = true;
for (int j = 0; j < i; ++j) if (s[j] <= s[i]) ok = false;
for (int j = i + 1; j < h; ++j) if (s[j] < s[i]) ok = false;
if (ok) { std::cout << i + 1 << std::endl; return 0; }
}
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: