(ACM ICPC 2013–2014, NEERC, Northern Subregional Contest)

Flight Boarding Optimization
Input file: flight.in
Output file: flight.out
Time limit: 2 seconds
Memory limit: 256 megabytes
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.
There are n passengers, they form a queue and board the plane one by one. If the i-th passenger sits in
a row r i then the difficulty of boarding for him is equal to the number of passengers boarded before him
and sit in rows 1...r i −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 r i (1 ≤ r i ≤ s).
Each row is occupied by at most 6 passengers.
Output
Output one number, the minimal possible difficulty of the boarding.
Example
flight.in                             flight.out
10 12 2                            6
6 4 2 5 2 3 1 11 8 5

此题做的倒是一波三折,

  f[i][j]=min(f[k][j-1]+w[k+1][i])

起先w[][]不会算,凭借wlm牛给的线段树思路起家,之后我调试代码,思路中断,混淆,最终还是wlm牛成功计算出了w[][]

然而在我的kn^2的朴素思想下,过了样例,草草上交,”in Queue“,CF跪了,草!

不管其他,以防万一,匆匆把代码改成了四边形优化,O(nk),其实很像poj1160,只是为了防止超时。

靠!WA on test 5!!! 最后一刻看朴素提交竟然WA了,而四边形优化还在 in Queue,目测裸算法跪了,加优化又能如何?

吃饭,回新校区,一路上和这群acm伙伴各种hi,别是高兴啊。

挖槽!打开电脑时(笔记本已断网),发现四边形优化 Run on test 40,惊呆了。

马上联网一看,A了,吐血啊!!~~~~~~~————————****¥¥¥%%%####

第二天又看了别人代码,也明白了正解。

题目大意:

  乘客按顺序进客机,每进来一个乘客会产生不满度(difficulty),是他之前进来的且坐在他前面的乘客人数,现在可以分k个机厢,求最少difficulty

思路:

  f[i][j]=min(f[k][j-1]+w[k+1][i])

  w[][]求法巧妙,看正解代码即可知晓。

以下给出

线段树+四边形优化代码 O(n^2logn+nk)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x3f3f3f3f
#define N 1005 using namespace std;
struct Point
{
int r,res;
}a[N]; int w[N][N],n,s,k,sum[N*N],f[N][N],ss[N][N];
bool isCalc[N]; bool cmp(Point a, Point b)
{
if(a.r==b.r)
return a.res>b.res;
return a.r<b.r;
} void PushUp(int rt){
sum[rt]=sum[rt*]+sum[rt*+];
}
void build(int l, int r, int rt)
{
sum[rt]=;
if(l==r) return;
int m=(l+r)/;
build(l,m,rt*);
build(m+,r,rt*+);
}
void update(int x, int l, int r, int rt)
{
if(l==r)
{
sum[rt]=;
return;
}
int m=(l+r)/;
if(x<=m) update(x,l,m,rt*);
else update(x,m+,r,rt*+);
PushUp(rt);
}
int query(int x, int y, int l, int r, int rt)
{
if(x>y) return ;
if(x<=l&&y>=r) return sum[rt];
int m=(l+r)/;
int s=;
if(x<=m) s+=query(x,y,l,m,rt*);
if(y>m) s+=query(x,y,m+,r,rt*+);
return s;
}
int main()
{
// freopen("flight.in","r",stdin);
// freopen("flight.out","w",stdout);
scanf("%d%d%d",&n,&s,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i].r);
a[i].res=i;
}
sort(a+,a+n+,cmp);
int head=,d=;
for(int i=; i<=s; i++)
{
build(,n,);
memset(isCalc,,sizeof(isCalc));
d=i;
int tmp=;
while(a[head].r<i) head++;
for(int j=head; j<=n; j++)
{
update(a[j].res,,n,);
while(d<a[j].r)
{
if(!isCalc[d])
{
w[i][d]=tmp;
}
d++;
}
tmp+=query(,a[j].res-,,n,);
w[i][a[j].r]=tmp;
isCalc[a[j].r]=;
}
while(d<=s)
{
if(!isCalc[d])
{
w[i][d]=w[i][d-];
}
d++;
}
}
for(int i=; i<=s; i++)
for(int j=; j<=k; j++)
f[i][j]=INF;
for(int i=; i<=s; i++)
f[i][]=w[][i]; for(int j=; j<=k; j++)
{
ss[s+][j]=s-;
for(int i=s; i>=j; i--)
{
for(int l=ss[i][j-]; l<=ss[i+][j]; l++)
if(f[i][j]>f[l][j-]+w[l+][i])
{
f[i][j]=f[l][j-]+w[l+][i];
ss[i][j]=l;
}
}
}
printf("%d",f[s][k]);
return ;
}

