parth_2003's blog

By parth_2003, history, 4 weeks ago, In English

So I was solving Question Chess Tournament 1569B rated 1000. when I submit the code then the output on the given test cases are showing wrong, Although I checked them on my compiler, and the code was working fine. Here is the code,

include<bits/stdc++.h>

using namespace std;

int main(){ int t; cin >> t; while(t--){ int n; cin >> n; vector v(n); int countO=0; int countT=0; for(int i=0; i<n; i++){ cin >> v[i]; if(v[i] == 1){ countO++; } else{ countT++; } } if(countT == 2 || countT == 1){ cout << "NO" << endl; continue; }

vector<vector<char> > f(n, vector<char>(n, '='));
for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
       if(i == j){
         f[i][j] = 'X';
       }
       else if(v[i] == 1 && v[j] == 2){
         f[i][j] = '+';
       }
       else if(v[i] == 1 && v[j] == 1){
         f[i][j] = '=';
       }
    }   
}

vector<int> type2;
for(int i=0; i<n; i++){
    if(v[i] == 2){
       type2.push_back(i);
    }
}
for(int i=0; i<type2.size(); i++){
    int curr = type2[i];
    int next = type2[i+1 % (type2.size())];
    f[curr][next] = '+';
    f[next][curr] = '-';
}

cout << "YES" << endl;  
for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
       cout << f[i][j];  
    }
    cout << endl;
}

} }

  • Vote: I like it
  • -19
  • Vote: I do not like it

»
4 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You have to fix this line of code:

int next = type2[i+1 % (type2.size())];

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you please tell the reason for this? is it because type2.size() is size_t datatype?

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      % operator has higher priority than +, so it's actually translated as type2[i + (1 % (type2.size())], which is unintended and gets out of the vector's range when i + 1 == type2.size().

»
4 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

deleted

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

265461929 Can someone explain why it says expected: '1005355647', found: '1005355648' even though when I run the same testcase on my compiler it gives 1005355647 as output. Pretty confused TBH.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

No.