Skip to content

Add cat to Binary search #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Binary Search/Cat/Cat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
This problem from TOI13 (Thailand Olympaid of Informatics 2017).
I love this problem because I love Cats.

Source: https://www.proprog.ml/tasks/toi13_cat/descs/12575

Time Limit 1000 ms
Memory Limit 512 MB

N cats stay in line. N is always even because we want to pair the cats which have the same height in
the consecutive position(Each value of height can have only 1 pair). But the cats stay in a random position.
It is your task to find the size of the pocket to bring the cats and move it to the right position.

For Example:
N = 6 (6 cats)
Cat height = 3 3 2 5 5 2
We can see that the cats that height equals to 3 and 5 already each with the couple but the cats that
height equals to 2 didn't sit near to each other.
So, we will use the pocket that has the size of 2 to bring the two-unit-height cat to sit next to each other.
It'll become 3 3 2 2 5 5 or 3 3 5 5 2 2.

Input:
1st line contains amount of N while 1<=N<=2x10^6 or 2000000
Next N lines is hi stands for the height of cat number i while 1<=i<=N and 1<=hi<=2^31
Output:
The smallest size of pocket that can rearrange all cat to the right position.

Example input1:
6
3
3
2
5
5
2
Example output1:
2
(Like an example)

Example input2:
6
3
5
2
2
5
3
Example output2:
3
(Use pocket size 3 to bring 2 and 3 cats sit next to each other it'll become 5 5 3 3 2 2 for example)

Example input3:
6
2
2
3
3
5
5
Example output3:
0
(Doesn't need to use the pocket)


84 changes: 84 additions & 0 deletions Binary Search/Cat/Sol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include<bits/stdc++.h>
using namespace std;
vector<long long int> v;
long long int l=0,r=0,mid,big;
int unpair;
long long int data[2000009];
int main()
{
int n;
scanf(" %d",&n);
long long int a;
for(int i=0;i<n;i++)
{
scanf(" %lld",&a);
v.push_back(a);
l=min(l,a);
r=max(r,a);
data[i]=a;
if(i%2==1)
{
//if the cat that stay in odd array has not the same value of the left cat then it is not in pair.
if(unpair==0)
{
if(a!=data[i-1])
{
unpair=1;
}
}
}
}
//In the case that every cat stay in pair.
if(unpair==0)
{
printf("0");
return 0;
}
sort(data,data+n);
r=n-1;
mid=(l+r)/2;
int broke=0;
//B Search for the size of pocket.
while(1)
{
if(l==r) break;
for(int i=0;i<n;i++)
{
//If the cat is bigger than the pocket...
if(data[mid]<v[i])
{
if(big==0)
{
big=v[i];
}
//And the next larger cat is not sit next to the cat we have to change the pocket size until we find the best size of pocket.
else
{
if(v[i]!=big)
{
broke=1;
break;
}
else
{
big=0;
}
}
}
}
if(broke==0)
{
r=mid;
}
else
{
l=mid+1;
}
broke=0;
mid=(l+r)/2;
big=0;
}
//Print the smallest size of pocket that can rearrange the cats.
printf("%d",data[l]);
return 0;
}