Educational Codeforces Round 78 (Rated for Div. 2) B. A and B
链接:
https://codeforces.com/contest/1278/problem/B
题意:
You are given two integers a and b. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 1; during the second operation you choose one of these numbers and increase it by 2, and so on. You choose the number of these operations yourself.
For example, if a=1 and b=3, you can perform the following sequence of three operations:
add 1 to a, then a=2 and b=3;
add 2 to b, then a=2 and b=5;
add 3 to a, then a=5 and b=5.
Calculate the minimum number of operations required to make a and b equal
思路:
先转换成,前n个数,分成两堆,差为a和b的差,
打一个前缀和,猜了一下,分成的两堆差肯定为a和b的差,考虑不断给两边加上1,如果总和是前n项和,答案就是n。
卡了一个小时。。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL sum[1000010];
int main()
{
int t;
cin >> t;
sum[0] = 0, sum[1] = 1;
for (int i = 1;i <= 1000000;i++)
sum[i] = sum[i-1]+i;
while(t--)
{
int a, b;
cin >> a >> b;
if (a == b)
{
cout << 0 << endl;
continue;
}
int sub = abs(a-b);
int res = 0;
for (int i = 1;i <= 1000000;i++)
{
if (sum[i] < sub)
continue;
if ((sum[i]-sub)%2 == 0)
{
res = i;
break;
}
}
cout << res << endl;
}
return 0;
}
Educational Codeforces Round 78 (Rated for Div. 2) B. A and B的更多相关文章
- Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree
链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...
- Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...
- Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing
链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...
- 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)
比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...
- 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- ...
- Educational Codeforces Round 78 (Rated for Div. 2)
A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...
- Educational Codeforces Round 78 (Rated for Div. 2) --补题
链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = ...
- Educational Codeforces Round 78 (Rated for Div. 2) 题解
Shuffle Hashing A and B Berry Jam Segment Tree Tests for problem D Cards Shuffle Hashing \[ Time Lim ...
- Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam(前缀和)
随机推荐
- $scope里的$watch方法
一.$watch的作用 监听model,如果model发生变化,则触发某些事情. 二.$watch的格式 $scope. $watch(watchFn,watchAction,deepWatch); ...
- 转《深入理解 Java 内存模型》读书笔记
转:https://mp.weixin.qq.com/s/2hA6u4hLEPWlTPdD-XB-bg 前提 <深入理解 Java 内存模型>程晓明著,该书在以前看过一遍,现在学的东西越多 ...
- Thinkpad S430 3364-A59 (笔记本型号)加内存条过程
背景:13年初选购的本,目前使用发现卡得厉害,原装内存只有4G,遂寻思扩充内存. [1]准备工具 待准备工具明细: (1)螺丝刀 主要为了卸后盖的螺丝. (2)内存条 待增加的内存条,如下图: 说明一 ...
- Java学习:注解简介
JAVA 注解的基本原理 以前,『XML』是各大框架的青睐者,它以松耦合的方式完成了框架中几乎所有的配置,但是随着项目越来越庞大,『XML』的内容也越来越复杂,维护成本变高. 于是就有人提出来一种标记 ...
- 《 .NET并发编程实战》实战习题集 - 4 - 如何重用一次性资源
如何重用以下一次性资源代码呢? string text; using (var stream = new StreamReader(path)) { text = stream.ReadToEnd() ...
- 《 .NET并发编程实战》实战习题集 - 1 - 隔离副作用
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- CDR镂空字踩坑记录
做个成品,看似没毛病 坑1 但是对整体上个色就会发现,白框部分一片漆黑(字黑色,框子黑色) 然后根据大牛的作品染色后没毛病推出 ==> 字体要做镂空字 坑1解决 先把框内元素全部选择(字.矢量图 ...
- asp.net SQLite关于各版本的调试
最近想做一个简版的管理系统,将SQL SERVER数据库切换到SQLite数据库中,采用的是SQLite3的版本数据库. 开发工具:SV2015 UP3 数据库:SQLite3 项目整体结构图 相同的 ...
- 记录一次OracleJDK开发的项目发部到Linux中使用OpenJDK启动后失败的错误的解决方案
一.现象 基于JAVA SpringBoot2.0.4的项目,发部后项目发部后,放到OpenJDK环境中运行时,提示下列错误: 2019-10-22 10:03:55 [main] WARN o.s ...
- python每日经典算法题5(基础题)+1(较难题)
一:基础算法题5道 1.阿姆斯特朗数 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数.判断用户输入的数字是否为阿姆斯特朗数. (1)题目分析:这里要先得到该数是多少位的,然后再把 ...