MASKPOL - Editorial

PROBLEM LINK:

Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1

Author: Nandeesh Gupta
Tester : Takuki Kurokawa
Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

There are N people living in the city out of which A people are infected. People get infected when they came into contact with an infected person and none of them are wearing the mask.

Your goal is to calculate the minimum number of masks needed to prevent any person from getting infected.

EXPLANATION:

There are two ways to stop the spread of further infection:

  • All the infected people wear a mask.
  • All the non-infected people wear a mask.

Since our goal is to minimize the number of masks. Hence the answer will be the minimum of the above two cases.

TIME COMPLEXITY:

O(1) per test case

SOLUTIONS:

Author's Solution
#include <iostream>
using namespace std;

int main() {
    int t; cin>>t;
    while(t--){
        int n,a; cin>>n>>a;
        cout<<min(a,n-a)<<'\n';
    }
	return 0;
}

Tester's Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
    int tt;
    cin >> tt;
    while (tt--) {
        int n, a;
        cin >> n >> a;
        cout << min(a, n - a) << '\n';
    }
    return 0;
}

Editorialist Solution
#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n,a;
    cin>>n>>a;

    cout<<min(n-a,a)<<"\n";
}

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int t;
  cin>>t;

  while(t--)
    solve();

return 0;
}

1 Like

Could anyone help me understand why I am getting a TLE on this solution CodeChef: Practical coding for everyone. It is just doing a Math.min which I believe is O(1)