steelarm's blog

By steelarm, history, 5 years ago, In English

The problem

I am trying out the following approach:

1.I sort the array of weights in decreasing order and "put" the largest weight in one group.

2.Then I keep adding weights to the other group until it becomes larger.

3.Then I start adding apples to the first group until this is one is larger and again start putting apples in the other group and keep on going like this until i reach the end of the array.

4.And then I print the difference between the total weights of the two groups.

My solution gives the correct answer with the sample test cases and a few others too but I am getting a WA, so can anyone suggest something I can do to correct my code. Code:

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

#define B begin()
#define E end()
#define I iterator
using pii = pair < int , int >;
using vi = vector < int >;
using llu = unsigned long long int;
using ll = long long int;


//ofstream cout ("op.txt");
//ifstream fin("inp.in");
int main ()
{
    ll n;
    cin >> n;
    vector <ll> vec(n);
    ll val;
    for ( int i = 0 ; i < n ; i ++ ){
        cin >> val;
        vec[i] = val;
    }
    sort ( vec.B , vec.E , greater<ll>() );
    ll ansa = 0 , ansb = 0 ;
    ansa = vec[0];
    for ( int i = 1; i < n ; i++ ){
        if ( ansb < ansa )
            ansb += vec[i];
        else
            ansa += vec[i];
    }
    cout << max( ansa , ansb ) - min( ansa , ansb );
    return 0;
}



Any and all help is appreciated

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By steelarm, history, 5 years ago, In English

Since I have limited time for practice due to school , I am constantly uncertain about how I should practice. The ways I see are

1.Keep solving varied and general problems from codeforces , spoj , codechef etc. , following a roadmap.

2.Solve problems from specific topics and work my way up from simple to complex topics.

3.Or work somewhat how USACO training works and do interesting problems that increase in difficulty , while still belonging to relevant topics.

I should state that I am still in the early stages of practice and my current focus is on qualifying the national olympiad of my country (India) so I cannot move to advanced topics. So could anyone suggest what path I should take or suggest an alternative path to maximise my performance.

And a big thanks for helping out.

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it