I wrote a really simple program, but it's crashing when I try to write the size of a queue (created with STL). I have no idea why, please help.
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (que.front() <= cut)
que.pop();
}
return 0;
}
You get an error because you call front while the queue is empty.
Just check if the queue is empty in your inner loop:
#include <queue>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (!que.empty() && que.front() <= cut )
que.pop();
}
return 0;
}
Your code is actually crashing on the line:
while (que.front() <= cut)
Because you have a loop that may be true for the whole queue. The next line pops a value. At some point, your queue is empty and que.front() will crash.
Related
I am making a set of pairs of Max and Min elements of Every Subset in an Array.But its giving me these errors. And at last I need Size of set.
(Edited with some suggestions)
In function 'int main()':
27:12: error: 'max_element' was not declared in this scope
27:12: note: suggested alternative: 'max_align_t'
28:12: error: 'min_element' was not declared in this scopeIn function 'int main()':
Code:
#include <iostream>
#include <set>
#include <vector>
#include <utility>
typedef std::pair<int,int> pairs;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, max, min;
set<pairs> s;
cin >> n;
int a[n];
for(int i=0;i<n;i++) {
cin >> a[i];
}
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
vector<int> v;
v.push_back(a[j]);
if(v.size() > 1) {
max = *max_element(v.begin(),v.end());
min = *min_element(v.begin(),v.end());
pairs p1 = make_pair(max, min);
s.insert(p1);
max = 0;
min = 0;
}
}
}
cout << s.size() << endl;
}
typedef pair<int,int> pairs;
should be
typedef std::pair<int,int> pairs;
(Or you could move using namespace std; so that it is before your typedef).
Plus typedefing a single pair as the plural pairs is a really really bad idea, that is going to confuse you and anyone else reading your code for the rest of this programs existence. If you want a typedef for a pair of ints, then call it that
typedef std::pair<int,int> pair_of_ints;
To make your last programme works, it was needed to move the declaration of std::vector<int> v;
Moreover, your code has a complexity O(n^3). In practice, it is possible to get a complexity O(n^2), by calculating
iteratively the max and min values.
This code compares your code and the new one. The results are identical. However, I cannot be sure
that your original code does what you intended to do.
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
typedef std::pair<int,int> pairs;
//using namespace std;
void print (const std::set<pairs> &s) {
for (auto& p: s) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << "\n";
}
int count_pairs_op (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
std::vector<int> v;
for(int j = i; j < n; j++) {
v.push_back(a[j]);
if(v.size() > 1) {
max = *std::max_element(v.begin(), v.end());
min = *std::min_element(v.begin(), v.end());
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
}
print (s);
return s.size();
}
int count_pairs_new (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
min = a[i];
max = a[i];
for(int j = i+1; j < n; j++) {
max = std::max (max, a[j]);
min = std::min (min, a[j]);
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
print (s);
return s.size();
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_op(a) << std::endl;
std::cout << count_pairs_new(a) << std::endl;
}
It appears that there was a mistake in the understanding of the problem.
For each subarray, we have to consider the maximum and the second maximum.
Moreover, we know that all elements are distinct.
As the size can be up to 10^5, we have to look for a complexity smaller than O(n^2).
In practice, each element can be the second element of two subarrays,
if there exist a greater element before and after it.
We just have to check it.
This can be perfomed by calculating, for each index i, the maximum value before and after it.
Total complexity: O(n)
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
int count_pairs_2nd_max (const std::vector<int>& a) {
int n = a.size();
int count = 0;
std::vector<int> max_up(n), max_down(n);
max_up[0] = -1;
for (int i = 1; i < n; ++i) {
max_up[i] = std::max(max_up[i-1], a[i-1]);
}
max_down[n-1] = -1;
for (int i = n-2; i >= 0; --i) {
max_down[i] = std::max(max_down[i+1], a[i+1]);
}
for(int i = 0; i < n; ++i) {
if (max_up[i] > a[i]) count++;
if (max_down[i] > a[i]) count++;
}
return count;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_2nd_max(a) << std::endl;
}
I saw some algorithm for a problem and rewrote it but I changed the fast_count_segment function's inner loop exit condition j=n1 to the break statement,the code timed out. Why does this happen?
The code runs fine otherwise but when I submit to the online grader, it shows code timeout, but as soon as I replace break with j=n1, it passes.
Shouldn't both be doing the same thing?
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> fast_count_segments(vector<pair<int,int>> &v, vector<int> points) {
vector<int> cnt(points.size());
int n1=v.size(),n2=points.size(),i,j,count;
for(i=0;i<n2;i++){
count=0;
for(j=0;j<n1;j++){
if(points[i]>=v[j].first && points[i]<=v[j].second)
count++;
else if(points[i]<v[j].first)
break; //j=n1; Changed this statement to break
}
cnt[i]=count;
}
return cnt;
}
int main() {
int n, m,a,b;
cin >> n >> m;
vector<pair<int,int>> v;
for (size_t i = 0; i < n; i++) {
cin >>a>>b;
v.push_back(make_pair(a,b));
}
vector<int> points(m);
for (size_t i = 0; i < points.size(); i++) {
cin >> points[i];
}
sort(v.begin(),v.end());
vector<int> cnt = fast_count_segments(v, points);
for (size_t i = 0; i < cnt.size(); i++) {
cout << cnt[i] << ' ';
}
cout<<endl;
return 0;
}
I was writing a code to sort and output the number by using "pair".
I tried some cases bat there was no output.
How should I rewrite the code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int k, i, n;
cin >> n;
pair<int, int> a[n];
for (i = 0; i < n; i++) {
cin >> k;
a[i].first = -k;
a[i].second = i + 1;
}
sort(a, a + n);
for (i = 0; i++; i < n) {
cout << a[i].second;
}
}
for(i=0;i++;i<n){
You meant to write this as:
for(i=0;i<n;i++){
Input:
1 1 2 2 3
Desired Output:
3
Here is my code:
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <iostream>
using namespace std;
int main(){
vector<int> v;
vector<int>::iterator it;
// input variables
int input, a, arr[10000];
// input
cin >> input;
// comment all your loops, etc
for(int i = 0; i < input ; i++){
cin >> a;
arr[i] = a;
v.push_back(a);
}
for(int j = 0; j < input; j++){
int ch1 = arr[j];
for(int i = 0;i < input; i++){
if(i == j){
}
else{
if(ch1 == arr[i]){
v.erase(std::remove(v.begin(), v.end(), ch1),v.end());
}
else{
}
}
}
}
for(it = v.begin(); it != v.end(); it++){
cout << *it;
}
return 0;
}
erase() is not working here.
How can I solve this problem?
Your problem is that you define two variables with name v.
vector<int>v;
for(int v=0...
So you basically hide your vector with an int and the compiler tries to call erase() for int, which gives you error.
Just change the name of one of these variables.
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
struct process
{
int burst;
int ar;
};
int x = 4;
int arival[x];
int burst[x];
ifstream arrival, burrst;
arrival.open("arrival.txt");
burrst.open("burrst.txt");
for (int i = 0; i<x; i++)
{
arrival >> arival[i];
burrst >> burst[i];
}
arrival.close();
burrst.close();
vector<process> a[x];
for (int i = 0; i<x; i++)
{
a[i].push_back({ burst[i], arival[i] });
}
cout << "Process\t" << "Arrival Time\t" << "Burst Time\n";
for (int i = 0; i<x; i++)
{
char k = 'A';
cout << k << "\t" << a[i].back().burst << "\t" << a[i].back().ar << endl;
}
queue<process> wait, ready; /* Declare a queue */
wait.push(a[1]);
return 0;
}
The compiler is not allowing me to push vector value into queue When I try to insert a[1] in wait queue like this:
wait.push(a[1]);
I get the following error
invalid arguments
Please have a look and help me to remove the error.
I think what you are looking for is just a vector of process, not an array of vector of process
Instead of:
vector<process> a[x];
Use:
vector<process> a;
and then, in the for loop, use:
// a[i].push_back({ burst[i], arival[i] });
// ^^^ drop this.
a.push_back({ burst[i], arival[i] });