线段树+朴素代码O(n^2logn+kn^2)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x3f3f3f3f
#define N 1005 using namespace std;
struct Point
{
int r,res;
}a[N]; int w[N][N],n,s,k,sum[N*N],f[N][N];
bool isCalc[N]; bool cmp(Point a, Point b)
{
if(a.r==b.r)
return a.res>b.res;
return a.r<b.r;
} void PushUp(int rt){
sum[rt]=sum[rt*]+sum[rt*+];
}
void build(int l, int r, int rt)
{
sum[rt]=;
if(l==r) return;
int m=(l+r)/;
build(l,m,rt*);
build(m+,r,rt*+);
}
void update(int x, int l, int r, int rt)
{
if(l==r)
{
sum[rt]=;
return;
}
int m=(l+r)/;
if(x<=m) update(x,l,m,rt*);
else update(x,m+,r,rt*+);
PushUp(rt);
}
int query(int x, int y, int l, int r, int rt)
{
if(x>y) return ;
if(x<=l&&y>=r) return sum[rt];
int m=(l+r)/;
int s=;
if(x<=m) s+=query(x,y,l,m,rt*);
if(y>m) s+=query(x,y,m+,r,rt*+);
return s;
} int main()
{
freopen("flight.in","r",stdin);
freopen("flight.out","w",stdout);
scanf("%d%d%d",&n,&s,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i].r);
a[i].res=i;
}
sort(a+,a+n+,cmp);
int head=,d=;
for(int i=; i<=s; i++)
{
build(,n,);
memset(isCalc,,sizeof(isCalc));
d=i;
int tmp=;
while(a[head].r<i) head++;
for(int j=head; j<=n; j++)
{
update(a[j].res,,n,);
while(d<a[j].r)
{
if(!isCalc[d])
{
w[i][d]=tmp;
}
d++;
}
tmp+=query(,a[j].res-,,n,);
w[i][a[j].r]=tmp;
isCalc[a[j].r]=;
}
while(d<=s)
{
if(!isCalc[d])
{
w[i][d]=w[i][d-];
}
d++;
}
} for(int i=; i<=s; i++)
for(int j=; j<=k; j++)
f[i][j]=INF;
for(int i=; i<=s; i++)
f[i][]=w[][i];
for(int i=; i<=s; i++)
{
for(int j=; j<=min(i,k); j++)
for(int l=; l<i; l++)
{
if(f[i][j]>f[l][j-]+w[l+][i])
f[i][j]=f[l][j-]+w[l+][i];
}
}
printf("%d",f[s][k]);
return ;
}

正解代码O(n^2+nk)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x3f3f3f3f
#define N 1005 using namespace std; int w[N][N],n,s,k,f[N][N],ss[N][N],a[N]; int main()
{
// freopen("flight.in","r",stdin);
// freopen("flight.out","w",stdout);
scanf("%d%d%d",&n,&s,&k);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
for(int i=; i<=n; i++)
for(int j=i; j<=n; j++)
if(a[i]<a[j])
w[a[i]][a[j]]++;    //w[i][j]表示第i排对第j排的difficulty
for(int j=; j<=s; j++)
{
for(int i=; i<=j; i++)
w[i][j]+=w[i-][j];  //w[i][j]表示前i排对第j排的difficulty
int sum=w[j][j];
for(int i=j; i>=; i--)
w[i][j]=sum-w[i-][j]; //w[i][j]表示第i-j排对第j排的difficlty
}
for(int i=; i<=s; i++)
for(int j=i; j<=s; j++)
w[i][j]+=w[i][j-];  //w[i][j]表示第i-j排对第i-j排的difficulty
// for(int i=1; i<=s; i++)
// for(int j=i; j<=s; j++)
// printf("%d %d %d\n",i,j,w[i][j]);
for(int i=; i<=s; i++)
for(int j=; j<=k; j++)
f[i][j]=INF;
for(int i=; i<=s; i++)
f[i][]=w[][i]; for(int j=; j<=k; j++)
{
ss[s+][j]=s-;
for(int i=s; i>=j; i--)
{
for(int l=ss[i][j-]; l<=ss[i+][j]; l++)
if(f[i][j]>f[l][j-]+w[l+][i])
{
f[i][j]=f[l][j-]+w[l+][i];
ss[i][j]=l;
}
}
}
printf("%d",f[s][k]);
return ;
}

