SUBTASK - Editorial

PROBLEM LINK:

Practice
Div1
Div2
Div3

Setter: Soumyadeep Pal
Tester: Samarth Gupta
Editorialist: Ajit Sharma Kasturi

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

We are given an array A of length N describing the status of chef’s solution for some problem where A_i =1 denotes that the i^{th} testcase has passed and A_i = 0 denotes that the i^{th} testcase has failed. If the chef’s code passes all testcases, he will get 100 points, or else if the first M \lt N testcases have passed, he will get K \lt 100 points. If both these conditions fail, he will get 0 points. We need to find the score of chef for that particular problem.

EXPLANATION:

  • We can solve the problem simply by simulation of the conditions in the problem statement.

  • Let us iterate over i from 1 to N and check the A_i values.

  • If A_i = 1 for all 1 \leq i \leq N, the score is 100.

  • If A_i = 1 for all 1 \leq i \leq M but A_i =0 for some i \geq M, the score is K.

  • If A_i =0 for some i \leq M, the score is 0.

TIME COMPLEXITY:

O(N) for each testcase.

SOLUTION:

Editorialist's solution

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

int main()
{
     int tests;
     cin >> tests;
     while (tests--)
     {
          int n, m, k;
          cin >> n >> m >> k;
          vector<int> a(n);

          for (int i = 0; i < n; i++)
               cin >> a[i];

          bool first_m_passed = true;
          bool all_passed = true;

          for (int i = 0; i < n; i++)
          {
               if (i < m && a[i] == 0)
               {
                    all_passed = first_m_passed = false;
               }
               else if (a[i] == 0)
               {
                    all_passed = false;
               }
          }

          if (all_passed)
               cout << 100 << endl;
          else if (first_m_passed)
               cout << k << endl;
          else
               cout << 0 << endl;
     }
}

Setter's solution

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

void solve() {
  int n, p, x; cin >> n >> p >> x;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0) break;
    if (i == p - 1) ans = x;
    if (i == n - 1) ans = 100;
  }
  cout << ans << '\n';
}

signed main() {
  ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) solve();
  return 0;
}

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

long long readInt(long long l, long long r, char endd) {
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true) {
        char g=getchar();
        if(g=='-') {
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g&&g<='9') {
            x*=10;
            x+=g-'0';
            if(cnt==0) {
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);
 
            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd) {
            if(is_neg) {
                x=-x;
            }
            assert(l<=x&&x<=r);
            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l, int r, char endd) {
    string ret="";
    int cnt=0;
    while(true) {
        char g=getchar();
        assert(g!=-1);
        if(g==endd) {
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt&&cnt<=r);
    return ret;
}
long long readIntSp(long long l, long long r) {
    return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
    return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
    return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
    return readString(l,r,' ');
}
 
void readEOF(){
    assert(getchar()==EOF);
}

int main() {
	// your code goes here
	int t = readIntLn(1, 100);
	int sum = 0;
	while(t--){
	    int n = readIntSp(2, 100);
	    int m = readIntSp(1, n - 1);
	    int k = readIntLn(1, 99);
	    vector<int> vec(n);
	    for(int i = 0 ;i < n ; i++){
	        if(i == n - 1)
	            vec[i] = readIntLn(0, 1);
	        else
	            vec[i] = readIntSp(0, 1);
	    }
	    int cnt = 0;
	    for(int i = 0; i < n ; i++)
	        cnt += vec[i];
	    if(cnt == n)
	        cout << "100\n";
	    else{
	        int i;
	        for(i = 0; i < n ; i++)
	            if(vec[i] == 0)
	                break;
	        if(i < m)
	            cout << "0\n";
	        else
	            cout << k << '\n';
	    }
	}
    readEOF();
	return 0;
}


Please comment below if you have any questions, alternate solutions, or suggestions. :slight_smile:

what’s wrong with this solution?

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define mp make_pair

void solve(){
    ll n,m,k;
    cin>>n>>m>>k;
    int arr[n];
    ll index = n;
    for (ll i = 0; i < n; i++){
        cin>>arr[i];        
    }
    for (auto i = 0; i < n; i++){
        if (arr[i] == 0){
            index = i;
            break;
        }
    }
    //cout<<index<<" ";
    if (index == n){
        //cout<<"here1 ";
        cout<<100;
    }
    else if (index == m){
        //cout<<"here2 ";
        cout<<k;
    }
    else {
        cout<<0;
    }
}

int main(){
    int t;
    cin>>t;
    while (t--){
        solve();
        cout<<endl;
    }

Consider the test input:

1                    
4 2 10
1 1 1 0

Can anyone share or explain this subtask problem in python?