My code is working fine on my cpu compiler but on codechef it shows wrong answers

i got his problem here is that once i enter value for test cases like here 3
the compiler treats it as the first string and prints regularly fancy for this How to fix this new problem whic i encounter when i use getline ??

Reading T won’t read the rest of the line after T, so the next call to getline will read the remaining characters after T until the end of the line.

You need to skip the rest of the line after reading T:

    int T;

    // Skip to the next line.
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

After that, you’ll need to fix the potential out-of-bounds access on the line:

        else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){

what did you mean by this i have no clue ?

Consider the testcase:


i ran with this and the
output :
regularly fancy
isn’t that whats supposed to be he output ??

since i m not much familiar with getline() funnction i wanna kno i did a slight adjustment in the code earlier and got an error message
the small change i made was
int T;
getline(cin,T); // instead of cin>>T;

but i got an error while compiling which says :
main.cpp:12:18: error: no matching function for call to ‘getline(std::istream&, int&)’

can you explain why this happened and why this dint happen when i used getline to read a string ?

i got the answer its basically because :
std::getline() function is not for integer type. and i was using it to read an integer in the variable T

1 Like

in this basically i took two sets and stored n different integers(ranging from 1 to 10^9) into each of the two multiset data structures. and since multisets store values already sorted so i used a for loop and tried to tranverse in both sets from the beginning till the end and did a small job. But it exceeds time limit of 5 seconds on online judge.

Note : i used multiset as it sorts a data in O(logn) time.
So is the problem occuring because i m dealing with very big number of the order upto 10^9 ???

my piece of code :

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

int main()
    int T;

    for(int k=0;k<T;k++)  {
    int a,n;
    int sum = 0;
   // cin>>n;   // n ranges from 1 to 10^9

    // multiset declare 
    multiset<int> s;
    for (int i =0;i<n;i++){
       // cin>>a;

    //multiset 2 declare
    multiset<int> s1;
    for (int i =0;i<n;i++){
     //   cin>>a;


    // Iterator declared to traverse 
    // set elements 
    multiset<int>::iterator it, it1; 

// now i want where iterator is look for values i the two sets and compare them and store the shorter of the two values in an int variable sum. 

    for (it = s.begin(), it1 = s1.begin(); it != s.end(); it++,it1++)  {
        (*it <= *it1 ) ? (sum=sum + (*it)) : (sum = sum + (*it1));
    return 0;

can you help why time limit is exceeding even when i m using multisets

I would say try using a regular array and then sort it before iterating through it.

but why isn’t multiset a good choice for this purpose ?