其他分享
首页 > 其他分享> > Educational Codeforces Round 109 (Rated for Div. 2) D. Armchairs(DP)

Educational Codeforces Round 109 (Rated for Div. 2) D. Armchairs(DP)

作者:互联网

#include<iostream>
#include<cstring>
using namespace std;
const int N = 5005;
int f[N][N], a[N], pos[N];
int n, cnt = 0;
int main()
{
    memset(f, 0x3f, sizeof f);
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(a[i]) pos[++cnt] = i; //记录已经被占座的椅子的位置
    }
    for(int i = 0; i <= n; i++) f[0][i] = 0; //前0个人在前i把椅子中完成转移花费的时间为0
    for(int i = 1; i <= cnt; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            f[i][j] = f[i][j-1]; //不坐该把椅子
            if(!a[j]) //该把椅子原来无人坐
                f[i][j] = min(f[i][j-1], f[i-1][j-1] + abs(j - pos[i]));
        }
    }
    cout << f[cnt][n] << endl;
    return 0;
}

标签:Educational,Rated,标记,int,Codeforces,转移,pos,Armchairs,椅子
来源: https://www.cnblogs.com/K2MnO4/p/14806053.html