「杂题乱刷」CF1650D

题目链接

CF1650D (luogu)

CF1650D (codeforces)

解题思路

我们发现要想让第 \(i\) 个数变换一次就需要给第 \(i \sim n\) 中的一个位置做一次操作,因此我们很自然的就想到了倒推,容易证明这样是不劣的。

时间复杂度 \(O(n^2)\)

参考代码

#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
/*
就是说,我们倒推回去

发现一定是最优的。

结束。

O(n^2) 
*/
ll n,a[100010];
void f(ll x)//前x位向左循环一次
{
	forl(i,2,x)
		swap(a[i],a[i-1]); 
}
ll ans[100010];
void clear(ll x){
	forl(i,0,x+1)
		ans[i]=0;
}
void solve()
{
	cin>>n;
	clear(n);
	forl(i,1,n)
		cin>>a[i];
	forr(i,n,1)
		while(a[i]!=i)
			f(i),ans[i]++;
	forl(i,1,n)
		cout<<ans[i]<<' ';
	cout<<endl;
}
int main()
{
	IOS;
	t=1;
	cin>>t;
	while(t--)
		solve();
	QwQ;
}
posted @ 2024-05-23 23:04  wangmarui  阅读(2)  评论(0编辑  收藏  举报