Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I what I want is to collect maximum element from each row(in a 2D vector) and push it into a new vector. And if the new vector is sorted I want the sum of all vector elements.

I tried it but the code is showing error : "cannot convert 'std::vector to 'int' assignment"

also, is my code correct?

What I have tried:

C++
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int t,n,v1;
vector < vector<int> > vec;
vector<int> max;
cin>>t;
while(t--)
{
 cin>>n;
 for(int i=0; i<n; i++)
 {  vector<int> row;
    for(int j=0; j<n; j++)
    {
     int val;
      cin>>val;
      row.push_back(val);   
     }
     vec.push_back(row);
       }    

for (int i=0; i<n; i++)
{
    for(int j=0; j<n; j++)
    {
        v1=*max_element(vec.begin(), vec.end());
        max.push_back(v1);
    }
}

if(is_sorted(max.begin(),max.end()))
{
    cout<<accumulate(max.begin(), max.end(),0)<<endl;

}

else
cout<<"-1"<<endl;

}
return 0;
 }
Posted
Updated 8-Jan-18 12:04pm

Quote:
for (int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
v1=*max_element(vec.begin(), vec.end());
max.push_back(v1);
}
}
Should be instead
C++
for (int i=0; i<n; i++)
{
   v1 = *max_element(vec[i].begin(), vec[i].end());
   max.push_back(v1);
}
 
Share this answer
 
Comments
CodeBlooded 8-Jan-18 6:31am    
Thanks, this help, however, I would like to ask one more thing. I want result such as let the 2d vector be

1 2 3
4 5 6
7 8 9

the output should be 18.

so, let's denote the element picked from sequence Mi by Ai. For each i (2 ≤ i ≤ N), Ai greater than Ai-1.

Compute the maximum possible value of A1 + A2 + ... + A_N. If it's impossible to pick the elements A1, A2, ..., A_N, print -1 instead.

but it seems like there is still a problem in my code which I can't understand.
You try to solve this problem Contest Page | CodeChef[^].
As usual CodeChef challenges are not for beginners, and unfortunately you have read the requirements too fast and misunderstood it. All details count.
Someone else have asked for help, read answers given, they should help you to understand.
Can someone please tell me why is this code not being accepted? Note:I am not asking for a solution, just tell me where this code fails[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900