【AtCoder】ABC 449 C - Comfortable Distance
AtCoder/ABC/C問題AtCoder/ABC/300点問題AtCoder/灰DiffAtCoder/NoviSteps/未分類AtCoder/二分探索/lower_boundAtCoder/二分探索/upper_bound競技プログラミング

C - Comfortable Distance
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
AtCoder
実行時間制限: 2 sec / メモリ制限: 1024 MiB / Difficulty: 330 / NoviSteps: ??? / 配点: 300 点
問題概要
英小文字からなる長さ の文字列 が与えられるので、整数の組 であって、以下の条件をすべて満たすものの個数を求めよ。
制約
- は整数
考察
各アルファベットについて、 での出現位置(インデックス)をそれぞれ小さい順に頻度配列として記録していく。
その上で、 の 文字目に注目したとき、 として見るべき範囲は、条件の第3式を変形して
となる。
頻度配列の の要素は昇順ソートされた状態となっているはずだから、二分探索で 以上 以下のものがいくつあるかを数え、それを全ての について合計すればよい。
計算量は全体で となる。
実装例
CPP
1.#include <bits/stdc++.h>2.using namespace std;3. 4.#define all(x) (x).begin(), (x).end()5.#define rep(i, start, end) for (auto i = (start); (i) < (end); (i)++)6. 7.using ll = long long;8. 9.// ======================================== //10. 11.int main()12.{13. int N, L, R;14. string S;15. cin >> N >> L >> R >> S;16. 17. vector<vector<int>> pos(26);18. rep(i, 0, N)19. {20. pos[S[i] - 'a'].push_back(i);21. }22. 23. ll ans = 0;24. rep(i, 0, N)25. {26. int c = S[i] - 'a';27. 28. int l = i + L;29. int r = i + R;30. 31. auto itr_l = lower_bound(all(pos[c]), l);32. auto itr_r = upper_bound(all(pos[c]), r);33. 34. ans += (itr_r - itr_l);35. }36. 37. cout << ans << endl;38. 39. return 0;40.}
Submission #74141581 - AtCoder Beginner Contest 449
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
AtCoder
実装時間: 5 分
コメント
今考えると、尺取り法のようにして の計算量で解くやり方の方が楽だったかもしれない。





