- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 86 lines of C++ from the credited upstream file ccc23s5.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 3 loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 34 5#include <iostream>6#include <math.h>7#include <unordered_map>8#include <vector>9#define int long long10using namespace std;11double n;12 13 14vector<int> finalN;15 16 1718void filter(double l, double r, int iteration){19 if(iteration == 0){ 20 for(int i = ceil(l); i <= floor(r); i++){21 finalN.push_back(i);22 }23 }else{24 25 26 27 double leftEdge = (r-l)/3 + l;28 double rightEdge = 2*(r-l)/3 + l;29 30 31 32 filter(l, leftEdge, iteration-1);33 filter(rightEdge, r, iteration-1);34 }35}36 37 38394041bool preciseFilter(int x){42 if(x == 0){43 return true;44 }45 unordered_map<int, bool> cycle;46 47 48 while(true){49 if(x*3 <= n){50 x*=3;51 if(cycle[x]){52 return true;53 }54 cycle[x] = true;55 }else if(x*3 >= n*2){56 x*=3;57 x-=n*2;58 if(cycle[x]){59 return true;60 }61 cycle[x] = true;62 }else{ 63 return false;64 }65 }66 67 68}69 70 71signed main() {72 cin >> n;73 74 75 filter(0.0, n, 20);76 for(auto i: finalN){77 if(preciseFilter(i)){78 cout << i << '\n';79 }80 81 82 }83 84 85 return 0;86}