[洛谷P3486]POI2009 KON-Ticket Inspector
问题描述
Byteasar works as a ticket inspector in a Byteotian National Railways (BNR) express train that connects Byteburg with Bitwise.
The third stage of the BNR reform (The never ending saga of BNR reforms and the Bitwise hub was presented in the problems Railway from the third stage of XIV Polish OI and Station from the third stage of XV Polish OI. Their knowledge, however, is not required at all in order to solve this problem.) has begun. In particular, the salaries system has already been changed.
For example, to encourage Byteasar and other ticket inspectors to efficient work, their salaries now depend on the number of tickets (passengers) they inspect. Byteasar is able to control all the passengers on the train in the time between two successive stations, but he is not eager to waste his energy in doing so. Eventually he decided he would check the tickets exactly times per ride.
Before setting out, Byteasar is given a detailed summary from which he knows exactly how many passengers will travel from each station to another. Based on that he would like to choose the moments of control in such a way that the number of passengers checked is maximal. Obviously, Byteasar is not paid extra for checking someone multiple times - that would be pointless, and would only disturb the passengers. Write a programme that will determine for Byteasar when he should check the tickets in order to maximise his revenue.
有n个车站,现在有一辆火车从1到n驶过,给出\(a_ij\)代表从i站上车j站下车的人的个数。列车行驶过程中你有K次检票机会,所有当前在车上的人会被检票,问最多能检多少个不同的人的票。
传送门
题解
由定义就可以得出状态的表达方式:设\(f[i][j]\)表示在第j个车站时检第i次票的最大检票人数,那么显然\(f[i][j]\)由前面某一个\(f[i-1][k]\)转移过来。那么需要确定的是从k到j有多少人在此时间段上车且没有下车。输入中的矩阵是锯齿形的,实际上全部向右边掉落后可以转化为一个矩阵a,其中\(a[i][j]\)表示在i上车j下车的人数。那么如何找到在一个区间内上车的人数呢?可以发现,在i时还在的人数就是所有在i之前到、在i之后走的人。那么对应的就是矩阵a中满足横坐标不大于i、纵坐标大于i的点的权值和。可以用类似前缀和的方法来计算。扩展到区间,一个时间段(i,j)中在i之后下车且在j之前没有下车的人就是横坐标大于i小于j、纵坐标大于j的点的和。所以,设\(sum[i][j]\)表示以(x,y)为左下角、(1,n)为右下角的矩阵和,则状态转移方程如下:
\]
答案为:
\]
接下来的问题是如何输出路径。记\(pre[i][j]\)表示状态\(f[i][j]\)是从哪一个车站转移过来的,在确定答案后倒推回去即为最佳方案(记得倒序输出)。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#define N 1002
using namespace std;
int n,m,i,j,k,a[N][N],f[N][N],sum[N][N],pre[N][N];
vector<int> v;
int main()
{
cin>>n>>m;
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++) cin>>a[i][j];
}
for(i=1;i<=n;i++){
for(j=n;j>=1;j--) sum[i][j]=sum[i-1][j]+sum[i][j+1]-sum[i-1][j+1]+a[i][j];
}
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
for(k=i-1;k<j;k++){
if(f[i-1][k]+sum[j][j+1]-sum[k][j+1]>f[i][j]){
f[i][j]=f[i-1][k]+sum[j][j+1]-sum[k][j+1];
pre[i][j]=k;
}
}
}
}
int maxx=0,ans=0;
for(i=m;i<=n;i++){
if(f[m][i]>maxx){
maxx=f[m][i];
ans=i;
}
}
int p=ans;
for(i=m;i>=1;i--){
v.push_back(p);
p=pre[i][p];
}
for(i=v.size()-1;i>=0;i--) cout<<v[i]<<' ';
cout<<endl;
return 0;
}
[洛谷P3486]POI2009 KON-Ticket Inspector的更多相关文章
- 洛谷 P3486 [POI2009]KON-Ticket Inspector
P3486 [POI2009]KON-Ticket Inspector 题目描述 Byteasar works as a ticket inspector in a Byteotian Nationa ...
- 【解题报告】 洛谷 P3492 [POI2009]TAB-Arrays
[解题报告] 洛谷 P3492 [POI2009]TAB-Arrays 这题是我随机跳题的时候跳到的.写完这道题之后,顺便看了一下题解,发现只有一篇题解,所以就在这里顺便写一个解题报告了. 首先当然是 ...
- 洛谷 P3480 [POI2009]KAM-Pebbles
https://www.luogu.org/problemnew/solution/P3480 讲不清楚... 首先对原序列做差分:设原序列为a,差分序列为d 那么,每一次按题意在原序列位置i处取走石 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
随机推荐
- MDX 入门
之前用到的SQL,解释:结构化查询语言(Structured Query Language)(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种 ...
- 后台管理模板ACE
要做系统,界面展示效果较简单,1个开发人员,无美工,无前端! 以前一直用bootstrap来做界面.这次也走的老路!同样适用的bootstrap界面.做完之后,功能上,我还是比较满意的,兼容IE8+, ...
- loc() iloc() at() iat()函数
1 四个函数都是用于dataframe的定位 []用于直接定位. loc()函数是用真实索引,iloc()函数是用索引序号. loc()函数切片是左闭右闭,iloc()函数切片是左闭右开. at(), ...
- SpringMvc @ModelAttribute 的用法
参考:Spring 3.x 企业应用开发实战 第15章:SpringMvc 页码:532 ModelAttribute 从字面上解释就是模型的属性. 对于MVC框架来说是模型数据是最重要的,因为 ...
- django-xadmin常用内容记录
自定义菜单名称: 1 修改app下的 apps.py文件 添加 class OperationConfig(AppConfig): name = 'operation' verbose_name = ...
- C# 模拟登陆
原理 我们知道,一般需要登录的网站,服务器和客户端都会有一段时间的会话保持,而这个会话保持是在登录时候建立的, 服务端和客户端都会持有这个KEY,在后续访问时,都需要核对这两个KEY是否一致. 而客户 ...
- mssql中quotename的作用
quotename是保证字符原样输入到mssql中 eg: select * from '+QUOTENAME(@tbname) 用于所传字符为mssql保留字样
- ubuntu彩色图形界面
Ubuntu的默认 ~/.bashrc 文件里,有一个控制是否打开彩色提示符文件的变量 $force_color_promt,只需要打开这个变量的开关,就可以使用彩色的命令行提示符了. 这对于输查看命 ...
- Spring Security框架入门
1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框 ...
- IDEA创建SpringBoot+maven项目
1.创建项目: 2.选择spring Initializr,注意要选择jdk,使用默认的spring.io这样就不用再去写pom文件了 3.输入项目名称: 4.选择Spring Web 5.目录结构: