0

Just see code enclosed between '' in comment. It outputs all '0s' and nothing else. Why is it producing only 0s and not the elements i just inserted into stl Map ?

For example if my input is '3,45,6,3,1' then output should be '1,2,3,1,5' ,shouldn't it be?

#include<bits/stdc++.h>
#define lli long long int
using namespace std;
lli k,o[1000010];
int n,tmp=0,i,uq=0,what,ans=1;
map<lli,int> tp;
int main() {
    cin>>n>>k;
//<tagz
    for (i=1;i<=n;i++) {
        cin>>o[i];
        if (tp[o[i]]==0) uq++;
        tp.insert(make_pair(o[i],i));
        cout<<tp[o[i]]<<",";
        tmp=max(tmp,tp[o[i]]);
    }
//   /tagz>
    //cout<<"ZZZ:tmp="<<tmp<<" ,uq="<<uq<<endl;
    cout<<"ZZZ:"<<tp[2]<<endl;
    if (uq<k) {
        cout<<"0";
        return 0;
    }
    i=tmp;int fck=0;
    for (;i<n;i++) {
        fck++;if (fck==200) break;
    //  cout<<"AT "<<i<<endl;
        uq=1;tmp=0;
        map<lli,int> yo;
        for (int j=i+1;j<n;j++) {
            if (o[j]!=o[i]) {
                if (yo[o[j]]==0) uq++;
                yo.insert(make_pair(o[j],j));
                tmp=max(tmp,yo[o[j]]);
            }
        }
        if (uq<k) {
            cout<<ans;
            return 0;
        }
    //  cout<<"  Uq="<<uq<<", tmp="<<tmp<<endl;
        ans++;
        i=tmp;
    }
    cout<<ans;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Shikhar
  • 31
  • 5
  • You should really start with much simpler programs before tackling `std::map`. There are some fundamental problems with your code which probably shadow any bug at a higher level of program logic. For example, you seem to assume that array indices start at 1 (they don't, they are 0-based), and you declare an array of 1000010 elements even if the user enters only a few numbers! Your variable names are completely meaningless (`uq`, `tmp`, `fck`...), and your `#define` for a built-in type is nothing but code obfuscation. – Christian Hackl Mar 21 '15 at 10:45
  • @ChristianHackl : Can you please tell me whats wrong in first 6 lines of main code ? You needn't read rest of code ,in first loop i am just trying to input some numbers and trying to print them after storing them in map tp ...(hence removing duplicates). Please :) ? – Shikhar Mar 21 '15 at 10:52
  • 1
    I already explained the problem with your array index incorrectly starting at 1. Generally, it is completely unclear (to me, at least) what your program wants to achieve. Your code communicates no intent. – Christian Hackl Mar 21 '15 at 10:58

1 Answers1

1

When you access a map with the [], it first checks if the key exists in the map. If it doesn't then, it creates a new member with the default value (which in this case is 0).

When you use the insert function, it will only insert the value if there isn't already a value for the key.

For more reference, you can read this answer: https://stackoverflow.com/a/4286924/1466095

In this case, what happens is:

if (tp[o[i]]==0) uq++;: This line creates a new element for the key with value 0. tp.insert(make_pair(o[i],i));: The next line doesn't do anything since, there already exists a member in the map with the key o[i]. Therefore, your output is all 0.

To fix this, you need to replace the line insert line with:

tp[o[i]] = i;
Community
  • 1
  • 1
Zero Fiber
  • 4,417
  • 2
  • 23
  • 34