Official
C - Chokutter Addiction Editorial by en_translator
This problem asks to implement the simulation according to the problem statement.
Since \(A\) is already sorted in the input, let us use this property to solve this problem in \(O(N)\) time.
- Manage the last timestamp \({\rm open}\) when Takahashi started chokutter last time.
- We inspect the timestamp \(a\) when Aoki passes behind Takahashi’s desk in chronological order.
- If \({\rm open} < a\), then Takahashi was using chokutter from time \({\rm open}\) to time \(a\), so add \((a-{\rm open})\) to the answer. Set \({\rm open} := a+100\), which is the next time he will open chokutter.
- Otherwise, Aoki has just passed behind Takahashi without affecting Takahashi’s behavior, so do nothing.
- Finally, if \({\rm open} < T\), then it means that Takahashi was using chokutter from time \({\rm open}\) to time \(T\), so add \((T-{\rm open})\) to the answer.
This way, the simulation is implemented appropriately, and the problem has been solved in \(O(N)\) time.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,t;
cin >> n >> t;
int res=0;
int open=0;
for(int i=0;i<n;i++){
int a;
cin >> a;
if(open<a){
res+=(a-open);
open=a+100;
}
}
if(open<t){ res+=(t-open); }
cout << res << "\n";
return 0;
}
posted:
last update: