CodeForces 287B 二分贪心

Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.

A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.

The figure shows a 4-output splitter

Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.

Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.

Input

The first line contains two space-separated integers n and k (1≤n≤1018, 2≤k≤109).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Output

Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.

Sample Input
Input

4 3

Output

2

Input

5 5

Output

1

Input

8 4

Output

-1

时间: 2024-11-05 18:57:16

CodeForces 287B 二分贪心的相关文章

算法题之CodeForces 302B 二分

Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i ci times. Eugeny's play list is organized as

算法题:CodeForces 251A 二分

Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Not

uva 1335 Beijing Guards

点击打开链接uva1335 思路:二分+贪心 分析: 1 当n为偶数的时候,我们可以知道肯定会有两两相等的情况,那么这个时候ans = max(r[i]+r[i+1]),可以自己画个图验证; 2 当n为奇数的时候就不满足了,那么我们可以利用二分答案然后判断从而求出最小值.假设有p种礼物,那么设第一个人的礼物为1-r1,那么不难发现最有的分配的策略一定是这样的"编号为偶数的人尽量往前取,编号为奇数的人尽量往后取",这样我们只要判断时候第n个人和第一个人有冲突即可. 3 由于题目并没有要我

UVa 714:Copying Books,最大值最小化问题(贪心 + 二分)

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most fa

CERC 2004 / UVa 1335 Beijing Guards:二分&贪心&想法题

1335 - Beijing Guards Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=4081 Beijing was once surrounded by four rings of city walls: the Forbidden City Wa

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

CodeForces 275C 贪心

A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x<y) from the set, such that y=x·k. You're given a set of n distinct posi

codeforces B. Friends and Presents(二分+容斥)

题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除!并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数整除的个数!  那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! #include&

codeforces B. Pasha and String(贪心)

题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai, 将字符序列区间为[ai,len-ai+1]进行反转.求出经过n次反转之后的序列! /* 思路1:将区间为偶数次的直接去掉!对剩下的区间进行反转.超时了,智商上的压制... */ #include<iostream> #include<cstdio> #include<algorithm> #include<stack> #include<cstring> #include