2021 ICPC澳门站 F Sandpile on Clique 题解

188 阅读1分钟

题目链接

思路

可以转化成给一个数组,每轮在其中找一个大于n1n-1的元素拆掉,给其它位置加上被拆的元素给的值。

如果拆了n+1n+1轮,那么必定存在元素至少加了nn,这样就可以继续拆,就拆不完了。所以最多搞n+1n+1轮如果还能拆就Recurrent。

暴力拆是O(N2)O(N^2)的,维护一个sumsum表示已经拆过的点能给当前值加多少,每次从优先队列取出最大值,加上sumsum后拆掉并维护sumsum,拆完再减去sumsum扔回去,复杂度就是O(NlogN)O(N\log N)

代码

#include<bits/stdc++.h>
#define rep(i,st,ed) for(int i=st;i<=ed;++i)
#define bl(u,i) for(int i=head[u];i;i=e[i].nxt)
#define en puts("")
#define LLM LONG_LONG_MAX
#define LLm LONG_LONG_MIN
#define pii pair<ll,ll> 
typedef long long ll;
typedef double db;
using namespace std;
const ll INF=0x3f3f3f3f;
void read() {}
void OP() {}
void op() {}
template <typename T, typename... T2>
inline void read(T &_, T2 &... oth)
{
    int __=0;
    _=0;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            __=1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        _=_*10+ch-48;
        ch=getchar();
    }
    _=__?-_:_;
    read(oth...);
}
template <typename T>
void Out(T _)
{
    if(_<0)
    {
        putchar('-');
        _=-_;
    }
    if(_>=10)
       Out(_/10);
    putchar(_%10+'0');
}
template <typename T, typename... T2>
inline void OP(T _, T2... oth)
{
	Out(_);
	putchar('\n');
	OP(oth...);
}
template <typename T, typename... T2>
inline void op(T _, T2... oth)
{
	Out(_);
	putchar(' ');
	op(oth...);
}
/*#################################*/
const ll N=5E5+10;
ll n,sum,x;
priority_queue<pii> q;
vector<pii> ve;
int main()
{
	read(n);
	rep(i,1,n)
	{
		read(x);
		q.push(pii(x,i));
	}
	ll cnt=0;
	while(!q.empty())
	{
		if(q.top().first+sum<n-1)
			break;
		++cnt;
		pii cur=q.top();
		q.pop();
		x=cur.first+sum;
		sum+=x/(n-1);
		x%=n-1;
		q.push(pii(x-sum,cur.second));
		if(cnt>=n+1)
			break;
	}
	while(!q.empty())
	{
		pii cur=q.top();
		q.pop();
		cur.first+=sum;
		if(cur.first>=n-1)
		{
			puts("Recurrent");
			return 0;
		}
		ve.emplace_back(cur.second,cur.first);
	}
	sort(ve.begin(),ve.end());
	rep(i,0,ve.size()-1)
	{
		printf("%lld",ve[i].second);
		if(i!=ve.size()-1)
			printf(" ");
	}
}