[洛谷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 不够 没有取 ...
随机推荐
- 网络处理器(Network Processor)
网络处理器(Network Processor,简称NP),又可以称为交换芯片,专用于实现核心交换机高速转发功能. 根据网络处理器会议(Network Processors Conference)的定 ...
- NOIP2012 洛谷P1083 借教室
传送门 题意:有一些学(xian)生(quan)要借教室.在n天内,第i天学校有ri个教室.有m份订单,每份订单有三个数值dj,sj,tj,分别表示这个订单从第sj天开始到第tj天结束(包括端点),每 ...
- 三十七、python中的logging介绍
A.单文件日志 import logging#定义日志文件#文件格式logging.basicConfig(filename='log.log', format='%(asctime)s-%(name ...
- 安装U盘启动ferdora-22-fce笔记
如何格式化为fat? windows图形界面格式化, 选项中没有fat, 只有fat32和exfat两种upan格式 Fat就是 传统的FAT16 要格式化为fat, 需要使用cmd的format命令 ...
- SpringBoot系列:一、SpringBoot搭建
打开IDEA,新建一个spring工程,然后无脑下一步就行. 新建完成后的目录结构 java文件夹下是java源码 resources下是配置文件 test下是测试文件 添加web模块支持,在pom. ...
- 002-es5.4.3结合spring-data-elasticsearch3.0.0.0使用
一.使用前设置 1.elasticsearch开启 cmd下,进入安装目录 cd D:\developToool\elasticsearch-5.4.3 elasticsearch # 或者后台运行 ...
- java对接短信平台
短信验证码目前是比较主流验证身份的一种方式,下面分享下我对接的几种短信平台 阿里云短信:https://api.alidayu.com/docs/api.htm?spm=a3142.7395905.4 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_17-ArrayList练习二_存储自定义
分析题目和解题思路 先来新建学生类.定义两个成员变量,后面进行代码的生成 遍历集合
- 《图解设计模式》读书笔记5-2 decorator模式
目录 代码演示 模式的角色和类图 思路拓展 Decorator模式即装饰器模式,就是对类进行装饰,下面通过代码说明. 代码演示 代码展示的内容 有一个类StringDisplay:表示一句话,比如he ...
- jmeter之三种参数化
前言:总结并记录几种jmeter比较有用的元件 1.接口文档 2.参数化 3.断言 1.接口文档 a.拿到接口文档 接口地址:http://localhost:8080/jpress/admin/lo ...