Submission #68602610


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
 
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

#define int long long
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define eb emplace_back
#define f first
#define s second
#define lep(i,a,b) for (int i = (a); i <= (b); i++)
#define rep(i,a,b) for (int i = (a); i >= (b); i--)
const int inf = 1e18;
int t;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    int N = n * k;
    vector<int> a(N+1);
    lep(i,1,N) {
        cin >> a[i];
    }
    vector<vector<int>> cycles;
    vector<bool> visited(N+1);
    lep(i,1,N) {
        if (!visited[i]) {
            vector<int> cur_cycle;
            int cur = i;
            while (!visited[cur]) {
                cur_cycle.pb(cur);
                visited[cur] = true;
                cur = a[cur];
            }
            DEBUG(cur_cycle);
            cycles.pb(cur_cycle);
        }
    }
    DEBUG(cycles);

    auto solve = [&](vector<int> cycle) -> int {
        vector<int> pos(N+1,-1);
        int sz = cycle.size();
        lep(i,0,sz-1) {
            pos[cycle[i]] = i;
        }
        vector<vector<int>> dp(sz, vector<int>(sz));
        rep(i,sz-1,0) {
            lep(j,i,sz-1) {
                if (j == i) {
                    dp[i][j] = 0;
                } else {
                    int dp_val = 0;
                    int md = cycle[i] % n;
                    for (int k = md; k <= N; k += n) {
                        if (k == cycle[i]) continue;
                        if (pos[k] >= i and pos[k] <= j) {
                            DEBUG(i,a[i],k,pos[k]);
                            int val = 1;
                            if (pos[k] >= i + 2) {
                                val += dp[i+1][pos[k]-1];
                            }
                            val += dp[pos[k]][j];
                            dp_val = max(dp_val, val);
                        }
                    }
                    dp_val = max(dp_val, dp[i+1][j]);
                    dp_val = max(dp_val, dp[i][j-1]);
                    DEBUG(i,j,dp_val);
                    dp[i][j] = dp_val;
                }
            }
        }
        DEBUG(dp[0][sz-1]);
        return dp[0][sz-1];
    };
    int ans = 0;
    for (auto &x: cycles) {
        ans += solve(x);
    }
    cout << ans << "\n";
}

Submission Info

Submission Time
Task B - Sort Permutation
User arvindr9
Language C++ 20 (gcc 12.2)
Score 800
Code Size 3514 Byte
Status AC
Exec Time 279 ms
Memory 198728 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define DEBUG(...) 6
      |                    ^
Main.cpp:52:13: note: in expansion of macro ‘DEBUG’
   52 |             DEBUG(cur_cycle);
      |             ^~~~~
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define DEBUG(...) 6
      |                    ^
Main.cpp:56:5: note: in expansion of macro ‘DEBUG’
   56 |     DEBUG(cycles);
      |     ^~~~~
Main.cpp: In lambda function:
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define DEBUG(...) 6
      |                    ^
Main.cpp:75:29: note: in expansion of macro ‘DEBUG’
   75 |                             DEBUG(i,a[i],k,pos[k]);
      |                             ^~~~~
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define DEBUG(...) 6
      |                    ^
Main.cpp:86:21: note: in expansion of macro ‘DEBUG’
   86 |                     DEBUG(i,j,dp_val);
      |                     ^~~~~
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define DEBUG(...) 6
      |                    ^
