Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)

时间:2025-04-04 08:36:20

题:https://codeforces.com/contest/1278/problem/B

思路:还是把1~n分配给俩个数,让他们最终相等

   假设刚开始两个数字相等,然后一个数字向前走了abs(b-a)步,由等差数列求和公式,这时候我们贪心的让另外一个数字走大于等于abs(b - a)的最小步数,然后如果两数相等必须满足走的步数之和%2=0

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int M=1e6+;
ll a[],countt[M];
void solve()
{
ll a,b;
cin>>a>>b;
ll c=max(a,b)-min(a,b);
int i;
for(i=;;i++)
if(c<=(i*(i + ))/)
break;
ll m =(i*(i + ))/;
while((m+c)% )
i++,m=(i*(i + ))/;
cout<<i<<endl;
return;
}
int main()
{
int t;cin>>t;
while(t--)
solve();
}