C. Save More Mice | Codeforces Round #748 (Div. 3)

C. Save More Mice
time limit per test
4 seconds
memory limit per test
256 megabytes
standard input
standard output

There are one cat, k mice, and one hole on a coordinate line. The cat is located at the point 0, the hole is located at the point n. All mice are located between the cat and the hole: the i-th mouse is located at the point xi (0<xi<n). At each point, many mice can be located.

In one second, the following happens. First, exactly one mouse moves to the right by 1. If the mouse reaches the hole, it hides (i.e. the mouse will not any more move to any point and will not be eaten by the cat). Then (after that the mouse has finished its move) the cat moves to the right by 1. If at the new cat's position, some mice are located, the cat eats them (they will not be able to move after that). The actions are performed until any mouse hasn't been hidden or isn't eaten.

In other words, the first move is made by a mouse. If the mouse has reached the hole, it's saved. Then the cat makes a move. The cat eats the mice located at the pointed the cat has reached (if the cat has reached the hole, it eats nobody).

Each second, you can select a mouse that will make a move. What is the maximum number of mice that can reach the hole without being eaten?


The first line contains one integer t (1t104) — the number of test cases. Then t test cases follow.

Each test case consists of two lines. The first line contains two integers n and k (2n1091k4105). The second line contains k integers x1,x2,xk (1xi<n) — the initial coordinates of the mice.

It is guaranteed that the sum of all k given in the input doesn't exceed 4105.


For each test case output on a separate line an integer m (m0) — the maximum number of mice that can reach the hole without being eaten.

10 6
8 7 5 4 9 4
2 8
1 1 1 1 1 1 1 1
12 11
1 2 3 4 5 6 7 8 9 10 11

The solution to the above problem is 

#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int aint b)
    int x = 1;
    while (b)
        if (b & 1) x *= a;
        a *= a;
        b >>= 1;
    return x;

template <typename Arg1>
void __f (const char* nameArg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1typename... Args>
void __f (const char* namesArg1&& arg1Args&&... args)
    const char* comma = strchr (names + 1',');
    cout.write (names, comma - names) << " : " << arg1 << " | "__f (comma + 1, args...);

const int N = 200005;

void solve() {

    int n,k;
    vi v(k);

    for(int i=0; i<k; i++){
    int ans=0,l=0,r=k-1;
    int curr=0,time=0;
        while(l<=r && v[l]<=time){



int32_t main()
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    freopen("input.txt",  "r",  stdin);
    freopen("output.txt""w", stdout);

    clock_t z = clock();

    int t = 1;
    cin >> t;
    while (t--) solve();

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);

    return 0;

Post a Comment