답안 #488979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
488979 2021-11-21T01:12:45 Z SirCovidThe19th K번째 경로 (IZhO11_kthpath) C++17
100 / 100
1 ms 332 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second

int n, m; ll k, ways[35][35], choose[65][65]; pll range[35][35]; string A[35], ans; 

int main(){
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> A[i], A[i] = " " + A[i];
    cin >> k;

    for (int i = 0; i < 65 and (choose[i][0] = 1); i++)
        for (int j = 1; j <= i; j++)
            choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];

    ans = A[1][1]; ways[1][1] = 1; range[1][1] = {1, choose[n + m - 2][n - 1]}; 

    for (int len = 2; len <= n + m - 1; len++){
        vector<pii> test; ll rangeSt = 0, tot = 0;
        for (int i = min(len, n), j = max(len - n + 1, 1); i and j <= m; i--, j++){
            pll R = {};
            if (ways[i - 1][j]) R = range[i - 1][j];
            if (ways[i][j - 1]) R = range[i][j - 1];
            if (k >= R.f and k <= R.s){ //a potential spot
                test.push_back({i, j}); rangeSt = R.f;
                ways[i][j] += ways[i - 1][j] + ways[i][j - 1];
            }
        }
        for (char c = 'a'; c <= 'z'; c++){
            for (auto [i, j] : test) if (A[i][j] == c)
                tot += ways[i][j] * choose[(n - i) + (m - j)][(m - j)];
            for (auto [i, j] : test) if (A[i][j] == c){
                pll R = {rangeSt, rangeSt + tot - 1};
                if (k >= R.f and k <= R.s){
                    if (ans.size() < len) ans += c;
                    range[i][j] = R;
                }
                else ways[i][j] = 0; //bad spot
            }
            rangeSt += tot; tot = 0;
        }
    }
    cout<<ans<<endl;
}

Compilation message

kthpath.cpp: In function 'int main()':
kthpath.cpp:40:36: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |                     if (ans.size() < len) ans += c;
      |                         ~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct