Блог пользователя StellarSpecter

Автор StellarSpecter, история, 5 часов назад, По-английски

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 ?

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

»
5 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

wtf are you doing

»
5 часов назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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.

»
5 часов назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

»
2 часа назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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?