P9562 [SDCPC2023] G-Matching 题解
P9562 [SDCPC2023] G-Matching 题解
签到题,是这场比赛第三简单的题。
解法
题中说对于
对于这个式子,我们可以变形为
我们考虑在输入
代码
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO
{
/*
快读快写,可忽略。
*/
};
using namespace fast_IO;
#define int long long
int t,n;
int ans;
struct node
{
int pos,val;
};
node a[100010];
inline bool cmp(const node &x,const node &y)
{
if(x.val==y.val) return x.pos>y.pos;
return x.val>y.val;
}
signed main()
{
read(t);
while(t--)
{
read(n),ans=0;
for(int i=1;i<=n;i++) read(a[i].val),a[i].val-=i,a[i].pos=i;
sort(a+1,a+n+1,cmp);
for(int i=2,cnt=1;i<=n;i++)
{
if(a[i].val!=a[i-1].val)
{
cnt=1;
continue;
}
if(cnt&1) ans+=max(a[i].val+a[i-1].val+a[i].pos+a[i-1].pos,0ll);
cnt++;
}
write(ans),Putchar('\n');
}
fwrite(Ouf,1,p3-Ouf,stdout),fflush(stdout);
return 0;
}