E. Balance Reset (Interactive dp)

Keywords: less

Original title: http://codeforces.com/gym/101848/problem/E

Title:

There are 2n dishes with their own prices. Each day, N dishes will be randomly selected. You have p yuan now. You can charge money first and then have a meal or not every day. Ask if you can make the balance zero in 365 days.

Analysis:

First of all, it is the requirement of interactive questions. After each input, one item should be output, and the buffer should be refreshed after the output. (directly endl or add fflush(stdout)), directly exit(0) to end the program after the balance is 0.

First, you can charge money indefinitely (100 as a unit), so - 25 = 75, so the state is only 0-99 (100 can be specifically judged). bfs can be used to maintain that it takes at least a few steps to reach each point.

When a state can be transferred to a state with fewer steps, it can be transferred directly. Because there must be a dish to make a state transfer to less state, and the probability of occurrence is at least 1 / 2, so don't worry about not finishing in 365 days.

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int a[2009];
int dp[2009];
bool vis[2009];

int main(){
    int n,p;
    cin>>n>>p;
    for(int i=1;i<=2*n;i++)cin>>a[i];
    queue<int>Q;
    vis[0]=1;
    dp[0]=0;
    Q.push(0);

    while(!Q.empty()){
        int u=Q.front();Q.pop();
        for(int i=1;i<=2*n;i++){
            int v=(u+a[i])%100;
            if(vis[v])continue;
            vis[v]=1;
            dp[v]=dp[u]+1;
            Q.push(v);
        }
    }

    int res=p;
    for(int ii=1;ii<=365;ii++){
        int now[1009];
        for(int i=1;i<=n;i++)scanf("%d",&now[i]);
        if(res==100){
            cout<<0<<' '<<now[1]<<endl;
            res-=a[now[1]];
            if(res==0)exit(0);
            continue;
        }
        int have=0;
        for(int i=1;i<=n;i++){
            int to=(res-a[now[i]]+100)%100;
            if(dp[to]<dp[res]){
                int ans=0;
                if(res-a[now[i]]<0)ans++;
                cout<<ans<<' '<<now[i]<<endl;
                have=1;
                res=to;
                i=n;
            }
        }
        if(!have){
            cout<<0<<' '<<0<<endl;
        }
        if(res==0)exit(0);
    }
}

Posted by powerofphp on Tue, 12 Nov 2019 13:49:07 -0800