Submission #523293

# Submission time Handle Problem Language Result Execution time Memory
523293 2022-02-07T10:44:44 Z cadmiumsky K-th path (IZhO11_kthpath) C++14
100 / 100
1 ms 372 KB
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define piii tuple<int,int,int>
#define x first
#define y second
using namespace std;

const int inf = 1e18 + 10;
const int nmax = 100;
int n, m;
char mat[nmax][nmax];
int comb[nmax][nmax];

static int mult(int a,int b) {  
  if(log(a) + log(b) > log(inf))
    return inf;
  return a * b;
} 
static int add(int a, int b) {
  if(a > inf - b)
    return inf;
  return a + b;
}

static vector<piii> move(vector<piii> initial) {
  vector<piii> sol;
  map<pii, int> ssol;
  auto insert = [&](int x, int y, int c) {
    if(x >= n || y >= m)
      return;
    ssol[{x, y}] += c;
  };
  for(auto [a, b, c] : initial)
    insert(a, b + 1, c), insert(a + 1, b, c);
  for(auto x : ssol) {
    int a, b;
    tie(a, b) = x.first;
    sol.push_back({a, b, x.second});
  }
  return sol;
}

static void solve(vector<piii> poz, string s, int k) {
  if(poz.size() == 0) {
    cout << s << '\n';
    exit(0);
  }
  sort(poz.begin(), poz.end(), [&](piii a_, piii b_) {
    int x, y, z;
    tie(x, y, z) = a_;
    pii a = {x, y};
    tie(x, y, z) = b_;
    pii b = {x, y};
    return mat[a.x][a.y] < mat[b.x][b.y];
  });
  //for(auto [a, b, c] : poz) {
    //cout << a << ' ' << b << ' ' << c << '\n';
  //}
  //cout << "-------\n";
  char last = 0;
  int sum = 0;
  vector<piii> curr;
  for(auto [a, b, c] : poz) {
    if(last != mat[a][b]) {
      if(sum >= k)
        solve(move(curr), s + last, k);
      k -= sum;
      last = mat[a][b]; 
      sum = 0;
      curr.resize(0);
    }
    sum = add(sum, mult(c, comb[n - a + m - b - 2][n - a - 1]));
    curr.push_back({a, b, c});
  }
  solve(move(curr), s + last, k);
}

signed main() {
  for(int i = 0; i < nmax; i++) {
    comb[i][0] = comb[i][i] = 1;
    for(int j = 1; j < i; j++)
      comb[i][j] = add(comb[i - 1][j], comb[i - 1][j - 1]);
  }
  cin >> n >> m;
  //cout << n << ' ' << m << '\n';
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
    //cout << i << ' ' << n << '\n';
  }
  int k;
  cin >> k;
  vector<piii> v;
  v.push_back({0, 0, 1});
  solve(v, "", k);
}

Compilation message

kthpath.cpp: In function 'std::vector<std::tuple<long long int, long long int, long long int> > move(std::vector<std::tuple<long long int, long long int, long long int> >)':
kthpath.cpp:34:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |   for(auto [a, b, c] : initial)
      |            ^
kthpath.cpp: In function 'void solve(std::vector<std::tuple<long long int, long long int, long long int> >, std::string, long long int)':
kthpath.cpp:64:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   64 |   for(auto [a, b, c] : poz) {
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 372 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 372 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 372 KB Output is correct
20 Correct 1 ms 332 KB Output is correct