Official

B - Pick Two Editorial by en_translator


The packages are paired in ascending order of the section numbers, and then they are carried out successively.
Therefore, if we can enumerate all the section numbers with packages, we can pair them two by two and print them to answer to the problem correctly.

In this problem, you are only given the string \(S\) representing the state of the warehouse. How can we enumerate the section numbers with packages in ascending order based on this \(S\)?

For example, we can take the following approach.

  • First, find the length of \(S\).
    • It is useful to use the feature of finding the length of a string, which is provided in most languages.
  • Then, scan \(S\) from the beginning.
    • Every occurrence of # indicates the existence of a package in that section.
    • When you find one, insert the section number to the end of the list of the packages’ section numbers. It is useful to use a variable-length array.

For more details, please refer to the sample code below.
Note that some languages regard the first character as the “\(0\)-th” element, and you may need to add \(1\) to the indices to answer correctly.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string S;
  cin >> S;
  vector<int> a;
  for(int i=0;i<S.size();i++){
    if(S[i]=='#'){ a.push_back(i+1); }
  }
  for(int i=0;i<a.size();i+=2){
    cout << a[i] << "," << a[i+1] << "\n";
  }
  return 0;
}

posted:
last update: