Submission #813676

#TimeUsernameProblemLanguageResultExecution timeMemory
813676CookieFinancial Report (JOI21_financial)C++14
0 / 100
168 ms54236 KiB
#include<bits/stdc++.h> #include<fstream> using namespace std; ifstream fin("VNOICUP.INP"); ofstream fout("VNOICUP.OUT"); #define sz(a) (int)a.size() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> const ld PI = 3.14159265359; using u128 = __uint128_t; //const int x[4] = {1, -1, 0, 0}; //const int y[4] = {0, 0, 1, -1}; const ll mod = 1e9 + 9; const int mxn = 1e6 + 5, mxq = 2e5 + 5, sq = 400, mxv = 1e5 + 5; //const int base = (1 << 18); const int inf = 1e9, neg = -69420; struct DSU{ vt<pii>past_p, past_sz; vt<int>checkpoint; int p[mxn + 1], sz[mxn + 1]; void init(){ for(int i = 1; i <= mxn; i++){ p[i] = i; sz[i] = 1; } past_sz.clear(); past_p.clear(); checkpoint.clear(); } int fp(int a){ if(p[a] == a)return(a); return(fp(p[a])); } bool check(int u, int v){ return(fp(u) == fp(v)); } void unon(int a, int b){ a = fp(a); b = fp(b); if(a > b)swap(a, b); //past_sz.pb({a, sz[a]}); past_p.pb({b, p[b]}); if(a != b){ sz[a] += sz[b]; p[b] = a; } } void rollback(){ //int before = p[past_p.back().fi], after = past_p.back().se; //if(before != past_p.back().fi && after == past_p.back().fi)cnt++; p[past_p.back().fi] = past_p.back().se; past_p.pop_back(); sz[past_sz.back().fi] = past_sz.back().se; past_sz.pop_back(); } void save(){ checkpoint.pb(past_sz.size()); } void to_last(){ while(past_sz.size() != checkpoint.back())rollback(); checkpoint.pop_back(); } }; DSU dsu; int n, d; int a[mxn + 1], dp[mxn + 1]; vt<int>idx[mxn + 1]; vt<int>comp; int find(int x){ return(lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1); } void compress(){ for(int i = 1; i <= n; i++)comp.pb(a[i]); sort(comp.begin(), comp.end()); for(int i = 1; i <= n; i++){ a[i] = find(a[i]); } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d; dsu.init(); for(int i = 1; i <= n; i++)cin >> a[i]; compress(); for(int i = 1; i <= n; i++){ idx[a[i]].pb(i); dp[i] = -1; } set<int>save; int ans = 1; for(int i = 1; i <= n; i++){ if(sz(idx[i]) == 0)continue; reverse(idx[i].begin(), idx[i].end()); for(int j = 0; j < sz(idx[i]); j++){ int x = idx[i][j]; auto lw = save.lower_bound(x); if(lw != save.begin()){ --lw; if((x - *lw) <= d){ dsu.unon(x, *lw); } } auto hg = save.upper_bound(x); if(hg != save.end()){ if((*hg - x) <= d){ dsu.unon(x, *hg); } } int lft = dsu.fp(x); dp[x] = 1; for(int l = x - 1; l >= lft; l--){ dp[x] = max(dp[x], dp[l] + 1); } save.insert(x); ans = max(ans, dp[x]); //cout << x << " " << dp[x] << "\n"; } } for(int i = 1; i <= n; i++)cout << dp[i] << ' '; cout << ans; }

Compilation message (stderr)

Main.cpp: In member function 'void DSU::to_last()':
Main.cpp:61:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   61 |         while(past_sz.size() != checkpoint.back())rollback();
      |               ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...