洛谷 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驶过,给出aij代表从i站上车j站下车的人的个数。列车行驶过程中你有K次检票机会,所有当前在车上的人会被检票,问最多能检多少个不同的人的票
输入输出格式
输入格式:
In the first line of the standard input two positive integers and
(
,
) are given. These are separated by a single space and denote, respectively, the number of stations en route and the number of controls Byteasar intends to make. The stations are numbered from
to
in the order of appearance on the route.
In the next lines the summary on passengers is given. The
-th line contains information on the passengers who enter the train on the station
- it is a sequence of  an increasing sequence of integers from the interval from
to
separated by single spaces to the standard output. These numbers should be the numbers of stations upon leaving which Byteasar should control the tickets.
输入输出样例
7 2
2 1 8 2 1 0
3 5 1 0 1
3 1 2 2
3 5 6
3 2
1
2 5
思路:dp
设f[i][j]表示第i次检票在第j车站的最优解。
那么就可以很容易列出状态转移方程:
sum是一个关于上车人数的一维前缀和。
summ是一个关于下车人数的二维前缀和。
f[i][j]=max(f[i][j],f[i-1][l]+sum[j]-sum[l]-tot);(tot=summ[j][j]-summ[l][j])
错因:在计算tot时的式子表示错了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,k,ans,pre;
int sum[],an[];
int step[][],f[][];
int map[][],summ[][];
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
for(int j=;j<=i;j++)
summ[i][j]+=summ[i-][j]+summ[i][j-]-summ[i-][j-];
for(int j=i+;j<=n;j++){
scanf("%d",&map[i][j]);
sum[i]+=map[i][j];
summ[i][j]+=map[i][j]+summ[i-][j]+summ[i][j-]-summ[i-][j-];
}
sum[i]+=sum[i-];
}
for(int i=;i<=n;i++)
f[][i]=sum[i]-summ[i][i]-summ[i][]-summ[][i]+summ[][];
for(int i=;i<=k;i++)
for(int j=i;j<=(n-k+i);j++)
for(int l=i-;l<=j-;l++){
int tot=summ[j][j]-summ[l][j];
if(f[i][j]<f[i-][l]+sum[j]-sum[l]-tot){
step[i][j]=l;
f[i][j]=f[i-][l]+sum[j]-sum[l]-tot;
}
}
for(int i=k;i<=n;i++)
if(f[k][i]>ans){ ans=f[k][i];pre=i; }
an[k]=pre;
for(int i=k;i>;i--){
an[i-]=step[i][pre];
pre=step[i][pre];
}
for(int i=;i<=k;i++) cout<<an[i]<<" ";
}
/*
7 2
2 1 8 2 1 0
3 5 1 0 1
3 1 2 2
3 5 6
3 2
1
*/
洛谷 P3486 [POI2009]KON-Ticket Inspector的更多相关文章
- [洛谷P3486]POI2009 KON-Ticket Inspector
问题描述 Byteasar works as a ticket inspector in a Byteotian National Railways (BNR) express train that ...
- 【解题报告】 洛谷 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 不够 没有取 ...
随机推荐
- matplotlib 可视化 —— 定制 matplotlib
1. matplotlibrc 文件 matplotlib使用matplotlibrc [matplotlib resource configurations] 配置文件来自定义各种属性,我们称之为 ...
- spring boot 的常用注解使用 总结
附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...
- Nginx服务器的反向代理proxy_pass配置方法讲解
Nginx的配置还是比较简单的,如: 1 2 3 4 location ~ /* { proxy_pass http://127.0.0.1:8008; } 或者可以 1 2 3 4 loca ...
- C语言基础-第一章
1.常量 直接常量: int mm=100; float nn=100.01; 字符常量: 编译指令,#define 常量名 常量值 (预处理命令,预处理命令都#开头.成为宏命令) 关键字, ...
- css hover图片hover效果兼容ie8
例子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- ACM-ICPC 2017 Asia Urumqi(第八场)
A. Coins Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all wit ...
- 51Nod 和为k的连续区间
一整数数列a1, a2, ... , an(有正有负),以及另一个整数k,求一个区间[i, j],(1 <= i <= j <= n),使得a[i] + ... + a[j] = k ...
- NodeJS学习笔记 (12)网络地址解析-url(ok)
模块概述 nodejs中,提供了url这个非常实用的模块,用来做URL的解析.在做node服务端的开发时会经常用到.使用很简单,总共只有3个方法. 正式讲解前,各位同学先把下面这个图记在心上(来自no ...
- CSS 预处理语言之 less 篇
less 前言 Less 是一门 CSS 预处理语言,它扩充了 CSS 语言,增加了诸如变量.混合(mixin).函数等功能,让 CSS 更易维护.方便制作主题.扩充. 安装 客户端使用 // 引入 ...
- [笔记-图论]Dijkstra
用于求正权有向图 上的 单源最短路 优化后时间复杂度O(mlogn) 模板 // Dijkstra // to get the minumum distance with no negtive way ...