Flight Boarding Optimization

题目连接:

http://codeforces.com/gym/100269/attachments

Description

Peter is an executive boarding manager in Byteland airport. His job is to optimize the boarding process.

The planes in Byteland have s rows, numbered from 1 to s. Every row has six seats, labeled A to F.

F

E

D

C

B

A

1 2 3 4 5 6 7 8 9 10

...

...

...

...

...

...

s

Boarding

There are n passengers, they form a queue and board the plane one by one. If the i-th passenger sits in

a row ri then the difficulty of boarding for him is equal to the number of passengers boarded before him

and sit in rows 1 . . . ri −1. The total difficulty of the boarding is the sum of difficulties for all passengers.

For example, if there are ten passengers, and their seats are 6A, 4B, 2E, 5F, 2A, 3F, 1C, 10E, 8B, 5A,

in the queue order, then the difficulties of their boarding are 0, 0, 0, 2, 0, 2, 0, 7, 7, 5, and the total

difficulty is 23.

To optimize the boarding, Peter wants to divide the plane into k zones. Every zone must be a continuous

range of rows. Than the boarding process is performed in k phases. On every phase, one zone is selected

and passengers whose seats are in this zone are boarding in the order they were in the initial queue.

In the example above, if we divide the plane into two zones: rows 5–10 and rows 1–4, then during the first

phase the passengers will take seats 6A, 5F, 10E, 8B, 5A, and during the second phase the passengers

will take seats 4B, 2E, 2A, 3F, 1C, in this order. The total difficulty of the boarding will be 6.

Help Peter to find the division of the plane into k zones which minimizes the total difficulty of the

boarding, given a specific queue of passengers.

Input

The first line contains three integers n (1 ≤ n ≤ 1000), s (1 ≤ s ≤ 1000), and k (1 ≤ k ≤ 50; k ≤ s).

The next line contains n integers ri (1 ≤ ri ≤ s).

Each row is occupied by at most 6 passengers.

Output

Output one number, the minimal possible difficulty of the boarding

Sample Input

10 12 2

6 4 2 5 2 3 1 11 8 5

Sample Output

6

Hint

题意

现在有n个人,s个位置和你可以划分长k个区域

你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量

问你怎么划分这s个位置(当然,每个区域必须是连续的),才能使得总代价最小

输出代价

题解:

数据范围1000,显然的dp

dp[i][j]表示第i个位置是第j个区域的结尾,然后暴力转移就好了

用树状数组预处理sum[i][j],表示第i个位置和第j个位置划分在一起的代价是多少

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1050;
int dp[maxn][55];
int sum[maxn][maxn];
int r[maxn];
int n,m,k;
int a[maxn];
vector<int> E[maxn];
int lowbit(int x)
{
return x&(-x);
}
int get(int x)
{
int ans = 0;
for(int i=x;i;i-=lowbit(i))
ans+=a[i];
return ans;
}
void update(int x,int v)
{
for(int i=x;i<maxn;i+=lowbit(i))
a[i]+=v;
}
int main()
{
freopen("flight.in","r",stdin);
freopen("flight.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&r[i]);
E[r[i]-1].push_back(i);
}
for(int i=0;i<m;i++)
{
memset(a,0,sizeof(a));
for(int j=i;j<m;j++)
{
if(i!=j)sum[i][j]=sum[i][j-1];
for(int t=0;t<E[j].size();t++)
sum[i][j]+=get(E[j][t]);
for(int t=0;t<E[j].size();t++)
update(E[j][t],1);
}
} for(int i=0;i<=m;i++)
for(int j=0;j<=k;j++)
dp[i][j]=1e9;
dp[0][0]=0;
for(int i=0;i<m;i++)
for(int j=0;j<k;j++)
{
if(dp[i][j]==1e9)continue;
for(int t=i;t<m;t++)
dp[t+1][j+1]=min(dp[t+1][j+1],dp[i][j]+sum[i][t]);
}
cout<<dp[m][k]<<endl;
}

最新文章

  1. 卷积神经网络_(4)_caffe简介
  2. 64位系统 IIS中应用程序池设置导致 访问数据库错误
  3. Oracle数据库更新时间的SQL语句
  4. Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信
  5. IOS UI 第三篇:基本UI
  6. 一台电脑 一起跑python2 python3
  7. OpenVPN client端配置文件详细说明(转)
  8. openresty+lua劫持请求,有点意思
  9. SpringMvc+AngularJS通过CORS实现跨域方案
  10. 介绍几款 Python 类型检查工具
  11. Javascript 面向对象(共有方法,私有方法,特权方法,静态属性和方法,静态类)示例讲解
  12. 记录一次spark连接mysql遇到的问题
  13. API验证插件
  14. 洛谷P3740 [HAOI2014]贴海报
  15. python列表排序方法reverse、sort、sorted
  16. django 用户注册功能实现
  17. 深入理解java虚拟机学习笔记(一)
  18. 状压DP 【洛谷P3694】 邦邦的大合唱站队
  19. sublime text2 for mac 实现json格式化
  20. Oracle 内置函数

热门文章

  1. [Leetcode Week15]Populating Next Right Pointers in Each Node
  2. monkey测试===monkeyrunner测试教程(1)
  3. 使用cURL POST上传文件
  4. ADO POST时出现“无法为更新定位行,一些值可能已在最后一次读取后已更改”问题的解决方法
  5. Canvas 高级
  6. AspxGridView在cell内显示颜色
  7. hit-testing机制介绍
  8. TCP三次握手和四次挥手及用户访问网页流程
  9. sublime view_in_browser
  10. python在windows下连接mysql数据库