洛谷 P3431:[POI2005]AUT-The Bus(离散化+DP+树状数组)
题目描述
The streets of Byte City form a regular, chessboardlike network - they are either north-south or west-east directed. We shall call them NS- and WE-streets. Furthermore, each street crosses the whole city. Every NS-street intersects every WE- one and vice versa. The NS-streets are numbered from \(1\) to \(n\), starting from the westernmost. The WE-streets are numbered from \(1\) to \(m\), beginning with the southernmost. Each intersection of the \(i\)'th NS-street with the \(j\)'th WE-street is denoted by a pair of numbers \((i,j)\) (for \(1\le i\le n\), \(1\le j\le m\)).
There is a bus line in Byte City, with intersections serving as bus stops. The bus begins its itinerary by the \((1,1)\) intersection, and finishes by the \((n,m)\) intersection. Moreover, the bus may only travel in the eastern and/or northern direction.
There are passengers awaiting the bus by some of the intersections. The bus driver wants to choose his route in a way that allows him to take as many of them as possible. (We shall make an assumption that the interior of the bus is spacious enough to take all of the awaiting passengers, regardless of the route chosen.)TaskWrite a programme which:
reads from the standard input a description of the road network and the number of passengers waiting at each intersection,finds, how many passengers the bus can take at the most,writes the outcome to the standard output.
Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个数(i, j) 表示(1 <= i <= n, 1 <= j <= m). Byte City里有一条公交线, 在某一些路口设置了公交站点. 公交车从 (1, 1) 发车, 在(n, m)结束.公交车只能往北或往东走. 现在有一些乘客在某些站点等车. 公交车司机希望在路线中能接到尽量多的乘客.帮他想想怎么才能接到最多的乘客.
输入格式
The first line of the standard input contains three positive integers \(n\), \(m\) and \(k\) - denoting the number of NS-streets, the number of WE-streets and the number of intersections by which the passengers await the bus, respectively \((1\le n\le 10^9, 1\le m\le 10^9, 1\le k\le 10^5)\).
The following \(k\) lines describe the deployment of passengers awaiting the bus, a single line per intersection. In the \((i+1)\)'st line there are three positive integers \(x_i, y_i\) and \(p_i\), separated by single spaces, \(1\le x_i\le n,1\le y_i\le m,1\le p_i\le 10^6\) . A triplet of this form signifies that by the intersection\((x_i,y_i)p_i\) passengers await the bus. Each intersection is described in the input data once at the most. The total number of passengers waiting for the bus does not exceed \(1\ 000\ 000\ 000\).
输出格式
Your programme should write to the standard output one line containing a single integer - the greatest number of passengers the bus can take.
输入输出样例
输入
8 7 11
4 3 4
6 2 4
2 3 2
5 6 1
2 5 2
1 5 5
2 1 1
3 1 1
7 7 1
7 4 2
8 6 2
输出
11
思路
首先想到的是一个\(n\times m\)的DP,但是因为\(n,m\)均为\(10^9\),所以肯定是不行的
可以注意到,虽然\(n,m\)很大,但是点的个数却很少,只有\(10^5\)个,所以可以考虑将点先离散化,这样时间就从\(O(n\times m)降到了O(k^2)\),但是依旧会超时
这时,我们可以将每个点按横坐标升序,如果横坐标相同,纵坐标升序的顺序排序,然后进行DP
状态转移方程:\(dp[i]=max(dp[1],dp[2]...dp[i])+p[i]\)对于\(max(dp[i])\),可以用树状数组来求
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
struct wzy
{
int x,y,s;
}p[maxn];
int c[maxn];
int mapx[maxn],mapy[maxn];
bool cmp(wzy u,wzy v)
{
if(u.x==v.x)
return u.y<v.y;
return u.x<v.x;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int place,int num,int n)
{
while(place<=n)
{
c[place]=max(c[place],num);
place+=lowbit(place);
}
}
int query(int place)
{
int ans=0;
while(place>0)
{
ans=max(ans,c[place]);
place-=lowbit(place);
}
return ans;
}
int dp[maxn];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in.txt", "r", stdin);
freopen("/home/wzy/out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=k;i++)
{
cin>>p[i].x>>p[i].y>>p[i].s;
mapx[i]=p[i].x;
mapy[i]=p[i].y;
}
// 离散化
sort(mapx+1,mapx+1+k);
sort(mapy+1,mapy+1+k);
int numx,numy;
numx=numy=k;
numx=unique(mapx+1,mapx+1+numx)-(mapx+1);
numy=unique(mapy+1,mapy+1+numy)-(mapy+1);
for(int i=1;i<=k;i++)
{
p[i].x=lower_bound(mapx+1,mapx+numx+1,p[i].x)-mapx;
p[i].y=lower_bound(mapy+1,mapy+numy+1,p[i].y)-mapy;
}
sort(p+1,p+1+k,cmp);
for(int i=1;i<=k;i++)
{
dp[i]=query(p[i].y)+p[i].s;
update(p[i].y,dp[i],k);
}
int ans=0;
for(int i=1;i<=k;i++)
ans=max(ans,dp[i]);
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/11582724.html
- 简明 Git 命令速查表
- spring cloud 学习(8) - sleuth & zipkin 调用链跟踪
- 快速学习Bash
- JavaProblem之hashCode详解
- spring cloud 学习(6) - zuul 微服务网关
- ES6 Features系列:Template Strings & Tagged Template Strings
- 基于Tcp协议的简单Socket通信实例(JAVA)
- spring集成kafka
- Java常用类(二)String类详解
- 用树莓派玩转蓝牙
- CSS魔法堂:你真的理解z-index吗?
- HashSet
- 树莓派的GPIO编程
- Java集合源码分析(三)Vevtor和Stack
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android判断登录情况
- linux尝试登录失败后锁定用户账户的两种方法
- Linux内存泄漏检测shell脚本
- 详解Linux系统中网卡MAC地址克隆方法
- linux下日志定时轮询的流程详解
- Vim中查找替换及正则表达式的使用详解
- CentOS 7下部署php7.1和开启MySQL扩展的方法教程
- Ubuntu系统下用Crontab命令定时执行PHP文件详解
- CentOS7 LNMP+phpmyadmin环境搭建 第三篇phpmyadmin安装
- Linux有限状态机FSM的理解与实现
- Linux下浅谈crond与crontab的命令用法
- centos 7系统下安装Jenkins的步骤详解
- linux tomcat配置https的方法
- Linux 中firewall的使用方法总结
- CentOS 7 安装vsftpd 服务器的具体操作步骤