Submission #599693

# Submission time Handle Problem Language Result Execution time Memory
599693 2022-07-19T18:57:47 Z Mounir Boarding Passes (BOI22_passes) C++14
25 / 100
2000 ms 3904 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define chmax(x, v) x = max(x, v)
#define chmin(x, v) x = min(x, v)
#define pb push_back
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define x first
#define y second
#define int long long
using namespace std;

const int N = 1e5 + 5, G = 16, B = (1 << G);
const int OO = 1e18;
int nAvant[N][G], nApres[N][G];
vector<int> occs[G];
int dp[B];

int nInversions(int n){
      return (n * n - n);
}

signed main(){ 
      string line; cin >> line;
      int nVals = line.length();
      vector<int> vals;
      for (int i = 0; i < nVals; ++i)
            vals.pb(line[i] - 'A');
      
      for (int c = 0; c < G; ++c)
            nAvant[0][c] = 0;
      nAvant[0][vals[0]]++;
      for (int iVal = 1; iVal < nVals; ++iVal){
            for (int val = 0; val < G; ++val)
                  nAvant[iVal][val] = nAvant[iVal - 1][val];
            nAvant[iVal][vals[iVal]]++;
      }

      for (int iVal = 0; iVal < nVals; ++iVal)
            for (int val = 0; val < G; ++val)
                  nApres[iVal][val] = nAvant[nVals - 1][val] - nAvant[iVal][val];
      /*
      cout << "wsh" << endl;
      for (int i = 0; i < nVals; ++i)
            cout << nAvant[i][2] << " ";
      cout << endl;
      for (int i = 0; i < nVals; ++i)
            cout << nApres[i][2] << " ";
      cout << endl;
      cout << "-----" << endl;
*/
      for (int iVal = 0; iVal < nVals; ++iVal)
            occs[vals[iVal]].pb(iVal);
      for (int mask = 0; mask < B; ++mask)
            dp[mask] = OO;
      dp[0] = 0;

      for (int mask = 0; mask < B; ++mask){
            vector<int> presents;
            for (int i = 0; i < G; ++i){
                  if ((mask&(1 << i)) > 0)
                        presents.pb(i);
            }
         //   cout << dp[mask] << endl;

            for (int c = 0; c < G; ++c){
                  if ((mask&(1 << c)) == 0){
                        int nCur = sz(occs[c]);
                        if (nCur == 0){
                              chmin(dp[mask|(1 << c)], dp[mask]);
                              continue;
                        }
                        vector<int> before, after;
                        for (int iVal : occs[c]){
                              int sum = 0;
                              for (int present : presents)
                                    sum += nAvant[iVal][present];
                              before.pb(sum);
                              sum = 0;
                              for (int present : presents)
                                    sum += nApres[iVal][present];
                              after.pb(sum);
                        }

                        vector<int> prefix(nCur, 0), suffix(nCur, 0);
                        prefix[0] = before[0];
                        for (int i = 1; i < nCur; ++i)
                              prefix[i] = prefix[i - 1] + before[i];
                        suffix[nCur - 1] = after[nCur - 1];
                        for (int i = nCur - 2; i >= 0; --i)
                              suffix[i] = suffix[i + 1] + after[i];

                        int best = OO;
                        for (int coupe = -1; coupe < nCur; ++coupe){
                              int actu = 0;
                              if (coupe >= 0)
                                    actu += 4 * prefix[coupe];
                              if (coupe + 1 < nCur)
                                    actu += 4 * suffix[coupe + 1];
                              actu += nInversions(coupe + 1);
                              actu += nInversions(nCur - coupe - 1);
                              chmin(best, actu);
                        }


                        if (false && mask == 4){
                              cout << "trans " << c << " " << best << endl;
                              for (int iVal : occs[c])
                                    cout << iVal << " ";
                              cout << endl;
                              for (int i : before)
                                    cout << i << " ";
                              cout << endl;
                              for (int i : after)
                                    cout << i << " ";
                              cout << endl;
                              cout << "---" << endl;
                        }

                        chmin(dp[mask|(1 << c)], dp[mask] + best);
                  }
            }
      }
      cout << dp[B - 1]/4.0 << endl;
      return 0;   
}
# Verdict Execution time Memory Grader output
1 Incorrect 655 ms 1108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 724 KB Output is correct
2 Correct 108 ms 852 KB Output is correct
3 Correct 207 ms 852 KB Output is correct
4 Correct 182 ms 852 KB Output is correct
5 Correct 116 ms 852 KB Output is correct
6 Correct 48 ms 724 KB Output is correct
7 Correct 150 ms 852 KB Output is correct
8 Correct 86 ms 816 KB Output is correct
9 Correct 103 ms 816 KB Output is correct
10 Correct 85 ms 820 KB Output is correct
11 Correct 82 ms 724 KB Output is correct
12 Correct 85 ms 828 KB Output is correct
13 Correct 90 ms 724 KB Output is correct
14 Correct 86 ms 744 KB Output is correct
15 Correct 87 ms 824 KB Output is correct
16 Correct 93 ms 724 KB Output is correct
17 Correct 84 ms 824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 724 KB Output is correct
2 Correct 108 ms 852 KB Output is correct
3 Correct 207 ms 852 KB Output is correct
4 Correct 182 ms 852 KB Output is correct
5 Correct 116 ms 852 KB Output is correct
6 Correct 48 ms 724 KB Output is correct
7 Correct 150 ms 852 KB Output is correct
8 Correct 86 ms 816 KB Output is correct
9 Correct 103 ms 816 KB Output is correct
10 Correct 85 ms 820 KB Output is correct
11 Correct 82 ms 724 KB Output is correct
12 Correct 85 ms 828 KB Output is correct
13 Correct 90 ms 724 KB Output is correct
14 Correct 86 ms 744 KB Output is correct
15 Correct 87 ms 824 KB Output is correct
16 Correct 93 ms 724 KB Output is correct
17 Correct 84 ms 824 KB Output is correct
18 Correct 41 ms 812 KB Output is correct
19 Correct 107 ms 852 KB Output is correct
20 Correct 160 ms 852 KB Output is correct
21 Correct 157 ms 852 KB Output is correct
22 Correct 119 ms 860 KB Output is correct
23 Correct 60 ms 812 KB Output is correct
24 Correct 142 ms 852 KB Output is correct
25 Correct 83 ms 824 KB Output is correct
26 Correct 89 ms 820 KB Output is correct
27 Correct 95 ms 824 KB Output is correct
28 Correct 89 ms 724 KB Output is correct
29 Correct 88 ms 820 KB Output is correct
30 Correct 85 ms 824 KB Output is correct
31 Correct 94 ms 724 KB Output is correct
32 Correct 105 ms 724 KB Output is correct
33 Correct 103 ms 724 KB Output is correct
34 Correct 85 ms 724 KB Output is correct
35 Execution timed out 2096 ms 3904 KB Time limit exceeded
36 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 655 ms 1108 KB Output isn't correct
2 Halted 0 ms 0 KB -