codeforces-760【B二分】

来源:互联网 发布:vm文件夹 mac 编辑:程序博客网 时间:2024/04/25 06:47

题目链接:点击打开链接

A. Petr and a calendar
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture:

Petr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap.

Input

The only line contain two integers m and d (1 ≤ m ≤ 121 ≤ d ≤ 7) — the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday).

Output

Print single integer: the number of columns the table should have.

Examples
input
1 7
output
6
input
1 1
output
5
input
11 6
output
5
Note

The first example corresponds to the January 2017 shown on the picture in the statements.

In the second example 1-st January is Monday, so the whole month fits into 5 columns.

In the third example 1-st November is Saturday and 5 columns is enough.


大意:给出你一个月份和这个月的第一天是星期几;问这个日历有多少列


思路:注意一下题中给出了限制条件,给出的年份不是闰年,刚开始没注意 WA 一发

#include<iostream>#include<algorithm>#include<cstring>using namespace std;int m,d;bool judge[]={0,1,0,1,0,1,0,1,1,0,1,0,1};int main(){while(~scanf("%d%d",&m,&d)){if(m==2){if(d==1)puts("4");elseputs("5");}else if(judge[m]){if(d<=5)puts("5");elseputs("6");}else if(!judge[m]){if(d<=6)puts("5");elseputs("6");}}return 0;}

题目链接:点击打开链接

B. Frodo and pillows
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have.

Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?

Input

The only line contain three integers nm and k (1 ≤ n ≤ m ≤ 1091 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.

Output

Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.

Examples
input
4 6 2
output
2
input
3 10 3
output
4
input
3 6 1
output
3
Note

In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.

In the second example Frodo can take at most four pillows, giving three pillows to each of the others.

In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.


大意:有 n 张床排成一行,m 个枕头, Frodo 在第 k 个床位睡觉,问他最多能得到的枕头个数。

两个限制条件:1. 每个人至少要有一个枕头;2. 相邻两个人的枕头个数相差不能大于 1。


思路:数据这么大,二分啊。判断是否合法

#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#define LL long longusing namespace std;LL n,m,k;bool judge(LL x){LL a=min(x,k); // 等差数列长度 LL b=min(x,n-k+1); // 等差数列长度 LL sum=a*(x+x-(a-1))/2+b*(x+x-(b-1))/2-x; // 两个公差为 1 的等差数列公式 return sum<=m;}int main(){while(~scanf("%lld%lld%lld",&n,&m,&k)){m=m-n; // 保证每个人至少有一个枕头 LL mid,l=1,r=m,ans=-1;while(l<=r){mid=l+r>>1;if(judge(mid)){ans=mid;l=mid+1;}elser=mid-1;}if(ans==-1)puts("1");elseprintf("%lld\n",ans+1);}return 0;}





0 0