[洛谷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 不够 没有取 ...
随机推荐
- WinForm实现最小化右下角
首先,要在窗体里面加入这么两个控件,左边的是托盘控件,右边的是菜单控件. 然后设置窗体的FormClosing事件: if (e.CloseReason == CloseReason.UserClos ...
- python-笔记(一)python简介和入门
一.什么是python? python是一种面向对象.解释型的计算机语言,它的特点是语法简洁.优雅.简单易学.在1989诞生,Guido(龟叔)开发.这里的python并不是蟒蛇的意思,而是龟叔非常喜 ...
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
- 转)delphi chrome cef3 控件学习笔记 (二)
(转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...
- 分享:陆行鸟BGM合集
第39首是原版哦 https://pan.baidu.com/mbox/homepage?short=pMkAqhX
- Jenkins---简单认识
老生常谈:知其然更要知其所以然,所以补充一下Jenkins的相关知识 官方文档:https://jenkins.io/zh/doc/ 问题1:Jenkins是什么? 1.1.Jenkins是一个开源的 ...
- 【Unity】给物品添加力
给物体添加力 两个方法: Rigidbody.AddForce(Vector3,ForceMode):给刚体添加一个力,让刚体按世界坐标系进行运动 Rigidbody.AddRelativeForce ...
- 史上最全的ORACLE基础教程
ORACLE命令和语句挺多,全部记忆下来不现实,况且有不常用的指令.下面把大部分的指令做了记录和详细的注释.建议收藏.转发此篇文章,如果忘记可以翻出来查查.关注公众号it_learn获取更多学习资源 ...
- Spring源码解析-核心类之XmlBeanFactory 、DefaultListableBeanFactory
DefaultListableBeanFactory XmlBeanFactory 继承自 DefaultListableBeanFactory , 而 DefaultListableBeanFact ...
- [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...