跳到正文

题解

题解:AT_abc369_f [ABC369F] Gather Coins

Elysian_Realme约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年9月1日
收录
2026年4月4日
更新
2026年9月21日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

这题数据范围小一点就是一道经典 dp。但是 H 和 W 范围都在 2×1052\times 10^5 无论是空间还是时间都受不了。

但是注意到金币的数量并不多,所以我们从金币的角度去考虑。

注意到若现在有三枚金币 i,j,ki,j,k。若取完 ii 之后能取到 jj,且取完 jj 之后能取到 kk,那么取完 ii 之后肯定能取到 kk。这启发我们可以用 lis 解决。

先把金币以横坐标为第一关键字,纵坐标为第二关键字排序,然后根据纵坐标做一次 lis 并记录路径。

因为是以横坐标为第一关键字而且是上升子序列所以得出的答案必定符合要求而且一定是最长的。

C++
// Problem: F - Gather Coins
// Contest: AtCoder - AtCoder Beginner Contest 369
// URL: https://atcoder.jp/contests/abc369/tasks/abc369_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// By:lmq
// AC Time:2024-08-31 20:02:30

#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void print(int x){
	static int s[20],t=0;
	do s[++t]=x%10,x/=10;while(x);
	while(t) putchar(s[t--] + '0');
}
const int N=2e5+5;
struct node{
	int x,y;
	bool operator<(node b)const{
		return x!=b.x?x<b.x:y<b.y;
	}
}a[N];
int h,w,n,len;
int f[N];
int lst[N];
stack<node> p;
signed main(){
	cin>>h>>w>>n;
	for(int i=1;i<=n;i++)
		a[i].x=read(),a[i].y=read();
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		int l=0,r=len+1;
		while(l+1!=r){
			int mid=(l+r)>>1;
			if(a[f[mid]].y<=a[i].y)
				l=mid;
			else
				r=mid;
		}
		lst[i]=f[r-1];
		len=max(len,r);
		f[r]=i;
	}
	cout<<len<<endl;
	p.push({h,w});
	int t=f[len];
	while(t){
		p.push(a[t]);
		t=lst[t];
	}
	node l={1,1};
	while(!p.empty()){
		for(int i=1;i<=p.top().x-l.x;i++)
			putchar('D');
		for(int i=1;i<=p.top().y-l.y;i++)
			putchar('R');
		l=p.top();
		p.pop();
	}
	return 0;
}
//0 0 0 1
//1 0 1 0
//0 0 1 0
// 某一日,祂从天坠落。人们抬头仰望,于是看见了星空。
// 星月送来神的女儿,她愿成为人的伴侣。
// 长风化作她的轺车,四海落成她的园圃。鸟雀衔来善的种子,百花编织爱的颂歌。
// 她便是这样降生于世,行于大地,与人类一同长大,与世界一起发芽。
// 而今,「终焉」之时将至。
// 而今,「归去」之时已至。
// 就此告别吧,美丽的世界。
// 此后,将有「群星」闪耀,因为我如今来过。
// 此后,将有「百花」绽放,因为我从未离去。
// 请将我的箭、我的花、与我的爱,织成新生的种子,带向那枯萎的大地。
// 然后,便让它开出永恒而无瑕的…「人性之华」吧。
// 「我名为爱莉希雅……」
// 「最初的律者,人之律者。」
原文