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(前缀和)
随机推荐
- maven基本知识的7个提问
在如今的互联网项目开发当中,特别是Java领域,Maven的仓库管理.依赖管理.继承和聚合等特性为项目的构建提供了一整套完善的解决方案. 这里我们通过7个关于Maven的提问来了解Maven的一些基本 ...
- Feign切换client到okhttp无法生效天坑!(附带发生的原因)
提示:如果只看如何解决问题,请看文章的末尾如何解决这个问题 1. 场景描述 最近项目中使用了feign当做http请求工具来使用.相对于httpclient.resttemplate来说,fegin用 ...
- 02、策略模式(Strategy)
一.概念: 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离, 手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法, 实现方法要根据具体的环境因素而变化. 二. ...
- 前端学习:学习笔记(JS部分)
前端学习:学习笔记(JS部分) 前端学习:JS学习总结(图解) JS的简介 JS基本语法 JS内置对象 JS的函数 JS的事件 JS的BOM JS的DOM JS的简介 新建步骤 <body ...
- 关于toLocaleString(), toString(), valueOf()方法的使用
所有对象都是具有toLocalString(), toString(), valueOf()三种方法的,此篇博客主要是讲述其在Array引用类型上的使用. 基本使用 调用valueOf()返回的是数组 ...
- ASP.NET MVC 过滤、异常过滤器
记录下过滤器的学习—_— APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前后如果想再加一些额外的逻辑处理,这样会造成大量代码的重复使用,这不是我们希 ...
- SpringBoot静态资源文件
1.默认静态资源映射 Spring Boot对静态资源映射提供了默认配置 Spring Boot默认将 /** 所有访问映射到一下目录 classpath:/static classpath:/pub ...
- QQuickPaintedItem鼠标精准拾取(pick/select)研究
QT C++在2D图形方面已经做的很完善了,在PC端(Windows.Linux和MaC)上都有很好的表现. QT中的QML特别适合于移动端应用的开发,QML中的一些基本形状类型并不是一一地与Qt C ...
- 英语chiltonite葡萄石chiltonite单词
葡萄石Chiltonite 1.葡萄石能够促进血液循环,具有美容养颜的功效,非常适合女性佩戴,可以增加个人魅力,还能加强事业财运,凝聚财富气场. 2.绿色光对应人体心轮,对心脏,肺脏有效用,内涵的磁石 ...
- elasticsearch组合多条件查询实现restful api以及java代码实现
原文:http://blog.java1234.com/blog/articles/372.html elasticsearch组合多条件查询实现restful api以及java代码实现 实际开发中 ...