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

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

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

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

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

 

你可能感兴趣的:(Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配))