Advertisement
rotti321

Untitled

Mar 20th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream f("findmin.in");
  5. ofstream g("findmin.out");
  6. struct Elem{
  7.     int val,poz;
  8. };
  9. using Sir=vector<Elem>;
  10.  
  11. int n;
  12.  
  13. vector<int> minn,rez;
  14.  
  15. void citire(Sir &a,int &n)
  16. {
  17.     f>>n;
  18.     Elem x;
  19.     for(int i=0;i<n;++i){
  20.         f>>x.val;
  21.         x.poz=i+1;
  22.         a.push_back(x);
  23.     }
  24. }
  25. void afis_poz(Sir a)
  26. {
  27.     for(auto x:a)
  28.     {
  29.         cout<<x.poz<<" ";
  30.     }
  31.     cout<<endl;
  32. }
  33. void afis_val(Sir a)
  34. {
  35.     for(auto x:a)
  36.     {
  37.         cout<<x.val<<" ";
  38.     }
  39.     cout<<endl;
  40. }
  41. bool cmp(Elem x,Elem y)
  42. {
  43.     return (x.val<y.val);
  44. }
  45.  
  46. bool myfn(Elem x, Elem y)
  47. {
  48.     return x.poz<y.poz;
  49. }
  50. void rezolva(Sir a,int n)
  51. {
  52.     int k;
  53.    // g<<"-1 ";
  54.     for(int i=0;i<n;i++)
  55.     {
  56.         k=a[i].poz-1;
  57.        // minn=*min_element(a.begin(),a.begin()+k,cmp);
  58.         if(minn[k]<a[k].poz){
  59.             rez[k]=minn[k];
  60.         }
  61.         else{
  62.             rez[k]=-1;
  63.         }
  64.     }
  65. }
  66. void constr_minn(Sir a) //,vector<int> &minn
  67. {
  68.     int ml=a[0].poz;
  69.     //minn.push_back(ml);
  70.     for(auto x:a)
  71.     {
  72.         ml=min(ml,x.poz);
  73.         minn.push_back(ml);
  74.     }
  75. }
  76. int main()
  77. {
  78.     Sir a,s;
  79.     citire(a,n);
  80.     s.resize(n);
  81.     rez.resize(n);
  82.     copy(a.begin(),a.end(),s.begin());
  83.     afis_val(a);
  84.     afis_poz(a);
  85.     sort(a.begin(),a.end(),cmp);
  86.     constr_minn(a);
  87.     for(auto x:minn)
  88.     {
  89.         cout<<x<<" ";
  90.     }
  91.     cout<<'\n';
  92.  
  93.     rezolva(s,n);
  94.     afis_val(s);
  95.     for(auto x:rez)
  96.     {
  97.         cout<<x<<" ";
  98.     }
  99.     cout<<'\n';
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement