跳到正文

题解

B4156 [厦门小学生 C++ 2023] 太空旅行 题解

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

文章信息

收录
2025年12月12日
更新
2025年12月27日
篇幅
约 4 分钟
题目
B4156

版本

  1. v1当前

快进到已经知道要排序了,只是不会处理相等的情况。

对于读入的第 ii 个数字,我们将其加上 i×10−8i\times 10^{-8},这样就不存在相等的情况。

这样就可以直接排序了。

C++
#include <bits/stdc++.h>
using namespace std;
constexpr int Spp{1<<20},S2{1<<20};
char buf[Spp],*p1,*p2,buf2[S2],*l2=buf2,_st[22];
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,Spp,stdin),p1==p2)?EOF:*p1++)
#define putchar(c) (l2==buf2+S2&&(fwrite(buf2,1,S2,stdout),l2=buf2),*l2++=(c))
template <typename T>
void read(T &x) {
	cin>>x;
}
template <typename T,typename ...Args>
void read(T& x,Args&... args) {read(x);read(args...);}
template<typename T>
void write(T x,char c='\n') {
	if (x<0) putchar('-'),x*=-1;
	int tp=0;
	do _st[++tp]=x%10; while (x/=10);
	while (tp) putchar(_st[tp--]+'0');
	putchar(c);
}
struct OI{~OI(){fwrite(buf2,1,l2-buf2,stdout);}}oi;
constexpr int N(4e5);
using LL=long long;
struct node {
	LL s,c;
	int i;
	friend bool operator<(const node &a,const node &b) {
		double l{min(a.s+1e-8*a.i,b.c+1e-8*(b.i+N))},r{min(a.c+1e-8*(b.i+N),b.s+1e-8*b.i)};
		return l<r;
	}
} a[N+5];
struct info {
	LL s,px,py;
	friend info operator+(const info &a,const info &b) {
		return {a.s+b.s,max(a.px+b.py,a.s+b.px),a.py+b.py};
	}
} t[N*2];
int is[N+5],m;
int act[N+5];
bool alive[N+5];
void mod(int x,node y) {
	x+=m-1;
	t[x]={y.s,y.s+y.c,y.c};
	while (x>>=1) t[x]=t[x<<1]+t[x<<1|1];
}
LL qr() {
	info la{0,0,0},ra{0,0,0};
	for (int l{m},r{2*m-1};l<=r;l>>=1,r>>=1) {
		if (l&1) la=la+t[l++];
		if (~r&1) ra=t[r--]+ra;
	}
	return (la+ra).px;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int n,q;read(n);q=0;
	for (int i{1};i<=n;++i) read(a[i].s,a[i].c),a[i].i=i;
	m=n;
	sort(a+1,a+1+m);
	for (int i{1};i<=m;++i) act[a[i].i]=i;
	for (int i{1};i<=n;++i) mod(act[i],a[act[i]]),alive[act[i]]=true;
	write(qr());
	return 0;
}
原文