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;
}

Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp的更多相关文章

  1. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  2. Gym - 100269F Flight Boarding Optimization(dp+树状数组)

    原题链接 题意: 现在有n个人,s个位置和你可以划分长k个区域你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量问你怎么划分这s个位置(当然,每个区域必须是连续 ...

  3. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  4. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  5. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

  6. 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)

    [题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...

  7. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  8. Codeforces 216D Spider&#39;s Web 树状数组+模拟

    题目链接:http://codeforces.com/problemset/problem/216/D 题意: 对于一个梯形区域,假设梯形左边的点数!=梯形右边的点数,那么这个梯形为红色.否则为绿色, ...

  9. Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)

    题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...

随机推荐

  1. CMD命令行下载文件

    远程执行sct的另一种姿势 cscript /b C:\Windows\System32\Printing_Admin_Scripts\zh-CN\pubprn.vbs 127.0.0.1 scrip ...

  2. 网络知识===wireshark抓包出现“TCP segment of a reassembled PDU”的解释(载)

    网上胡说八道,众说风云,感觉这篇还算靠谱点. 原文链接:http://blog.csdn.net/dog250/article/details/51809566 为什么大家看到这个以后总是会往MSS, ...

  3. 3.FireDAC组件快照

    TFDManager 连接定义和Connect连接管理  TFDConnection 数据库连接组件,支持三种连接方式:1.持久定义(有一个唯一名称和一个配置文件,可以由FDManager管理) 例: ...

  4. XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件

    XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行COPY.除非 ...

  5. 【模板】BZOJ 3685: 普通van Emde Boas树——Treap

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3685 据说神犇都是用zkw线段树水过的啊... 我蒟蒻只会写treap,加了fread之后8 ...

  6. 2017多校第7场 HDU 6128 Inverse of sum 推公式或者二次剩余

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/a ...

  7. canvas画画板,canvas画五角星,canvas制作钟表、Konva写钟表

    制作一个画画板,有清屏有橡皮擦有画笔可以换颜色 style样式 <head> <meta charset="UTF-8"> <title>画画板 ...

  8. 初识ES6

    1.ECMAScript的官网地址:http://www.ecma-international.org/cma-262/6.0/,其是JS语言的下一代标准,已经在2015年6月正式发布,目标是让JS可 ...

  9. django “如何”系列2:如何编写django-admin 命令

    应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作.这节我们将为polls应用添加一个closepoll的命令 添加一个managemen ...

  10. Cloudstack平台实战

    https://blog.csdn.net/zhangliu463884153/article/details/80606020