能让我凌晨一点多爬起来写一篇题解,大概只能去感谢舍友的呼噜声了叭~
D. Armchairs There are n armchairs, numbered from 1 to n from left to right. Some armchairs are occupied by people (at most one person per armchair), others are not. The number of occupied armchairs is not greater than n2.
For some reason, you would like to tell people to move from their armchairs to some other ones. If the i-th armchair is occupied by someone and the j-th armchair is not, you can tell the person sitting in the i-th armchair to move to the j-th armchair. The time it takes a person to move from the i-th armchair to the j-th one is |i−j| minutes. You may perform this operation any number of times, but these operations must be done sequentially, i. e. you cannot tell a person to move until the person you asked to move in the last operation has finished moving to their destination armchair.
You want to achieve the following situation: every seat that was initially occupied must be free. What is the minimum time you need to do it?
Input The first line contains one integer n (2≤n≤5000) — the number of armchairs.
The second line contains n integers a1,a2,…,an (0≤ai≤1). ai=1 means that the i-th armchair is initially occupied, ai=0 means that it is initially free. The number of occupied armchairs is at most n2.
Output Print one integer — the minimum number of minutes you have to spend to achieve the following situation: every seat that was initially occupied must be free.
解: DP学狗🐕身上了。 这种初始化的方式真的非常巧妙!!! 一下就将上一个人所占的椅子利用极大值,排除在外。 (懒得多写了,困死,直接给代码了)
#include
using namespace std;
const int N=5005;
int a[N],b[N],c1,c2,f[N][N]; //全觉变量未赋值则自动为0
int main()
{
int n;
cin>>n;
for(int i=1;i>x;
if(x==1)
a[++c1]=i;
else
b[++c2]=i;
}
memset(f,0x3f,sizeof f);
for(int i=0;i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?