题目描述

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;
}

洛谷 P3431:[POI2005]AUT-The Bus(离散化+DP+树状数组)的更多相关文章

  1. Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

  3. 洛谷 P2038 无线网络发射器选址 —— 二维树状数组

    题目:https://www.luogu.org/problemnew/show/P2038 大水题暴露出我的愚蠢. 用二维树状数组,然而居然忘了它应该那样写,调了一个小时: 正方形可以超出外面,只要 ...

  4. 洛谷 P1972"[SDOI2009]HH的项链"(离线+树状数组 or 在线+主席树)

    传送门 •题意 给你一个包含 n 个数的数组 $a$: 有 m 此操作,每次操作求区间 [l,r] 中不同数的个数: •题解(离线+树状数组) 以样例 $[1,2,3,4,3,5]$ 为例,求解区间 ...

  5. cf 61 E. Enemy is weak 离散化+树状数组

    题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ...

  6. POJ 2299 Ultra-QuickSort (离散化)+【树状数组】

    <题目链接> 题目大意: 给你一段序列,问你如果每次只交换该序列相邻的两个元素,最少需要交换多少步才能够使该序列变为升序排列. 解题分析: 不难发现,其实本题就是让我们求原始序列的逆序对, ...

  7. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  8. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  9. codeforces 652D D. Nested Segments(离散化+sort+树状数组)

    题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. js获取中国省市区,省市筛选、省市、省市筛选联动。【C#】【js】

    <style type="text/css"> .labelhide { -webkit-box-shadow: 0px 1px 0px 0px #f3f3f3 !im ...

  2. Python中的随机采样和概率分布(一)

    Python(包括其包Numpy)中包含了了许多概率算法,包括基础的随机采样以及许多经典的概率分布生成.我们这个系列介绍几个在机器学习中常用的概率函数.先来看最基础的功能--随机采样. 1. rand ...

  3. 大数据学习day34---spark14------1 redis的事务(pipeline)测试 ,2. 利用redis的pipeline实现数据统计的exactlyonce ,3 SparkStreaming中数据写入Hbase实现ExactlyOnce, 4.Spark StandAlone的执行模式,5 spark on yarn

    1 redis的事务(pipeline)测试 Redis本身对数据进行操作,单条命令是原子性的,但事务不保证原子性,且没有回滚.事务中任何命令执行失败,其余的命令仍会被执行,将Redis的多个操作放到 ...

  4. 【Linux】【Services】【Docker】应用

    1. Docker应用: 镜像:包含了启动Docker容器所需要的文件系统层级及其内容:基于UnionFS采用分层结构实现: bootfs,rootfs registry:保存docker镜像及镜像层 ...

  5. AOP中环绕通知的写法

    package com.hope.utils;import org.aspectj.lang.ProceedingJoinPoint;/** * @author newcityman * @date ...

  6. linux环境centos

    qhost:查看集群 投送到集群qsub -l vf=2G,p=1 work.sh -cwd -V all_section_run.sh 杀死任务 qdel  id qstat -u \* |less ...

  7. SpringSecurity Oauth2.0

    1.用户认证分析 上面流程图描述了用户要操作的各个微服务,用户查看个人信息需要访问客户微服务,下单需要访问订单微服务,秒杀抢购商品需要访问秒杀微服务.每个服务都需要认证用户的身份,身份认证成功后,需要 ...

  8. centos7部署mysql-5.7

    目录 一.环境声明 二.程序部署 三.更改初始密码 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql ...

  9. gitlab官方api使用

    目录 一.简介 二.技术要点 三.案例 一.简介 Gitlab作为一个开源.强大的分布式版本控制系统,已经成为互联网公司.软件开发公司的主流版本管理工具.使用过Gitlab的都知道,想要提交一段代码, ...

  10. 转:Sed使用

    awk于1977年出生,今年36岁本命年,sed比awk大2-3岁,awk就像林妹妹,sed就是宝玉哥哥了.所以 林妹妹跳了个Topless,他的哥哥sed坐不住了,也一定要出来抖一抖. sed全名叫 ...