Submission #1279006

#TimeUsernameProblemLanguageResultExecution timeMemory
1279006SmuggingSpunK-th path (IZhO11_kthpath)C++20
0 / 100
1 ms572 KiB
#include<bits/stdc++.h> #define taskname "D" using namespace std; typedef long long ll; const int lim = 31; int n, m; char a[lim][lim]; ll Ckn[lim << 1][lim << 1]; string ans = ""; struct Data{ int i, j; ll cnt; Data(){} Data(int i, int j, ll cnt){ this->i = i; this->j = j; this->cnt = cnt; } bool operator <(const Data& other){ return a[i][j] < a[other.i][other.j] || (a[i][j] == a[other.i][other.j] && make_pair(i, j) < make_pair(other.i, other.j)); } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> a[i][j]; } } if(n == 1){ for(int i = 1; i <= m; i++){ cout << a[1][i]; } return 0; } if(m == 1){ for(int i = 1; i <= n; i++){ cout << a[i][1]; } return 0; } memset(Ckn, 0, sizeof(Ckn)); for(int i = Ckn[0][0] = 1; i < (lim << 1); i++){ Ckn[0][i] = 1; for(int j = 1; j <= i; j++){ Ckn[j][i] = Ckn[j][i - 1] + Ckn[j - 1][i - 1]; } } ll k; cin >> k; ans += a[1][1]; vector<Data>p; p.push_back(Data(1, 2, 1LL)); p.push_back(Data(2, 1, 1LL)); for(int i = 1; i < n + m - 1; i++){ sort(p.begin(), p.end()); vector<Data>np; np.push_back(p[0]); for(int j = 1; j < p.size(); j++){ if(p[j].i != np.back().i || p[j].j != np.back().j){ np.push_back(p[j]); } else{ np.back().cnt += p[j].cnt; } } swap(p, np); char z; for(Data& it : p){ int N = n - it.i + 1, M = m - it.j + 1; ll C = it.cnt * Ckn[N - 1][N + M - 2]; if(C < k){ k -= C; } else{ ans += (z = a[it.i][it.j]); break; } } np.clear(); for(Data& it : p){ if(a[it.i][it.j] == z){ if(it.i < n){ np.push_back(Data(it.i + 1, it.j, it.cnt)); } if(it.j < m){ np.push_back(Data(it.i, it.j + 1, it.cnt)); } } } swap(p, np); } cout << ans; }

Compilation message (stderr)

kthpath.cpp: In function 'int main()':
kthpath.cpp:26:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...