답안 #1112328

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1112328 2024-11-14T04:29:12 Z Requiem Teams (CEOI11_tea) C++17
100 / 100
564 ms 125880 KB
#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 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 "divteam"
using namespace std;
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; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;

/**
Ta có thể dễ dàng tính được số team nhiều nhất bằng dp thông thường.

Bây giờ ta cần kiểm soát kích thước của tập lớn nhất.

Giả sử tập lớn nhất là mid.
dp_i = dp_j + 1 (j thuộc [i - mid, i - a[i] ].

Ta nhận thấy rằng là i - mid sẽ tăng dần, trong khi i - a[j] sẽ giảm dần.
Mình có thể dùng deque ko nhỉ.
**/



const int MAXN = 1e6 + 1;

vector<ii> query[MAXN];
vector<int> listPosition[MAXN];
int pos_ptr[MAXN];
int ord[MAXN], cnt[MAXN], newOrd[MAXN];
int n;
int dp[MAXN], trace[MAXN], mx = 0;

void fastscan(int &number){
    //variable to indicate sign of input number
    bool negative = false;
    register int c;

    number = 0;

    // extract current character from buffer
    c = getchar();
    if (c=='-')
    {
        // number is negative
        negative = true;

        // extract the next character from the buffer
        c = getchar();
    }

    // Keep on extracting characters if they are integers
    // i.e ASCII Value lies from '0'(48) to '9' (57)
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;

    // if scanned input has a negative sign, negate the
    // value of the input number
    if (negative)
        number *= -1;
}

int stk[MAXN], sz_stk = 0;

int lab[MAXN];

inline int root(int u){
    return (lab[u] < 0) ? u : lab[u] = root(lab[u]);
}

bool check(int mid){
    memset(dp, -0x3f, sizeof(dp));
    dp[0] = 0;

    memset(lab, -1, sizeof(lab));
    FOR(i, 1, n){
        if (i - ord[i] >= 0) query[i - ord[i]].pb({max(0, i - mid), i});
    }

    sz_stk = 0;
    FOR(i, 0, n){
        if (sz_stk == 0) stk[sz_stk++] = i;
        else{
            while(sz_stk > 0 and dp[stk[ sz_stk - 1 ] ]< dp[i]){
                lab[stk[sz_stk - 1]] = i;
                sz_stk--;
            }
            stk[sz_stk++] = i;

        }
        for(auto &p: query[i]){
            dp[p.se] = dp[root(p.fi)] + 1;
            trace[p.se] = root(p.fi);
        }
    }
    FOR(i, 1, n){
        if (i - ord[i] >= 0) query[i - ord[i]].pop_back();
    }
    return dp[n] == mx;
}

void traced(int s){
    while(true){
        int tr = trace[s];
        cout << s - tr << ' ';
        FOR(i, tr + 1, s){
            cout << listPosition[ord[i]][pos_ptr[ord[i]] - 1] << ' ';
            pos_ptr[ord[i]]--;
        }
        cout << endl;
        if (tr == 0) return;
        s = trace[s];
    }
}

main()
{
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    cin >> n;

    int mxA = 0;
    FOR(i, 1, n){
        cin >> ord[i];

        listPosition[ord[i]].pb(i);
        pos_ptr[ord[i]]++;
        maximize(mxA, ord[i]);

        cnt[ord[i]]++;
    }

    FOR(i, 1, n){
        cnt[i] += cnt[i - 1];
    }

    FOR(i, 1, n){
        newOrd[cnt[ord[i]]--] = ord[i];
    }
    FOR(i, 1, n){
        ord[i] = newOrd[i];
    }

    check(n);
    mx = dp[n];

    int l = mxA, r = n - 1, res = n;
    while(l <= r){
        int mid = (l + r) >> 1;
        if (check(mid)){
            res = mid;
            r = mid - 1;
        }
        else{
            l = mid + 1;
        }
    }
    check(res);
    cout << mx << endl;
    traced(n);

}
/**
Warning:
Đọc sai đề???
Cận lmao
Code imple thiếu case nào không.
Limit.
**/

Compilation message

tea.cpp: In function 'void fastscan(int&)':
tea.cpp:47:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |     register int c;
      |                  ^
tea.cpp: At global scope:
tea.cpp:126:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  126 | main()
      | ^~~~
tea.cpp: In function 'int main()':
tea.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tea.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 66384 KB Output is correct
2 Correct 17 ms 66384 KB Output is correct
3 Correct 13 ms 66384 KB Output is correct
4 Correct 13 ms 66384 KB Output is correct
5 Correct 12 ms 66384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 66384 KB Output is correct
2 Correct 12 ms 66384 KB Output is correct
3 Correct 13 ms 66384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 66384 KB Output is correct
2 Correct 13 ms 66436 KB Output is correct
3 Correct 13 ms 66384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 66384 KB Output is correct
2 Correct 17 ms 66640 KB Output is correct
3 Correct 15 ms 66640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 66384 KB Output is correct
2 Correct 15 ms 66584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 69192 KB Output is correct
2 Correct 47 ms 69632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 69788 KB Output is correct
2 Correct 44 ms 69448 KB Output is correct
3 Correct 48 ms 69724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 368 ms 107296 KB Output is correct
2 Correct 389 ms 103252 KB Output is correct
3 Correct 332 ms 102216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 415 ms 115668 KB Output is correct
2 Correct 550 ms 125880 KB Output is correct
3 Correct 451 ms 117288 KB Output is correct
4 Correct 486 ms 112544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 105136 KB Output is correct
2 Correct 144 ms 85688 KB Output is correct
3 Correct 466 ms 117484 KB Output is correct
4 Correct 564 ms 117596 KB Output is correct