Submission #969970

#TimeUsernameProblemLanguageResultExecution timeMemory
969970RequiemFinancial Report (JOI21_financial)C++17
5 / 100
96 ms18932 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 1e18
#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;
    }
}

namespace subtask6{
    int dp[MAXN];
    bool checkSubtask6(){
         return true;
    }
    multiset<int> st;
    void solveSubtask6(){
        for(int i = 1; i <= n; i++){
            auto it = st.lower_bound(a[i]);
            if (it == st.end()) st.insert(a[i]);
            else {
                st.erase(it);
                st.insert(a[i]);
            }
        }

        cout << (int) st.size() << 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:196:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  196 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  200 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  201 |         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...