StellarSpecter's blog

By StellarSpecter, history, 3 hours ago, In English

2009A - Minimize!

What is wrong with my code ?? I have been struggling with this for so long.

Can anyone help me debug this code ??

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    int t;
    cin >> t;
    
    while (t--) {
        int a, b;
        cin >> a >> b;
 
        vector<int> v;
        for (int i = a; i <= b; i++) {
            v.push_back(i);
        }
 
        sort(v.begin(), v.end());
 
        int m = v.size() / 2;
        int c = v[m]; 
        cout << c << endl;
    }
}

Can you help me what is wrong ???

I first push all values from a to b in a vector then I sort the vector to greedily select the median value.

Why is my logic wrong ?

  • Vote: I like it
  • -10
  • Vote: I do not like it

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

PS: If you are ratist and here to just downvote me, Mike please ban such people.

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

wtf are you doing

»
3 hours ago, # |
  Vote: I like it +3 Vote: I do not like it

I think you've misread the question, you are given 2 numbers and the equation : min{(c-a)+(b-c)} where (a≤c≤b) ,if you look closely the equation is min(b-a) which is a constant 'b-a' so thats it.

»
3 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I mean, you just have to return (c-a)+(b-c), which equals (b-a).

»
28 minutes ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

cout << "Jesus Christ" << endl;

Jesus Christ is the answer to everything. I get that you're a newbie, but how do you not even know this?