結果

問題 No.158 奇妙なお使い
ユーザー fumofumofunifumofumofuni
提出日時 2020-12-17 22:55:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 511 ms / 5,000 ms
コード長 2,907 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 213,476 KB
実行使用メモリ 402,856 KB
最終ジャッジ日時 2023-10-21 07:17:21
合計ジャッジ時間 13,677 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 314 ms
390,724 KB
testcase_01 AC 315 ms
390,748 KB
testcase_02 AC 309 ms
390,724 KB
testcase_03 AC 309 ms
390,724 KB
testcase_04 AC 511 ms
402,856 KB
testcase_05 AC 307 ms
390,724 KB
testcase_06 AC 309 ms
390,724 KB
testcase_07 AC 308 ms
390,724 KB
testcase_08 AC 308 ms
390,724 KB
testcase_09 AC 307 ms
390,724 KB
testcase_10 AC 309 ms
390,740 KB
testcase_11 AC 307 ms
390,736 KB
testcase_12 AC 310 ms
390,724 KB
testcase_13 AC 314 ms
390,724 KB
testcase_14 AC 312 ms
390,748 KB
testcase_15 AC 311 ms
390,792 KB
testcase_16 AC 307 ms
390,732 KB
testcase_17 AC 312 ms
390,924 KB
testcase_18 AC 329 ms
392,480 KB
testcase_19 AC 308 ms
390,728 KB
testcase_20 AC 310 ms
390,748 KB
testcase_21 AC 309 ms
390,996 KB
testcase_22 AC 311 ms
390,996 KB
testcase_23 AC 314 ms
390,728 KB
testcase_24 AC 308 ms
390,728 KB
testcase_25 AC 309 ms
390,724 KB
testcase_26 AC 313 ms
390,736 KB
testcase_27 AC 310 ms
390,724 KB
testcase_28 AC 310 ms
390,764 KB
testcase_29 AC 311 ms
390,728 KB
testcase_30 AC 312 ms
390,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=int;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
//const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll base[3]={0,101,10001};
vl decode(ll n){
    vl ret(3);
    ret[2]=n%base[2];n/=base[2];ret[1]=n%base[1];n/=base[1];ret[0]=n;
    return ret;
}
ll calc(ll hash,ll p){
    vl z=decode(hash);
    ll k=p/1000;ll t=z[0];
    z[0]=max(z[0]-k,0);
    p-=(t-z[0])*1000;
    k=p/100;t=z[1];
    z[1]=max(z[1]-k,0);
    p-=(t-z[1])*100;
    if(p<=z[2]){
        z[2]-=p;
        ll ret=0;
        rep(i,3){ret*=base[i];ret+=z[i];}
        return ret;
    }
    else return -1;
}

vl topsort(vvl &g){
    ll n=g.size();
    vl ans,ind(n);
    rep(i,n)for(auto e:g[i])ind[e]++;
    queue<ll> que;
    rep(i,n)if(!ind[i])que.push(i);
    while(!que.empty()){
        ll v=que.front();que.pop();
        ans.pb(v);
        for(auto nv:g[v]){
            ind[nv]--;if(!ind[nv])que.push(nv);
        }
    }
    return ans;
}

int main(){
    ll s=0;
    rep(i,3){s*=base[i];ll t;cin >> t;s+=t;}
    ll b,x=0;cin >> b;
    rep(i,3){x*=base[i];ll t;cin >> t;x+=t;}
    ll c,y=0;cin >> c;
    rep(i,3){y*=base[i];ll t;cin >> t;y+=t;}
    vector<bool> seen(base[1]*base[2]*10+10);
    queue<ll> que;
    seen[s]=true;que.push(s);
    vvl g(base[1]*base[2]*10+10);
    while(!que.empty()){
        ll p=que.front();que.pop();
        //for(auto x:decode(p))cout << x <<" ";cout << endl;
        ll k=calc(p,b);
        if(k!=-1){
            k+=x;
            g[p].pb(k);
            if(!seen[k]){
                que.push(k);seen[k]=true;
            }
        }
        k=calc(p,c);
        if(k!=-1){
            k+=y;
            g[p].pb(k);
            if(!seen[k]){
                que.push(k);seen[k]=true;
            }
        }
    }
    vl ts=topsort(g);
    vl dist(base[1]*base[2]*10+10);
    dist[s]=0;
    for(auto p:ts){
        for(auto to:g[p]){
            chmax(dist[to],dist[p]+1);
        }
    }
    cout << *max_element(all(dist)) <<endl;
}       
0