跳到正文

题解

题解:P11861 [CCC 2025 Senior] 写作业 / To-Do List

wangyizhi约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
P11861

版本

  1. v1当前

神奇 DS 题。

提供一个与众不同的大常数解法!!!

题目分析

根据生活经验,优先做 ss 小的任务一定不劣。

证明:设 s1<s2s_1<s_2,则先做第 11 个任务的时间为 S1=max⁡{s1+t1+t2,s2+t2}S_1=\max\{s_1+t_1+t_2,s_2+t_2\},先做第 22 个任务的时间为 S2=max⁡{s2+t2+t1,s1+t1}S_2=\max\{s_2+t_2+t_1,s_1+t_1\}。显然 S2=s2+t2+t1>S1S_2=s_2+t_2+t_1>S_1。证毕。

注意到做任务的时间段可能会连在一起形成一个大段,而只有最后一个大段会影响答案。设排好序后最后一个大段从 kk 开始,则答案为 sk+∑i=kti−1s_k+\sum_{i=k}t_i-1。

记 Wi=si+∑j=inti−1W_i=s_i+\sum_{j=i}^{n}t_i-1。考虑这个 WkW_k 有什么特别之处。摆结论:WkW_k 是所有 WiW_i 中最大的。

证明:对于 i<ki<k,一定有 Wi<WkW_i<W_k,因为其中存在空的部分,如果计算的时候把它去掉了必定会使其减小。对于 i>ki>k,一定有 Wi≤WkW_i\le W_k,因为去掉第一段后可能导致后面的前移,会使其减小。

考虑用平衡树维护。对每个节点维护 si,tis_i,t_i,子树内的 Wi+1W_i+1 的最大值,子树内的总和。push_up 应该比较简单了,详见代码实现。

AC Code

C++
by wangyizhi(571247)
#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
using ll=long long;
using ld=long double;
//#define int ll
using pii=pair<int,int>;
//bool Mst;
const int V=2e6+5;
mt19937 rnd(time(nullptr));
struct piii
{
	ll fi,se,th;
};
inline piii nxt(piii x){return {x.fi,x.se,x.th+1};}
inline piii pre(piii x){return {x.fi,x.se,x.th-1};}
bool operator==(piii x,piii y){return x.fi==y.fi&&x.se==y.se&&x.th==y.th;}
bool operator<(piii x,piii y)
{
	if(x.fi!=y.fi) return x.fi<y.fi;
	if(x.se!=y.se) return x.se<y.se;
	return x.th<y.th;
}
bool operator>(piii x,piii y)
{
	if(x.fi!=y.fi) return x.fi>y.fi;
	if(x.se!=y.se) return x.se>y.se;
	return x.th>y.th;
}
bool operator<=(piii x,piii y){return x==y||x<y;}
bool operator>=(piii x,piii y){return x==y||x>y;}
struct treap
{
	struct node
	{
		node *ch[2];
		int w;
		piii v; // fir:s sec:t
		ll ans,st; // ans:max{s+pret},st:sumt
		node(piii _v={0,0,0}){ch[0]=ch[1]=nullptr,w=rnd(),v=_v,ans=v.fi+v.se,st=v.se;}
		node(node &x){w=x.w,v=x.v,ans=x.ans,st=x.st;}
		void push_up()
		{
			ans=st=0;
			if(ch[1]!=nullptr) ans=max(ans,ch[1]->ans),st=ch[1]->st;
			ans=max(ans,v.fi+v.se+st),st+=v.se;
			if(ch[0]!=nullptr) ans=max(ans,ch[0]->ans+st),st+=ch[0]->st;
		}
	};
	node *rt;
	pair<node*,node*> split_val(piii v,node *cur)
	{
		if(cur==nullptr) return {nullptr,nullptr};
		else if(cur->v<=v)
		{
			auto tmp=split_val(v,cur->ch[1]);
			cur->ch[1]=tmp.first,cur->push_up();
			return {cur,tmp.second};
		}
		else
		{
			auto tmp=split_val(v,cur->ch[0]);
			cur->ch[0]=tmp.second,cur->push_up();
			return {tmp.first,cur};
		}
	}
	node *merge(node *u,node *v)
	{
		if(u==nullptr&&v==nullptr) return nullptr;
		else if(u==nullptr) return v;
		else if(v==nullptr) return u;
		else if(u->w<v->w)
		{
			u->ch[1]=merge(u->ch[1],v),u->push_up();
			return u;
		}
		else
		{
			v->ch[0]=merge(u,v->ch[0]),v->push_up();
			return v;
		}
	}
	void insert(piii v)
	{
		auto tmp=split_val(v,rt);
		node *nd=new node(v);
		rt=merge(merge(tmp.first,nd),tmp.second);
	}
	void erase(piii v)
	{
		auto tmp=split_val(v,rt);
		auto tmpl=split_val(pre(v),tmp.first);
		if(tmp.first==tmpl.second) tmp.first=nullptr;
		delete tmpl.second;
		tmpl.second=nullptr;
		rt=merge(tmpl.first,tmp.second);
	}
}tr;
const int N=1e6+5,p=1e6+3;
ll s[N],t[N];
//bool Med;
signed main()
{
//	cerr<<"Memory Size: "<<abs((&Med)-(&Mst))/1024.0/1024<<" MB\n";
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int q,cnt=0,x;
	ll lstans=0;
	char op;
	cin>>q;
	while(q--)
	{
		cin>>op;
		if(op=='A') cnt++,cin>>s[cnt]>>t[cnt],(s[cnt]+=lstans)%=p,(t[cnt]+=lstans)%=p,tr.insert({s[cnt],t[cnt],cnt});
		else if(op=='D') cin>>x,x=(x+lstans)%p,tr.erase({s[x],t[x],x});
		cout<<(lstans=tr.rt->ans-1)<<"\n",lstans%=p;
	}
	return 0;
}
原文