답안 #967248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967248 2024-04-21T15:41:19 Z Requiem Vrtić (COCI18_vrtic) C++17
144 / 160
2000 ms 189740 KB
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 50000007
#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 "vrtic"
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 = 153;
int n;
int best_friend[MAXN], bag_of_candy[MAXN];

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct chash {
    // any random-ish large odd number will do
    const uint64_t C = uint64_t(2e18 * pi) + 71;
    // random 32-bit number
    const uint32_t RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    size_t operator()(uint64_t x) const {
        // see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
        return __builtin_bswap64((x ^ RANDOM) * C);
    }
};
template <class K, class V> using cmap = unordered_map<K, V, chash>;
namespace subtask1{
    bool checkSubtask1(){
         for(int i = 1; i <= n; i++){
             if (best_friend[i] != i % n + 1) return false;
         }
         return true;
    }

    int arr[MAXN];

    void solveSubtask1(){
        int res = 0, splitting_point = 0;
        sort(bag_of_candy + 1, bag_of_candy + 1 + n);
        int cur = 0;

        for(int i = 3; i <= n; i++){
            maximize(res, abs(bag_of_candy[i] - bag_of_candy[i - 2]));
        }

        cout << res << endl;
        for(int i = 1; i <= n; i += 2){
            cout << bag_of_candy[i] << ' ';
        }
        for(int i = n - (n & 1); i > 0; i -= 2){
            cout << bag_of_candy[i] << ' ';
        }
        cout << endl;
    }

}
namespace subtask2{ //not usable yet
    bool checkSubtask2(){
        return n <= 20;
    }

    int MinDistriRange[MAXN][MAXN], ok[MAXN], answer[MAXN];
    vector<vector<int>> Listcycle;
    vector<ii> Nxtmask;
    int dp[21][(1 << 20)], trace[21][(1 << 20)];
    void prep(){
        sort(bag_of_candy + 1, bag_of_candy + 1 + n);
        for(int i = 1; i <= n; i++){
            for(int j = i; j <= n; j++){
                int length = j - i + 1;
                if (length == 1) MinDistriRange[i][j] = 0;
                else if (length == 2) MinDistriRange[i][j] = abs(bag_of_candy[j] - bag_of_candy[i]);
                else {
                     for(int k = i + 2; k <= j; k++){
                         maximize(MinDistriRange[i][j], abs(bag_of_candy[k] - bag_of_candy[k - 2]) );
                     }
                }
            }
        }
    }

