Problem K: Frames
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 45 Solved: 17
[Submit][Status][Web Board]
Description
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well.
This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good.
Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, m icons can fit in a horizontal row in any window.
Igor K.'s computer contains n folders in the D: disk's root catalog. The folders are numbered from 1 to n in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from a to b inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from a to b and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders.
Input
The only line contains four integers n, m, a, b (1≤n,m≤109, 1≤a≤b≤n). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.
Output
Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from a to b.
Examples
Input
11 4 3 9
Output
3
Input
20 5 2 20
Output
2
Note
The images below illustrate statement tests.
The first test:
In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection.
The second test:
In this test we can first select all folders in the first row (2, 3, 4, 5), then − all other ones.
题目大意:
给n个文件夹,按照每行m个排列,然后从左往右编号1—n,输入起点和终点,要求只能用矩形框选中文件夹的方式把全部文件选中要几次,要注意重复选择会取消选择。
想法:
试几次后可以发现最多也只要三次,所以答案只有三种情况:1,2,3.但是要得出这三种答案的情况有点复杂,具体有以下几种:
1.m=1时显然次数最小值为1.
2.b=n时特判:如果a在第一列或者a,b同行,次数为1,否则次数为2
3.所有a在第1列,b在最后一列的情况下或a,b同行,最小次数都是1.
4.a在第一列或b在最后一列或者a,b的列相邻(对角的两个矩形)或者a,b所在行相邻则次数为2
5.剩下的次数都是3.
AC代码:
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn =1000+5;
int main()
{
long long n,m,a,b;
cin>>n>>m>>a>>b;
int st=a/m+(a%m!=0);
int ed=b/m+(b%m!=0);
if(m==1){
cout<<1;
return 0;
}
if(b==n){
if(a%m==1||st==ed)
cout<<1;
else
cout<<2;
return 0;
}
if((a%m==1&&b%m==0)||st==ed)
{cout<<1;return 0;}
if(a%m==1||b%m==0||st+1==ed||(b+1)%m==a%m)
cout<<2;
else
cout<<3;
return 0;
}