Codeforces_GYM Flight Boarding Optimization的更多相关文章

  1. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

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

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

  3. Codeforces Gym 100269A Arrangement of Contest 水题

    Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...

  4. theano scan optimization

    selected from Theano Doc Optimizing Scan performance Minimizing Scan Usage performan as much of the ...

  5. [Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.

    控制台输出的时候显示一串这样的信息:[Project Name] was compiled with optimization - stepping may behave oddly; variabl ...

  6. Mvc 之System.Web.Optimization 压缩合并如何让*.min.js 脚本不再压缩

    最近项目中用到了easy ui ,但是在配置BundleConfig 的时候出现了问题,easy ui的脚本jquery.easyui.min.js 压缩后出现各种脚本错误,总是莫名其妙的 i标量错误 ...

  7. MySQL Range Optimization

    8.2.1.3 Range Optimization MYSQL的Range Optimization的目的还是尽可能的使用索引 The range access method uses a sing ...

  8. 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)

    今天,在.net4.5,mvc4下新建了个区域,运行起来就报这个错误: 命名空间"System.Web"中不存在类型或命名空间名称"Optimization"( ...

  9. [阅读笔记]Software optimization resources

    http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++   7. The efficiency of differe ...

随机推荐

  1. 高效开发Android App的10个建议(转)

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗 电或者内存占用 ...

  2. VBS基础篇 - Err对象

    Err对象是一个具有全局范围的内部对象,含有关于错误的所有信息.On Error Resume next 忽略运行时产生的所有错误On Error Goto 0 取消忽略错误措施主要方法有:Clear ...

  3. 3144:[HNOI2013]切糕 - BZOJ

    题目描述 Description 经过千辛万苦小 A 得到了一块切糕,切糕的形状是长方体,小 A 打算拦腰将切糕切成两半分给小 B.出于美观考虑,小 A 希望切面能尽量光滑且和谐.于是她找到你,希望你 ...

  4. SQL Server 之 事务隔离级别

    SET TRANSACTION ISOLATION LEVEL xxx  -- 每次设置只针对当前事务块 xxx 取值: READ UNCOMMITTED READ COMMITTED REPEATA ...

  5. QC缺陷管理操作-细说(转)

    一.缺陷常用字段说明 二.缺陷管理流程图 三.开发人员修改缺陷填写规范 四.项目经理决定延期修改缺陷 一.缺陷常用字段说明 1.摘要 对缺陷的简单描述.摘要包括该缺陷所属的模块名称-子模块名称,以及简 ...

  6. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  7. oracle 字符集导入、导出 、转换

    导入导出及转换 导入导出是我们常用的一个数据迁移及转化工具,因其导出文件具有平台无关性,所以在跨平台迁移中,最为常用. 在导出操作时,非常重要的是客户端的字符集设置,也就是客户端的NLS_LANG设置 ...

  8. 【C++基础】 类中static private public protected

    静态成员在一个类的所有实例间共享数据 “类属性”,是描述类的所有对象共同特征的一个数据项,对所有对象,它的值相同,static定义,为整个类所共有.相对于“实例属性” 如果static成员是私有类型, ...

  9. auto_ptr的设计动机

    auto_ptr的设计动机 C++标准程序库提供的auto_ptr是一种智能型指针(smart pointer),帮助程序员防止“被异常抛出时发生资源泄露”. 函数的操作经常依以下模式进行: 1.获取 ...

  10. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...