Skip to content

Commit a3ffe60

Browse files
author
Rodolfo Quispe
committed
Added chusky and hunger problem
1 parent c540474 commit a3ffe60

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

chusky_hunger/problem_statement.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Chusky and Hunger
2+
=========================
3+
4+
Chusky lives in Dogland and as everyone in this city, he loves the local dish: Aguadito. There is a unique Polleria (a really nice place to eat Aguadito) in Dogland, hence, this eatery is really selective with its customers. To eat in the Polleria, Chusky must solve the next task: Given D boxes, each one has all integer numbers between L_i and R_i (1 <= i <= D) inclusive, indicate the K-th lowest unique existing element. Because there is big queue waiting after Chusky, he has just some seconds to give the correct answer, otherwise he will not be able to eat in the Polleria and will eat some boiled potatoes that has at home (he does not like it too much) .
5+
Help Chusky to eat an Aguadito.
6+
7+
Input
8+
=====
9+
10+
The first line of the input contains a number T (1 <= T <= 100) indicating the number of test cases. Each test case follows the next format:
11+
The first line of each test case contains a number D (1 <= D <= 100) representing the number of boxes Chusky receives, the second line contains a number K (1 <= K <= 10 ^ 15) representing the K- th number Chusky must guess, then D lines follows, each one in has two integers L_i and R_i (1 <= L_i <= R_i <= 10 ^ 15) representing that box i has all integers between L_i and R_i (1 <= i <= D) inclusive.
12+
13+
Output
14+
=====
15+
16+
For each test case, print the K-th lowest number between all boxes. Print -1 if no K-th lowest number exists.
17+
18+
Example
19+
======
20+
21+
Input
22+
23+
2
24+
3
25+
2
26+
3 6
27+
3 4
28+
11 13
29+
1
30+
7
31+
1 5
32+
33+
Output
34+
35+
4
36+
-1
37+
38+
Explanation:
39+
40+
For the first test case, the first box has numbers {3, 4, 5, 6}, the second one has {3, 4} and the last one has {11, 13}, then all existing numbers are {3, 4 , 5, 6, 11, 13} and the 2nd lowest number is 4. In the second case the box has numbers {1, 2, 3, 4, 5}, thus there is no 7th lowest element.
41+

chusky_hunger/solution.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
//#define DEBUG
3+
using namespace std;
4+
typedef vector < int > vi;
5+
typedef pair < long long int, long long int > pii;
6+
typedef vector < pii > vii;
7+
typedef long long int i64;
8+
int d;
9+
i64 k;
10+
bool byPairs(pii left, pii right)
11+
{
12+
if(left.first != right.first)
13+
return left.first < right.first;
14+
return left.second < right.second;
15+
}
16+
i64 sol(vii &nums)
17+
{
18+
sort(nums.begin(), nums.end(), byPairs);
19+
20+
i64 ans = -1ll;
21+
i64 last = -1ll;
22+
int i = 0;
23+
while(k>0ll && i < (int)nums.size())
24+
{
25+
if(last < nums[i].first) // no overlap
26+
{
27+
i64 len = nums[i].second - nums[i].first + 1ll;
28+
#ifdef DEBUG
29+
30+
cout<<nums[i].first<<" --- "<<nums[i].second<<" : "<<len<< " > " <<k<<endl;
31+
#endif
32+
33+
if(len < k)
34+
k-=len, last = nums[i].second, i++;
35+
else
36+
{
37+
ans = nums[i].first + k - 1ll, k = 0ll;
38+
39+
}
40+
}
41+
else // overlap
42+
{
43+
if(nums[i].second <= last) i++;
44+
else nums[i].first = last + 1;
45+
}
46+
}
47+
return ans;
48+
}
49+
int main()
50+
{
51+
#ifdef DEBUG
52+
clock_t begin = clock();
53+
#endif
54+
int test_cases;
55+
scanf("%d", &test_cases);
56+
while(test_cases--)
57+
{
58+
cin>>d>>k;
59+
vii nums = vii();
60+
for(int i = 0; i < d; i++)
61+
{
62+
pii x;
63+
cin>>x.first>>x.second;
64+
nums.push_back(x);
65+
}
66+
cout<< sol(nums) <<endl;
67+
}
68+
#ifdef DEBUG
69+
clock_t end = clock();
70+
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
71+
printf("%.10f\n", elapsed_secs);
72+
#endif
73+
}
74+
75+

0 commit comments

Comments
 (0)