Friday, August 12, 2022

Codeforces 1593C - Save More Mice [ EXPLANATION + TUTORIAL + SOLUTION ]

 

1593C - Save More Mice


Tutorial


Let's solve the problem using a linear search. Let m be the number of mice we are trying to save. Then it is more efficient to save m mice such that they are the closest ones to the hole. Let ri be the distance from the i-th mouse to the hole (ri=n−xi). Denote R:=∑i=1mri. Let's prove that these mice will be saved if and only if R<n.
The necessary condition. Suppose we can save the mice and R≥n. Since only one mouse can be moved in one second, the following will happen: m−1 of mice will already be saved and one mouse will have to be saved. When it's been q seconds, then the distance from the cat to the hole will be equal to n−q, and the distance from the mouse to the hole will be equal to R−q (since all other mice are already in the hole, their distances to the hole are equal to 0, so the sum of the distances from all mice to the hole at the current time is exactly equal to the distance to the hole from one remaining mouse). Since R−q≥n−q, the distance from the mouse to the hole is greater than or equal to the distance from the cat to the hole. But this cannot be, because both the mice and the cat move only to the right, and all mice met by the cat are eaten. So, R<n.
Sufficient condition. Suppose R<n. If R=0, then all the mice are already in the hole, i.e. they are saved. Suppose R>0. Let's move any mouse, then the cat. Suppose the cat ate at least one of the mice. This mouse is definitely not the one that was moved. Then the distance from it to the eaten mouse was equal to 1, i.e. the distance from it to the hole was equal to the distance from the eaten mouse to the hole plus 1. The distance from the moved mouse to the hole was at least 1. So, R≥rs+rm, where rs=n−1 is the distance from the eaten mouse to the hole, rm≥1 is the distance from the moved mouse to the hole. So, R≥n−1+1=n, but it's false. Therefore, none of the mice will be eaten on the first move. Then the distance from the cat to the hole will be equal to n−1, the total distance from the mice to the hole will be equal to R−1. R−1<n−1, i.e. now we have to solve a similar problem for smaller R and n. So R will be gradually decreased to 0, while no mouse will be eaten. So, if R<n, all the mice will be saved.
Thus, to solve the problem, we need to find the maximum m such that the sum of the distances from the m nearest mice to the hole is less than n.

Explanation 



Solution

    
    //Codeforces 1593C - Save More Mice
    #include<bits/stdc++.h>
    using namespace std;

    int main()
    {  
    int t, n, k;
    cin>>t;
    while(t--)
    {
        cin >> n >> k;

        vector<int> v(k);
        for (int i = 0; i < k; i++)
            cin >> v[i];
        sort(v.rbegin(), v.rend());

        int count=0;
        long long sum=0;

        while (count < v.size() && sum + n - v[count] < n)
        {
            sum += n - v[count++];
        }

        cout<<count<<endl;
       
     }

        return 0;
    }

No comments:

Post a Comment