Skip to main content
added 18 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Need help optimizing code, getting TLE for subtask2 CodeChef Chewing challenge

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem: One of the problems is the https://www.codechef.com/ZCOPRAC/problems/ZCO13003CodeChef Chewing problem The.

The code inputstakes an integer N that specifies the number of chewing gums, integer K that is the maximum hardness quotient of a pair the chewing gums possible to be eaten. This is followed by N integers that specify the hardness quotient of each type of gum. My code selects only gums with a quotient less than K and adds them to a vector. A nested for loop is used to count the number of pairs for which hardness quotient of Gum_Type1+Gum_Type2 < K. Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

CouldFor subtask 2, I'm getting TLE by 0.01s. Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

Need help optimizing code, getting TLE for subtask2

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem:https://www.codechef.com/ZCOPRAC/problems/ZCO13003 The code inputs an integer N that specifies the number of chewing gums, integer K that is the maximum hardness quotient of a pair the chewing gums possible to be eaten. This is followed by N integers that specify the hardness quotient of each type of gum. My code selects only gums with a quotient less than K and adds them to a vector. A nested for loop is used to count the number of pairs for which hardness quotient of Gum_Type1+Gum_Type2 < K. Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

CodeChef Chewing challenge

The ZCO is today and I did 5 problems to prepare for it overnight. One of the problems is the CodeChef Chewing problem.

The code takes an integer N that specifies the number of chewing gums, integer K that is the maximum hardness quotient of a pair the chewing gums possible to be eaten. This is followed by N integers that specify the hardness quotient of each type of gum. My code selects only gums with a quotient less than K and adds them to a vector. A nested for loop is used to count the number of pairs for which hardness quotient of Gum_Type1+Gum_Type2 < K.

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

For subtask 2, I'm getting TLE by 0.01s. Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem:https://www.codechef.com/ZCOPRAC/problems/ZCO13003 The code inputs an integer N that specifies the number of chewing gums, integer K that is the maximum hardness quotient of a pair the chewing gums possible to be eaten. This is followed by N integers that specify the hardness quotient of each type of gum. My code selects only gums with a quotient less than K and adds them to a vector. A nested for loop is used to count the number of pairs for which hardness quotient of Gum_Type1+Gum_Type2 < K. Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem:https://www.codechef.com/ZCOPRAC/problems/ZCO13003 Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem:https://www.codechef.com/ZCOPRAC/problems/ZCO13003 The code inputs an integer N that specifies the number of chewing gums, integer K that is the maximum hardness quotient of a pair the chewing gums possible to be eaten. This is followed by N integers that specify the hardness quotient of each type of gum. My code selects only gums with a quotient less than K and adds them to a vector. A nested for loop is used to count the number of pairs for which hardness quotient of Gum_Type1+Gum_Type2 < K. Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

Could somebody help me optimize this code? I don't want to get TLEs in the exam today.

Source Link

Need help optimizing code, getting TLE for subtask2

The ZCO is today and I did 5 problems to prepare for it overnight. There's this one problem where I'm getting TLE by 0.01s. Link to problem:https://www.codechef.com/ZCOPRAC/problems/ZCO13003 Code:

#include <iostream>
#include <vector>
#include <algorithm>
#define ll unsigned long long int
#define speedup ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
 
int main(){
  speedup;
  ll N,K,hq,numPairs=0;
  vector<ll> hardness;
  cin >> N >> K;
  for(ll i=0; i< N; i++){
    cin >> hq;
    if (hq< K) hardness.push_back(hq); 
  }
  sort(hardness.begin(), hardness.end());
  for(vector<ll>::iterator it=hardness.begin(); it<hardness.end();it++){
    for(vector<ll>::iterator it2=it+1;it2<hardness.end();it2++){
      if (*it+*it2 < K) numPairs+=1;
      else hardness.erase(it2);
    }
  }
  cout << numPairs<<endl;
}  

Could somebody help me optimize this code? I don't want to get TLEs in the exam today.