    void AssignPermutation(int pos, int msk){
         if (pos == 0) return;
         int taken = trace[pos][msk];

         int numbit = __builtin_popcount(taken);
         vector<int> arr;
         vector<int> ans;
         for(int i = 0; i < n; i++){
             if (taken & (1 << i)) arr.pb(bag_of_candy[i + 1]);
         }

         for(int i = 0; i < numbit; i += 2){
             ans.pb(arr[i]);
         }
         for(int i = numbit - 1 - !((numbit - 1)&1); i >= 0; i -= 2){
             ans.pb(arr[i]);
         }

         for(int i = 0; i < Listcycle[pos - 1].size(); i++){
             answer[Listcycle[pos - 1][i]] = ans[i];
         }

         AssignPermutation(pos - 1, msk ^ taken);
    }
    void solveSubtask2(){
        prep();
        memset(ok, 0, sizeof(ok));
        int numcycle = 0;
        for(int i = 1; i <= n; i++){
            if (ok[i]) continue;
            vector<int> oneCycle;
            numcycle++;
            for(int j = i; !ok[j] ; j = best_friend[j]){
                oneCycle.pb(j);
                ok[j] = 1;
            }
            Listcycle.pb(oneCycle);
        }
        memset(dp, 0x3f, sizeof(dp));
        sort(Listcycle.begin(), Listcycle.end(), [&](const vector<int> &a, const vector<int> &b){
             return a.size() > b.size();
        });
        queue<array<int, 3>> q;
        q.push({0, 0, 0});
        dp[0][0] = 0;
        int sz = 0;
        while(!q.empty()){
            int du = q.front()[0];
            int pos = q.front()[1];
            int msk = q.front()[2];
            int testmsk = 0;
            q.pop();
            if (du > dp[pos][msk] or pos == (int) Listcycle.size()) continue;

            int sz = Listcycle[pos].size();
            for(int i = 0; i < sz; i++){
                testmsk <<= 1;
                testmsk++;
            }

//            cout << sz << ' ' << testmsk << endl;
            for(int j = 0; j < n + 1 - sz; j++){
                if (((testmsk << j) & msk) == 0) {
                    if (minimize(dp[pos + 1][msk ^ (testmsk << j)], max(du, MinDistriRange[j + 1][j + sz]))){
                        q.push({dp[pos + 1][msk ^ (testmsk << j)], pos + 1, msk ^ (testmsk << j) });
                        trace[pos + 1][msk ^ (testmsk << j)] = (testmsk << j);
                    }
                }
            }
        }
        cout << dp[Listcycle.size()][(1 << n) - 1] << endl;
        AssignPermutation(Listcycle.size(), (1 << n) - 1);
        for(int i = 1; i <= n; i++){
            cout << answer[i] << ' ';
        }
        cout << endl;
    }
}
namespace subtask3{
    int MinDistriRange[MAXN][MAXN], ok[MAXN], answer[MAXN], ptr[MAXN];
    vector<vector<int>> Listcycle;
    vector<vector<int>> cycle[MAXN];
    vector<int> range[MAXN][MAXN];
    void prep(){
        sort(bag_of_candy + 1, bag_of_candy + 1 + n);
        for(int i = 1; i <= n; i++){
            for(int j = i; j <= n; j++){
                int length = j - i + 1;
                if (length == 1) MinDistriRange[i][j] = 0;
                else if (length == 2) MinDistriRange[i][j] = abs(bag_of_candy[j] - bag_of_candy[i]);
                else {
                     for(int k = i + 2; k <= j; k++){
                         maximize(MinDistriRange[i][j], abs(bag_of_candy[k] - bag_of_candy[k - 2]) );
                     }
                }

                vector<int> arr;
                vector<int> ans;
                for(int k = i; k <= j; k++){
                    arr.pb(bag_of_candy[k]);
                }

                for(int i = 0; i < length; i += 2){
                    ans.pb(arr[i]);
                }
                for(int i = length - 1 - !((length - 1) & 1); i >= 0; i -= 2){
                    ans.pb(arr[i]);
                }
                range[i][j] = ans;
            }
        }
    }

    gp_hash_table<ll, ll, chash> dp;
    vector<ii> FreqCycle;
    const int base = 113;
    int POW[MAXN], originalHash = 0;

    void calcPow(){
         POW[0] = 1;
         for(int i = 1; i <= 16; i++){
             POW[i] = (1LL * POW[i - 1] * base) % MOD;
         }
    }
    void extractCycle(){
         memset(ok, 0, sizeof(ok));
         map<int, int> tmp;
         for(int i = 1; i <= n; i++){
             if(ok[i]) continue;
             vector<int> onecycle;
             for(int j = i; !ok[j]; j = best_friend[j]){
                 onecycle.pb(j);
                 ok[j] = true;
             }

             cycle[(int)onecycle.size()].pb(onecycle);
             tmp[onecycle.size()]++;
         }

         for(auto x: tmp) {
             FreqCycle.pb({x.fi, x.se});
         }

         reverse(FreqCycle.begin(), FreqCycle.end());
         for(int i = 0; i < FreqCycle.size(); i++){
//             cout << FreqCycle[i].fi << ' ' << FreqCycle[i].se << endl;
             originalHash = 1LL * (1LL * originalHash + 1LL * POW[i] * FreqCycle[i].se) % MOD;
         }
    }

    vector<int> ord;
    vector<ii> TempCycle;

    bool dfs(int p, int state, int lim, bool traceback){
        if (dp.find(state) != dp.end()) return false;
        dp[state] = 1;
//        cout << p << ' ' << ' ' << state << ' ' << lim << ' ' << traceback << endl;
        if (p == n) {
            return true;
        }
        for(int i = 0; i < TempCycle.size(); i++){
            ii &chosenCycle = TempCycle[i];
            if (chosenCycle.se == 0 or MinDistriRange[p + 1][p + chosenCycle.fi] > lim) continue;
            int nxtHash = ((1LL * state - 1LL * POW[i]) % MOD + MOD) % MOD;
            chosenCycle.se--;
            if (traceback) ord.pb(chosenCycle.fi);
//            cout << p << ' ' << chosenCycle.fi << ' ' << ord.back() << endl;
            if (dfs(p + chosenCycle.fi, nxtHash, lim, traceback)) return true;
            chosenCycle.se++;
            if (traceback) ord.pop_back();
        }
        return false;
    }