Main.cpp:91:9: note: in expansion of macro ‘DEBUG’
   91 |         DEBUG(dp[0][sz-1]);
      |         ^~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-small-01.txt, 02-small-02.txt, 02-small-03.txt, 02-small-04.txt, 02-small-05.txt, 02-small-06.txt, 02-small-07.txt, 02-small-08.txt, 02-small-09.txt, 02-small-10.txt, 02-small-11.txt, 02-small-12.txt, 02-small-13.txt, 02-small-14.txt, 02-small-15.txt, 02-small-16.txt, 03-random-01.txt, 03-random-02.txt, 03-random-03.txt, 03-random-04.txt, 03-random-05.txt, 04-large-01.txt, 04-large-02.txt, 04-large-03.txt, 04-large-04.txt, 04-large-05.txt, 05-id-01.txt, 05-id-02.txt, 05-id-03.txt, 05-id-04.txt, 06-large-cycle-01.txt, 06-large-cycle-02.txt, 06-large-cycle-03.txt, 06-large-cycle-04.txt, 06-large-cycle-05.txt, 07-large-cycle-2-01.txt, 07-large-cycle-2-02.txt, 07-large-cycle-2-03.txt, 07-large-cycle-2-04.txt, 07-large-cycle-2-05.txt, 08-divide-01.txt, 08-divide-02.txt, 08-divide-03.txt, 08-divide-04.txt, 08-divide-05.txt, 09-large-ans-01.txt, 09-large-ans-02.txt, 09-large-ans-03.txt, 09-large-ans-04.txt, 09-large-ans-05.txt
Case Name Status Exec Time Memory
01-sample-01.txt AC 1 ms 3464 KiB
01-sample-02.txt AC 1 ms 3408 KiB
01-sample-03.txt AC 1 ms 3416 KiB
02-small-01.txt AC 1 ms 3408 KiB
02-small-02.txt AC 1 ms 3548 KiB
02-small-03.txt AC 1 ms 3552 KiB
02-small-04.txt AC 1 ms 3560 KiB
02-small-05.txt AC 1 ms 3352 KiB
02-small-06.txt AC 1 ms 3504 KiB
02-small-07.txt AC 1 ms 3556 KiB
02-small-08.txt AC 1 ms 3560 KiB
02-small-09.txt AC 1 ms 3556 KiB
02-small-10.txt AC 1 ms 3424 KiB
02-small-11.txt AC 1 ms 3552 KiB
02-small-12.txt AC 1 ms 3420 KiB
02-small-13.txt AC 1 ms 3552 KiB
02-small-14.txt AC 1 ms 3552 KiB
02-small-15.txt AC 1 ms 3488 KiB
02-small-16.txt AC 1 ms 3420 KiB
03-random-01.txt AC 24 ms 28348 KiB
03-random-02.txt AC 47 ms 39232 KiB
03-random-03.txt AC 1 ms 3552 KiB
03-random-04.txt AC 2 ms 4524 KiB
03-random-05.txt AC 19 ms 13396 KiB
04-large-01.txt AC 114 ms 65048 KiB
04-large-02.txt AC 37 ms 25304 KiB
04-large-03.txt AC 78 ms 38644 KiB
04-large-04.txt AC 74 ms 69636 KiB
04-large-05.txt AC 84 ms 38112 KiB
05-id-01.txt AC 3 ms 3768 KiB
05-id-02.txt AC 3 ms 3616 KiB
05-id-03.txt AC 3 ms 3672 KiB
05-id-04.txt AC 3 ms 3884 KiB
06-large-cycle-01.txt AC 279 ms 198728 KiB
06-large-cycle-02.txt AC 208 ms 161856 KiB
06-large-cycle-03.txt AC 257 ms 184116 KiB
06-large-cycle-04.txt AC 106 ms 97856 KiB
06-large-cycle-05.txt AC 272 ms 193496 KiB
07-large-cycle-2-01.txt AC 79 ms 39044 KiB
07-large-cycle-2-02.txt AC 167 ms 132960 KiB
07-large-cycle-2-03.txt AC 233 ms 168756 KiB
07-large-cycle-2-04.txt AC 37 ms 26488 KiB
07-large-cycle-2-05.txt AC 194 ms 140752 KiB
08-divide-01.txt AC 2 ms 3676 KiB
08-divide-02.txt AC 7 ms 4268 KiB
08-divide-03.txt AC 2 ms 3616 KiB
08-divide-04.txt AC 3 ms 3816 KiB
08-divide-05.txt AC 2 ms 3688 KiB
09-large-ans-01.txt AC 128 ms 81876 KiB
09-large-ans-02.txt AC 53 ms 18916 KiB
09-large-ans-03.txt AC 157 ms 111772 KiB
09-large-ans-04.txt AC 74 ms 48228 KiB
09-large-ans-05.txt AC 46 ms 25548 KiB