Submission #970075

#TimeUsernameProblemLanguageResultExecution timeMemory
970075RequiemFinancial Report (JOI21_financial)C++17
100 / 100
1040 ms23308 KiB
#include<bits/stdc++.h> //#define int long long #define pb push_back #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #define MOD 1000000007 #define INF 1e9 #define fi first #define se second #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORD(i,a,b) for(int i=a;i>=b;i--) #define sz(a) ((int)(a).size()) #define endl '\n' #define pi 3.14159265359 #define TASKNAME "financialreport" template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; } using namespace std; typedef pair<int,int> ii; typedef pair<int,ii> iii; typedef vector<int> vi; const int MAXN = 3e5 + 9; int a[MAXN], n, d; namespace subtask1{ bool checkSubtask1(){ return n <= 20; } void solveSubtask1(){ int res = 0; for(int msk = 0; msk < (1 << (n - 1)); msk++){ int prv = -1, mx = -INF, valid = 1, cur = 0; for(int j = 0; j < n; j++){ if ((msk & (1 << j)) or j == n - 1) { if (prv != -1 and j - prv > d) valid = 0; if (maximize(mx, a[j + 1])){ cur++; } prv = j; } } if (valid) maximize(res, cur); } cout << res << endl; } } namespace subtask2{ bool checkSubtask2(){ return n <= 400; } int dp[406][406]; vector<int> listval; void computeDp1(){ memset(dp, -0x3f, sizeof(dp)); for(int i = 1; i <= n; i++){ dp[i][i] = 1; } a[0] = -INF; for(int i = 0; i <= n; i++){ //regard i for(int j = 0; j <= n; j++){ if (dp[i][j] > -INF){ // cout << i << ' ' << j << ' ' << dp[i][j] << endl; for(int k = i + 1; k <= min(i + d, n); k++){ int mxId = (a[j] >= a[k]) ? j : k; if (mxId == k) maximize(dp[k][mxId], dp[i][j] + 1); else maximize(dp[k][mxId], dp[i][j]); } } } } int res = 0; for(int i = 0; i <= n; i++){ maximize(res, dp[n][i]); } cout << res << endl; } void computeDp2(){ for(int i = 1; i <= n; i++){ listval.pb(a[i]); } sort(listval.begin(), listval.end()); listval.erase(unique(listval.begin(), listval.end()), listval.end()); } void solveSubtask2(){ computeDp1(); } } namespace subtask3{ int Valid[7003][7003]; bool checkSubtask3(){ return n <= 7000; } int dp[MAXN]; void solveSubtask3(){ memset(Valid, 0x3f, sizeof(Valid)); deque<int> opt; for(int i = 1; i <= n; i++){ Valid[i][i] = a[i]; for(int j = i; j <= n; j++){ if (j - i <= d) Valid[i][j] = max(a[i], a[j]); else minimize(Valid[i][j], max(Valid[i][opt.front()], a[j]) ); while(!opt.empty() and Valid[i][j] <= Valid[i][opt.back()]) opt.pop_back(); opt.push_back(j); while(!opt.empty() and opt.front() + d <= j) { opt.pop_front(); } // cout << Valid[i][j] << ' '; } opt.clear(); // cout << endl; } int res = 0; for(int i = 1; i <= n; i++){ dp[i] = 1; for(int j = i - 1; j >= 1; j--){ if (Valid[j][i] <= a[i] and a[i] > a[j]) maximize(dp[i], dp[j] + 1); } maximize(res, dp[i]); // cout << dp[i] << ' '; } // cout << endl; cout << res << endl; } } namespace subtask4{ bool checkSubtask4(){ return d == 1; } void solveSubtask4(){ stack<int> st; int res = 1; st.push(n); for(int i = n - 1; i >= 1; i--){ while(!st.empty() and a[i] >= a[st.top()]) st.pop(); st.push(i); maximize(res, (int) st.size()); } cout << res << endl; } } namespace subtask5{ bool checkSubtask5(){ return d == n; } int dp[MAXN]; void solveSubtask5(){ multiset<int> st; int res = 0; for(int i = 1; i <= n; i++){ auto it = st.lower_bound(a[i]); // cout << st.size() << ' ' << i << endl; if (it == st.end()) st.insert(a[i]); else { st.erase(it); st.insert(a[i]); } } cout << (int) st.size() << endl; } } struct DSU{ int par[MAXN], ranking[MAXN], ending[MAXN]; multiset<int> component; void reset(){ for(int i = 1; i <= n; i++){ ending[i] = i; ranking[i] = 1; par[i] = i; } } int root(int u){ if (par[u] == u) return u; else return par[u] = root(par[u]); } void unite(int u,int v){ u = root(u); v = root(v); if (u != v){ if (ranking[u] >= d) component.erase(ending[u]); if (ranking[v] >= d) component.erase(ending[v]); if (ranking[u] < ranking[v]) swap(u, v); par[v] = u; ranking[u] += ranking[v]; maximize(ending[u], ending[v]); if (ranking[u] >= d) component.insert(ending[u]); } } }; struct IT{ int st[MAXN << 2]; void reset(){ memset(st, -0x3f, sizeof(st)); } void update(int node, int l, int r, int pos,int x){ if (l == r){ st[node] = x; return; } int mid = (l + r) >> 1; if (pos <= mid) update(node << 1, l, mid, pos, x); else update(node << 1 | 1, mid + 1, r, pos, x); st[node] = max(st[node << 1], st[node << 1 | 1]); // cout << "UPD: " << node << ' ' << l << ' ' <<r << ' ' << pos << ' ' << x << ' ' << st[node] << endl; } int get(int node, int l,int r, int u, int v){ if (l >= u and r <= v){ return st[node]; } if (l > v or r < u) return -INF; int mid = (l + r) >> 1; return max(get(node << 1, l, mid, u, v), get(node << 1 | 1, mid + 1, r, u, v)); } int get(int u, int v){ return get(1, 1, n, u, v); } void update(int pos, int x){ update(1, 1, n, pos, x); } }; namespace subtask6{ int dp[MAXN], R[MAXN], revive[MAXN]; ii b[MAXN]; bool checkSubtask6(){ return true; } DSU DSU; bool check(int i, int mid){ auto it = DSU.component.lower_bound(mid); int numzero = 0; if (it == DSU.component.end()) numzero = 0; else{ int e = *it; int sz = DSU.ranking[DSU.root(e)]; int s = e - sz + 1; maximize(numzero, min(e, i) - max(s, mid) + 1); it++; if (it != DSU.component.end()){ int e = *it; int sz = DSU.ranking[DSU.root(e)]; int s = e - sz + 1; maximize(numzero, min(e, i) - max(s, mid) + 1); } } return numzero < d; } void solveSubtask6(){ for(int i = 1; i <= n; i++){ b[i] = {a[i], i}; } IT IT; DSU.reset(); IT.reset(); memset(revive, 0, sizeof(revive)); sort(b + 1, b + 1 + n); for(int i = n, last; i >= 1; i = last){ last = i; while(last >= 1 and b[last].fi == b[i].fi){ int l = 0, r = b[last].se, res = 0; while(l <= r){ int mid = (l + r) >> 1; if (check(b[last].se, mid)){ res = mid; r = mid - 1; } else{ l = mid + 1; } } // if (b[last].se == 6){ // if (check(6, 2)){ // cout << "YES" << endl; // } // else cout << "NO" << endl; // } R[b[last].se] = res; // cout << res << ' ' << b[last].se << endl; last--; } for(int j = i; j > last; j--){ if (1 >= d) DSU.component.insert(b[j].se); revive[b[j].se] = 1; if (b[j].se > 1 and revive[b[j].se - 1]) DSU.unite(b[j].se - 1, b[j].se); if (b[j].se < n and revive[b[j].se + 1]) DSU.unite(b[j].se, b[j].se + 1); } } for(int i = 1; i <= n; i++){ a[b[i].se] = i; } // for(int i = 1; i <= n; i++){ // cout << a[i] << ' '; // } // cout << endl; // for(int i = 1; i <= n; i++){ // cout << R[i] << ' '; // } // cout << endl; int res = 0; for(int i = 1, last = 0; i <= n; i = last){ last = i; while(last <= n and b[last].fi == b[i].fi){ dp[b[last].se] = max(1, IT.get(R[b[last].se], b[last].se) + 1); // cout << R[b[last].se] + 1<< ' ' // << b[last].se << ' ' // << dp[b[last].se] << ' ' // << endl; maximize(res, dp[b[last].se]); last++; } for(int j = i; j < last; j++){ IT.update(b[j].se, dp[b[j].se]); } } // for(int i = 1; i <= n; i++){ // cout <<dp[i] << ' ' ; // } // cout << endl; cout << res << endl; } } main() { fast; if (fopen(TASKNAME".inp","r")){ freopen(TASKNAME".inp","r",stdin); freopen(TASKNAME".out","w",stdout); } cin >> n >> d; for(int i = 1; i <= n; i++){ cin >> a[i]; } // if (subtask1::checkSubtask1()) return subtask1::solveSubtask1(), 0; // if (subtask2::checkSubtask2()) return subtask2::solveSubtask2(), 0; // if (subtask3::checkSubtask3()) return subtask3::solveSubtask3(), 0; // if (subtask4::checkSubtask4()) return subtask4::solveSubtask4(), 0; // if (subtask5::checkSubtask5()) return subtask5::solveSubtask5(), 0; if (subtask6::checkSubtask6()) return subtask6::solveSubtask6(), 0; } /** Warning: - MLE / TLE? - Gioi han mang? - Gia tri max phai luon gan cho -INF - long long co can thiet khong? - tran mang. - code can than hon - Nho sinh test de tranh RTE / TLE --> Coi lai truoc khi nop **/

Compilation message (stderr)

Main.cpp: In function 'void subtask5::solveSubtask5()':
Main.cpp:162:13: warning: unused variable 'res' [-Wunused-variable]
  162 |         int res = 0;
      |             ^~~
Main.cpp: At global scope:
Main.cpp:354:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  354 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:358:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  358 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:359:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  359 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...