    bool check(int mid, int traceback){
        ord.clear();
        dp.clear();
        TempCycle = FreqCycle;
        return dfs(0, originalHash, mid, traceback);
    }
    void solveSubtask3(){
        calcPow();
        prep();
        extractCycle();
//        cout << originalHash << endl;
        int l = 0, r = 1e9, res = 0;
        while(l <= r){
            int mid = (l + r) >> 1;
            if (check(mid, 0)){
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        int start = 0;
        check(res, 1);
        for(int i = 0; i < ord.size(); i++){
            int x = ord[i];
//            cout << start + 1 << ' ' << start + x << endl;
//            for(auto x: range[start + 1][start + x]){
//                cout << x << ' ';
//            }
//            cout << endl;
            for(int j = 0; j < cycle[x][ptr[x]].size(); j++){
                answer[cycle[x][ptr[x]][j]] = range[start + 1][start + x][j];
            }
            ptr[x]++;
            start += x;
        }
        cout << res << endl;
        for(int i = 1; i <= n; i++){
            cout << answer[i] << ' ';
        }
        cout << endl;
    }


}
main()
{
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> best_friend[i];
    }

    for(int i = 1; i <= n; i++){
        cin >> bag_of_candy[i];
    }

//    if (subtask1::checkSubtask1()) return subtask1::solveSubtask1(), 0;
//    if (subtask2::checkSubtask2()) return subtask2::solveSubtask2(), 0;
    return subtask3::solveSubtask3(), 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

vrtic.cpp: In function 'void subtask1::solveSubtask1()':
vrtic.cpp:51:22: warning: unused variable 'splitting_point' [-Wunused-variable]
   51 |         int res = 0, splitting_point = 0;
      |                      ^~~~~~~~~~~~~~~
vrtic.cpp:53:13: warning: unused variable 'cur' [-Wunused-variable]
   53 |         int cur = 0;
      |             ^~~
vrtic.cpp: In function 'void subtask2::AssignPermutation(int, int)':
vrtic.cpp:113:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |          for(int i = 0; i < Listcycle[pos - 1].size(); i++){
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp: In function 'void subtask2::solveSubtask2()':
vrtic.cpp:140:13: warning: unused variable 'sz' [-Wunused-variable]
  140 |         int sz = 0;
      |             ^~
vrtic.cpp: In function 'void subtask3::extractCycle()':
vrtic.cpp:239:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  239 |          for(int i = 0; i < FreqCycle.size(); i++){
      |                         ~~^~~~~~~~~~~~~~~~~~
vrtic.cpp: In function 'bool subtask3::dfs(int, int, int, bool)':
vrtic.cpp:255:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  255 |         for(int i = 0; i < TempCycle.size(); i++){
      |                        ~~^~~~~~~~~~~~~~~~~~
vrtic.cpp: In function 'void subtask3::solveSubtask3()':
vrtic.cpp:291:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  291 |         for(int i = 0; i < ord.size(); i++){
      |                        ~~^~~~~~~~~~~~
vrtic.cpp:298:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  298 |             for(int j = 0; j < cycle[x][ptr[x]].size(); j++){
      |                            ~~^~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp: At global scope:
vrtic.cpp:313:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  313 | main()
      | ^~~~
vrtic.cpp: In function 'int main()':
vrtic.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".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
vrtic.cpp:318:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  318 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3780 KB Output is correct
2 Correct 43 ms 9412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 6980 KB Output is correct
2 Correct 135 ms 28768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 5596 KB Output is correct
2 Correct 439 ms 53004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 7888 KB Output is correct
2 Correct 894 ms 100676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8152 KB Output is correct
2 Execution timed out 2073 ms 189740 KB Time limit exceeded
3 Halted 0 ms 0 KB -