1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ABC408】A問題 - Timeout 考察から実装(c++)まで

Posted at

AtCoderBeginnerContest408の解説&感想です。
コンテストリンク

問題一覧

A問題 - Timeout

問題リンク

問題概要

単調増加な自然数列$T=(T_1,T_2,...,T_N)$が与えられる。
$T_0=0$として、$T_i + S \lt T_{i+1}$となる場所があるか判定せよ。

制約

  • $ 1 \le N,S \le 100 $
  • $ 1 \le T_i \le 1000 $
  • $ T_i < T_{i+1} $

考察

問題文の読解がちょっと難しいけど、問題概要くらいまで簡略化したら解きやすそう。
隣り合う二つで条件を満たす場所が欲しいので、adjacent_findが使えそう。
計算量は$O(N)$

実装

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;

int main(void){
    //入力
    ll N,S;
    cin>>N>>S;
    
    vll T(N+1,0);
    for(int i=1;i<=N;i++) cin>>T[i];
    
    //Tの中で、初めてT[i] +S < T[i+1]となるインデックスを取得
    ll idx = adjacent_find(T.begin(), T.end(), [&](ll a, ll b){ return a+S < b; }) - T.begin();
    cout<<(idx==N+1?"Yes":"No")<<endl;
    return 0;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?