-3

to resolve a codeforces problem

I have written this c++ code and i have a big problem while displaying the result: in fact,if i add this code :

cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;

the result will be correct Otherwise it will be wrong The code:

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

void aff (vector<int> v)
{
    for (int i=0;i<v.size();i++)
        cout<<v[i]<<"|";
}

int main()
{

    int n;

    cin>>n;

    int t1[2][n];

    vector <int> t2 ;
    vector <int> t2prim ;//flous

    vector <int> t2simplifier ;
    vector <int> t2primsimplifier  ;//flous


    vector <int> t3prim ;//flous



    for(int i=0;i<n;i++)
    cin>>t1[0][i];

    for(int i=0;i<n;i++)
    cin>>t1[1][i];

    for (int i = 0;i<n;i++)
        for (int j = i+1 ;j<n ;j++)
    {
        if(t1[0][i]<t1[0][j])
         {

             t2.push_back(j);
             t2prim.push_back(t1[1][i]+t1[1][j]);
         }
    }
//    cout<<"t2"<<endl;
//    aff(t2);
//    cout<<endl;
//    aff(t2prim);
//    cout<<endl;




    //pour simplifier t2  et t2prim
    int minn;
    for (int i = 1;i<n;i++)
    {
        minn==1000000000;

        for (int j = 0 ;j<n ;j++)
    {
        if((t2[j]==i)&&(t2prim[j]<minn))
         {
            minn=t2prim[j];
         }
    }
             t2simplifier.push_back(i);
             t2primsimplifier.push_back(minn);
 }


    cout<<"t2simplifier"<<endl;
    aff(t2simplifier);
    cout<<endl;
    aff(t2primsimplifier);
    cout<<endl;


    for (int i = 0;i<t2simplifier.size();i++)
        for (int j = t2simplifier[i] ;j<n ;j++)
    {
        if(t1[0][t2simplifier[i]]<t1[0][j])
         {

             t3prim.push_back(t2primsimplifier[i]+t1[1][j]);
         }
    }
  //  cout<<"t3prim";
   // aff(t3prim);

if (t3prim.size()==0)
        cout<<-1;
    else
    {
    //talla3 min
    int k = t3prim[0];
    for (int i = 1;i<t3prim.size();i++)
    {
     if(k>t3prim[i])
            k=t3prim[i];

    }
// k is the result
    cout << k ;
    }

    return 0;
}

input :

5
2 4 5 4 10
40 30 20 10 40

the result with the part of code is :

t2simplifier
1|2|3|4|
70|50|50|50|
90

but when we delete this piece of code(of vector display) the result will be wrong :

24
  • `int t1[2][n];` is not standard c++. Some compilers support it as an extension known as variable length arrays. Notably gcc enables that extension by default. – François Andrieux Jun 01 '18 at 17:52
  • If you want a very large value, instead of using some arbitrary number, consider using [`std::numeric_limits::max()`](http://en.cppreference.com/w/cpp/types/numeric_limits). – François Andrieux Jun 01 '18 at 17:54
  • The way your code is formatted, it is unreadable... – DeiDei Jun 01 '18 at 17:54
  • 1
    Please take a moment to read [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – François Andrieux Jun 01 '18 at 17:57
  • The line `minn == 1000000000;` is performing a comparison. You likely meant to perform an assignment with `minn = 1000000000;`. I can only reproduce your problem with optimizations enabled and only if I don't fix the assignment to `minn`. This is almost certainly undefined behavior from reading from an uninitialized variable. – François Andrieux Jun 01 '18 at 17:58
  • 3
    When writing code on competitive programming they say "we do not have to follow good programming practices, we need to write it fast, we will follow later when writing real programs" and when time to debug it comes they do not realize why it is so difficult to do so. – Slava Jun 01 '18 at 18:02
  • 1
    More to the point, the aforementioned line Francois identified should be flagging a compiler warning informing you the statement is an eval that has no actual resulting effect. If your compiler didn't tell you that, turn up your warnings to pedantic levels. That is a warning that is nearly always a logical error, and this is no exception to that case. – WhozCraig Jun 01 '18 at 18:02
  • *the result will be correct Otherwise it will be wrong* ...well, yes. – Artemis Jun 01 '18 at 19:33
  • You could use a [good book](https://stackoverflow.com/q/388242/9254539), as it looks like you've picked up some really bad practices. Typing fast isn't an excuse since you will probably waste more time debugging than the time you saved. If you're worried about wasting too much time typing, you should work on your typing speed. – eesiraed Jun 02 '18 at 04:13
  • Stack Overflow isn't a free debugging service, and you should show your attempts at debugging the code with a debugger or other simpler methods such as debug print statements. This won't be the only time you end up with a bug in your code, and learning to debug your programs will help you much more than having someone find the bug for you. http://idownvotedbecau.se/nodebugging/ – eesiraed Jun 02 '18 at 04:14

1 Answers1

0

Thanks to Mr "François Andrieux"

in the code there is a typing error

The line minn == 1000000000;is performing a comparison and it should be minn = 1000000000;

this error leads to an undefined behavior from reading from an uninitialized variable.