Submission #970017

#TimeUsernameProblemLanguageResultExecution timeMemory
970017RequiemFinancial Report (JOI21_financial)C++17
31 / 100
286 ms17876 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 BIT{
    int BIT[MAXN];
    void reset(){
         memset(BIT, 0, sizeof(BIT));
    }
    void update(int id){
         id++;
         for(int i = id; i < MAXN; i += i & (-i)){
             BIT[i] += 1;
         }
    }

    int get(int id){
         int res = 0;
         id++;
         for(int i = id; i > 0; i -= i & (-i)){
             res += BIT[i];
         }
         return res;
    }

    int getrange(int l, int r){
        return get(r) - get(l - 1);
    }
};

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];
    ii b[MAXN];
    bool checkSubtask6(){
         return true;
    }

    void solveSubtask6(){
        for(int i = 1; i <= n; i++){
            b[i] = {a[i], i};
        }
        BIT BIT;
        IT IT;
        BIT.reset();
        IT.reset();
        sort(b + 1, b + 1 + n);
        for(int i = n, last = 0; i >= 1; i = last){
            last = i;
//            cout << b[last].fi << ' ' << b[last].se << endl;
            while(last >= 1 and b[last].fi == b[i].fi){
                  int l = 1, r = b[last].se, res = 0;
                  while(l <= r){
                      int mid = (l + r) >> 1;
                      if (BIT.getrange(mid, b[last].se) >= d){
                          res = mid;
                          l = mid + 1;
                      }
                      else{
                          r = mid - 1;
                      }
                  }
                  R[b[last].se] = res;
                  last--;
            }

            for(int j = i; j > last; j--){
                BIT.update(b[j].se);
            }
        }


//        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] + 1, b[last].se) + 1);
//                  cout << R[b[last].se] + 1<< ' '
//                       << b[last].se << ' '
//                       << dp[b[last].se] << ' '
//                       << IT.get(R[b[last].se] + 1, b[last].se) + 1 << 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:312:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  312 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:316:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  316 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:317:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  317 |         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...