[CSP-campus training] hotel

The meaning of problems

There are n rooms respectively \ (a_i \) Personal \ ((a_i \ Leq. 7) \) , such that by moving the operating person can have a room \ (0, 4 \) A personal in spend a unit cost of a person moving a grid, no solution outputs -1

Sample input
. 7
. 1 0 0 0 0. 7. 3

Sample output
6

Thinking

DP directly engage, began to think of is to use \ (dp_ {i, j} \) before recording \ (i \) extra when rooms are lawful a \ (j \) personally, but to do so \ (j \) may We will keep rising

Thus with \ (dp_ {i, j} \) represents from \ (I \) to room \ (i + 1 \) moved \ (J \) Individuals so that the first \ (I \) rooms meet minimum cost conditions

The i-th person can come in the room through the upper layer obtained, enumerating the transfer layer and this layer can be, since the negative potential (i.e. \ (i + 1 \) to \ (i \) movement), the need to add a standard number (here, plus 7)

Code

#include<bits/stdc++.h>
#define N 100005
#define Min(x,y) ((x)<(y)?(x):(y))
using namespace std;
typedef long long ll;
const ll inf = 10000000000000000;
int n,a[N];
ll f[N][15];

template <class T>
void read(T &x)
{
    char c;int sign=1;
    while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1; x=c-48;
    while((c=getchar())>='0'&&c<='9') x=x*10+c-48; x*=sign;
}

int main()
{
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=0;i<=n;++i)
      for(int j=0;j<=14;++j)
        f[i][j]=inf;
    
    f[0][7]=0;
    for(int i=1;i<=n;++i)
      for(int j=0;j<=14;++j)//now
        for(int k=0;k<=14;++k)//las
        {
            int in=k-7,out=j-7;
            int res=a[i]+in-out;
            if(res==0||res==4||res==7)  
              f[i][j]=Min(f[i][j],f[i-1][k]+abs(in));
        }
    cout<<(f[n][7]==inf ? -1 : f[n][7])<<endl;
    return 0;
}

This is not a reason Rui DP it, wondering what the hell the examination room

Guess you like

Origin www.cnblogs.com/Chtholly/p/11639483.html