Submission #1146292

#TimeUsernameProblemLanguageResultExecution timeMemory
1146292mychecksedadCookies (JOI23_cookies)C++20
Compilation error
0 ms0 KiB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 500+100, M = 1e5+10, K = 52, MX = 30;

struct E{
  bool ok;
  int go;
};

int n, b[N], m, pref[N], PD[N][N][N];
E DP[N][N];
array<int, 2> a[N];
pair<bool, vector<vi>> f(vector<pii> A){
  vector<vi> res(A.size());
  for(int i = 1; i <= n; ++i){
    if(A.size() < a[i][0]){
      return pair<bool, vector<vi>>{false, res};
    }
    for(int j = 0; j < a[i][0]; ++j){
      if(A[j].ff == 0){
        return pair<bool, vector<vi>>{false, res};
      }
      A[j].ff--;
      res[A[j].ss].pb(a[i][1]);
    }    
    sort(all(A));
    reverse(all(A));
  }
  return pair<bool, vector<vi>>{true, res};
}
void solve(){
  cin >> n;
  int s = 0;
  for(int i = 1; i <= n; ++i){
    cin >> a[i][0];
    a[i][1] = i;
    s += a[i][0];
  }
  sort(a+1, a+1+n, greater<array<int, 2>>());
  pref[0] = 0;
  for(int i = 1; i <= n; ++i){
    pref[i] = pref[i - 1] + a[i][0];
  }
  cin >> m;
  vector<bool> is_size(s + 1);
  for(int i = 1; i <= m; ++i){
    cin >> b[i];
    is_size[b[i]] = 1;
  }
  sort(b+1, b+1+m);
  for(int i = 0; i <= s + 1; ++i){
    for(int j = 0; j <= s + 1; ++j) DP[i][j] = {0, 0};
  }
  b[0] = 0;
  is_size[0] = 1;
  DP[m + 1][0] = {true, 0};
  // for(int i = 1; i <= n; ++i){
  //   cout << pref[i] << ' ';
  // }
  // en;
  for(int i = m; i >= 1; --i){ // C_{n-i+1}
    int SZ = b[i], LEN = b[i] - b[i - 1];
    for(int cur_sum = s; cur_sum >= 0; --cur_sum){
      if(!DP[i + 1][cur_sum].ok || s - cur_sum < pref[b[i]]) continue;
      for(int x = DP[i + 1][cur_sum].go; x <= s; ++x){
        int next_sum = cur_sum + x * LEN;
        // cout << cur_sum << ' ' << prev_sum << '\n';
        bool good = 1;
        int k = s - next_sum;
        for(int j = b[i-1]; j <= b[i]; ++j){
          if(k < pref[j]) good = 0;
          k += x;
        }
        if(good && DP[i][next_sum].ok == false && DP[i + 1][cur_sum].ok){
          DP[i][next_sum] = {true, x};
          // break;
        }
      }
    }
  }
  if(DP[1][s].ok){
    vector<pii> boxes;
    int cur_num = DP[1][s].go, cur_sum = s;
    for(int i = 1; i <= m; ++i){
      if(cur_sum == 0) break;
      int LEN = b[i] - b[i - 1];
      auto dp = DP[i + 1][cur_sum - cur_num * LEN];
      if(cur_num > dp.go){
        for(int l = dp.go; l < cur_num; ++l) boxes.pb({b[i], boxes.size()});
      }
      cur_sum -= cur_num * LEN;
      cur_num = dp.go;
    }
    assert(cur_sum == 0);
    sort(all(boxes), greater<pii>());
    auto g = f(boxes);
    assert(g.ff);
    cout << g.ss.size() << '\n';
    for(auto v: g.ss){
      cout << v.size() << ' ';
      for(int u: v) cout << u << ' ';
      en;
    }
    return;
  } 
  cout << -1;
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  while(tt--){
    solve();
    en;
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
} /* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 500+100, M = 1e5+10, K = 52, MX = 30;

struct E{
  bool ok;
  int go;
};

int n, b[N], m, pref[N], PD[N][N][N];
E DP[N][N];
array<int, 2> a[N];
pair<bool, vector<vi>> f(vector<pii> A){
  vector<vi> res(A.size());
  for(int i = 1; i <= n; ++i){
    if(A.size() < a[i][0]){
      return pair<bool, vector<vi>>{false, res};
    }
    for(int j = 0; j < a[i][0]; ++j){
      if(A[j].ff == 0){
        return pair<bool, vector<vi>>{false, res};
      }
      A[j].ff--;
      res[A[j].ss].pb(a[i][1]);
    }    
    sort(all(A));
    reverse(all(A));
  }
  return pair<bool, vector<vi>>{true, res};
}
void solve(){
  cin >> n;
  int s = 0;
  for(int i = 1; i <= n; ++i){
    cin >> a[i][0];
    a[i][1] = i;
    s += a[i][0];
  }
  sort(a+1, a+1+n, greater<array<int, 2>>());
  pref[0] = 0;
  for(int i = 1; i <= n; ++i){
    pref[i] = pref[i - 1] + a[i][0];
  }
  cin >> m;
  vector<bool> is_size(s + 1);
  for(int i = 1; i <= m; ++i){
    cin >> b[i];
    is_size[b[i]] = 1;
  }
  sort(b+1, b+1+m);
  for(int i = 0; i <= s + 1; ++i){
    for(int j = 0; j <= s + 1; ++j) DP[i][j] = {0, 0};
  }
  b[0] = 0;
  is_size[0] = 1;
  DP[m + 1][0] = {true, 0};
  // for(int i = 1; i <= n; ++i){
  //   cout << pref[i] << ' ';
  // }
  // en;
  for(int i = m; i >= 1; --i){ // C_{n-i+1}
    int SZ = b[i], LEN = b[i] - b[i - 1];
    for(int cur_sum = s; cur_sum >= 0; --cur_sum){
      if(!DP[i + 1][cur_sum].ok || s - cur_sum < pref[b[i]]) continue;
      for(int x = DP[i + 1][cur_sum].go; x <= s; ++x){
        int next_sum = cur_sum + x * LEN;
        // cout << cur_sum << ' ' << prev_sum << '\n';
        bool good = 1;
        int k = s - next_sum;
        for(int j = b[i-1]; j <= b[i]; ++j){
          if(k < pref[j]) good = 0;
          k += x;
        }
        if(good && DP[i][next_sum].ok == false && DP[i + 1][cur_sum].ok){
          DP[i][next_sum] = {true, x};
          // break;
        }
      }
    }
  }
  if(DP[1][s].ok){
    vector<pii> boxes;
    int cur_num = DP[1][s].go, cur_sum = s;
    for(int i = 1; i <= m; ++i){
      if(cur_sum == 0) break;
      int LEN = b[i] - b[i - 1];
      auto dp = DP[i + 1][cur_sum - cur_num * LEN];
      if(cur_num > dp.go){
        for(int l = dp.go; l < cur_num; ++l) boxes.pb({b[i], boxes.size()});
      }
      cur_sum -= cur_num * LEN;
      cur_num = dp.go;
    }
    assert(cur_sum == 0);
    sort(all(boxes), greater<pii>());
    auto g = f(boxes);
    assert(g.ff);
    cout << g.ss.size() << '\n';
    for(auto v: g.ss){
      cout << v.size() << ' ';
      for(int u: v) cout << u << ' ';
      en;
    }
    return;
  } 
  cout << -1;
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  while(tt--){
    solve();
    en;
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
} 

Compilation message (stderr)

cookies.cpp:145:11: error: redefinition of 'const int N'
  145 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |           ^
cookies.cpp:14:11: note: 'const int N' previously defined here
   14 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |           ^
cookies.cpp:145:24: error: redefinition of 'const int M'
  145 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                        ^
cookies.cpp:14:24: note: 'const int M' previously defined here
   14 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                        ^
cookies.cpp:145:36: error: redefinition of 'const int K'
  145 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                                    ^
cookies.cpp:14:36: note: 'const int K' previously defined here
   14 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                                    ^
cookies.cpp:145:44: error: redefinition of 'const int MX'
  145 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                                            ^~
cookies.cpp:14:44: note: 'const int MX' previously defined here
   14 | const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
      |                                            ^~
cookies.cpp:147:8: error: redefinition of 'struct E'
  147 | struct E{
      |        ^
cookies.cpp:16:8: note: previous definition of 'struct E'
   16 | struct E{
      |        ^
cookies.cpp:152:5: error: redefinition of 'int n'
  152 | int n, b[N], m, pref[N], PD[N][N][N];
      |     ^
cookies.cpp:21:5: note: 'int n' previously declared here
   21 | int n, b[N], m, pref[N], PD[N][N][N];
      |     ^
cookies.cpp:152:8: error: redefinition of 'int b [600]'
  152 | int n, b[N], m, pref[N], PD[N][N][N];
      |        ^
cookies.cpp:21:8: note: 'int b [600]' previously declared here
   21 | int n, b[N], m, pref[N], PD[N][N][N];
      |        ^
cookies.cpp:152:14: error: redefinition of 'int m'
  152 | int n, b[N], m, pref[N], PD[N][N][N];
      |              ^
cookies.cpp:21:14: note: 'int m' previously declared here
   21 | int n, b[N], m, pref[N], PD[N][N][N];
      |              ^
cookies.cpp:152:17: error: redefinition of 'int pref [600]'
  152 | int n, b[N], m, pref[N], PD[N][N][N];
      |                 ^~~~
cookies.cpp:21:17: note: 'int pref [600]' previously declared here
   21 | int n, b[N], m, pref[N], PD[N][N][N];
      |                 ^~~~
cookies.cpp:152:26: error: redefinition of 'int PD [600][600][600]'
  152 | int n, b[N], m, pref[N], PD[N][N][N];
      |                          ^~
cookies.cpp:21:26: note: 'int PD [600][600][600]' previously declared here
   21 | int n, b[N], m, pref[N], PD[N][N][N];
      |                          ^~
cookies.cpp:153:3: error: redefinition of 'E DP [600][600]'
  153 | E DP[N][N];
      |   ^~
cookies.cpp:22:3: note: 'E DP [600][600]' previously declared here
   22 | E DP[N][N];
      |   ^~
cookies.cpp:154:15: error: redefinition of 'std::array<int, 2> a [600]'
  154 | array<int, 2> a[N];
      |               ^
cookies.cpp:23:15: note: 'std::array<int, 2> a [600]' previously declared here
   23 | array<int, 2> a[N];
      |               ^
cookies.cpp:155:24: error: redefinition of 'std::pair<bool, std::vector<std::vector<int> > > f(std::vector<std::pair<int, int> >)'
  155 | pair<bool, vector<vi>> f(vector<pii> A){
      |                        ^
cookies.cpp:24:24: note: 'std::pair<bool, std::vector<std::vector<int> > > f(std::vector<std::pair<int, int> >)' previously defined here
   24 | pair<bool, vector<vi>> f(vector<pii> A){
      |                        ^
cookies.cpp:173:6: error: redefinition of 'void solve()'
  173 | void solve(){
      |      ^~~~~
cookies.cpp:42:6: note: 'void solve()' previously defined here
   42 | void solve(){
      |      ^~~~~
cookies.cpp:252:5: error: redefinition of 'int main()'
  252 | int main(){
      |     ^~~~
cookies.cpp:121:5: note: 'int main()' previously defined here
  121 | int main(){
      |     ^~~~