1278B - A and B Educational Codeforces Round 78 (Rated for Div. 2)

本文介绍了一个算法问题的解决方法,即通过一系列规定的操作(每轮递增),找到使两个初始不等的整数相等所需的最小操作轮数。文章提供了完整的代码实现,并解释了核心思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题意:

给你两个数A,B,,你可以一系列操作,这一系列操作是 第一个回合,你必须让A或者B 加上 1 , 第二回合你必须让A或者B加上2, 第三回合你必须要让A或者B加上3…你可以进行任意回合的操作,但是你需要找到最小回合,使得A和B相同。

思路:

可以先预处理从1到10000的前缀和(因为数据范围就知道了1e9,可以保证一定大于等于1e9)

首先如果a,b相等那么直接输出0就好,如果不相等,从第一个开始遍历,如果满足条件:加起来的和为偶数(因为要均分为两个)并且当前的前缀和值大于等于两个数相差的值就直接输出即可。

总共的时间最多也就1e5*1e2=1e7;

代码:

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
ll s[N];

void init()
{
	int res=0;
	for(int i =1;i<=100000;i++)
	{
		res+=i;
		s[i]=res;
	}
}
//1 2 3 4 5 6
//1 3 6 10 15 21    10 21    24 
int main()
{
	init();
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		if(n==m)cout<<0<<endl;
		else{
			int i=1;
			while((s[i]+n+m)%2!=0||s[i]<(abs(n-m)))i++;
			cout<<i<<endl;
		}
		
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值