답안 #89778

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
89778 2018-12-18T12:03:43 Z 314rate K번째 경로 (IZhO11_kthpath) C++14
0 / 100
2 ms 696 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll INF=1000000000000000000;
const int N=30+5;

int n,m;
int k;
ll go[N][N];
string v[N];
ll e[N][N];

vector<pair<int,int>>p[2*N];

ll t[100];

ll add(ll a,ll b)
{
    a+=b;
    if(a>INF)
    {
        a=INF;
    }
    return a;
}

ll mul(ll a,ll b)
{
    if(a==0 || b==0)
    {
        return 0;
    }
    ll ans=a*b;
    if(ans%a!=0 || ans/a!=b)
    {
        return INF;
    }
    if(ans>INF)
    {
        return INF;
    }
    return ans;
}

int main()
{
   /// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            if(i==n && j==m)
            {
                go[i][j]=1;
            }
            else
            {
                go[i][j]=go[i+1][j]+go[i][j+1];
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int s=i+j-1;
            p[s].push_back({i,j});
        }
    }
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];
        v[i]="."+v[i];
    }
    cin>>k;
    e[1][1]=1;
    string ans;
    ans+=v[1][1];
    for(int di=2;di<=n+m-1;di++)
    {
        for(auto &it:p[di-1])
        {
            int r=it.first;
            int c=it.second;
            e[r+1][c]=add(e[r+1][c],e[r][c]);
            e[r][c+1]=add(e[r][c+1],e[r][c]);
        }
        for(int i=0;i<26;i++)
        {
            t[i]=0;
        }

        for(auto &it:p[di])
        {
            int r=it.first;
            int c=it.second;
        //    cout<<"\t"<<r<<" , "<<c<<"\n";
            int f=v[r][c]-'a';
        //    cout<<f<<"\n";
            int a=e[r][c];
            int b=go[r][c];
        //    cout<<"\t\t = "<<a<<" , "<<b<<"\n";
        //    cout<<"\t\t = "<<mul(a,b)<"\n";
            t[f]=add(t[f],mul(a,b));
      ///      cout<<t[f]<<"\n";
        }
        char ch;
        ll cur=0LL;
      //  cout<<"GOOD\n";
        for(int i=0;i<26;i++)
        {
            cur+=t[i];
            if(cur<k) continue;
            cur-=t[i];
            k-=cur;
            ch=i+'a';
            break;
        }

        for(auto &it:p[di])
        {
            int r=it.first;
            int c=it.second;
            if(v[r][c]!=ch)
            {
                e[r][c]=0;
            }
        }
        ans+=ch;
      ///  cout<<":\t"<<ans<<"\n";
    }
///    cout<<" = "<<k<<"\n";
    cout<<ans<<"\n";
    return 0;
}
/**

3 4
abcd
efdg
hijk
4

a b c
b c d
d e f

1 1 1
1 2 0
1 0 0

**/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 584 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 696 KB Output is correct
10 Incorrect 2 ms 696 KB Output isn't correct
11 Halted 0 ms 0 KB -