Advertisement
Patrickmeme

inundatie

Mar 30th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. #define MAX_N 100000
  6. #define INFINIT 20001
  7.  
  8. int coloane[MAX_N+1];
  9.  
  10. int apasec(int p){
  11.     int maxim2, cp2;
  12.     maxim2=0;
  13.     while(coloane[p]!=INFINIT){
  14.         cp2=p-1;
  15.         while(coloane[cp2]<=coloane[p]){
  16.             maxim2+=coloane[p]-coloane[cp2];
  17.             cp2--;
  18.         }
  19.         p=cp2;
  20.     }
  21.     return maxim2;
  22. }
  23. int main()
  24. {
  25.     ifstream cin("inundatie.in");
  26.     ofstream cout("inundatie.out");
  27.     int maxim, st, dr, mij, rez, minim, cer, n, p, s, poz, h, dif;
  28.     cin>>cer>>n>>p>>s;
  29.     cin>>coloane[1];
  30.     maxim=minim=coloane[1];
  31.     for(int i=2; i<=n; i++){
  32.         cin>>coloane[i];
  33.         minim=min(minim, coloane[i]);
  34.         maxim=max(maxim, coloane[i]);
  35.     }
  36.     coloane[0]=coloane[n+1]=INFINIT;
  37.     if(cer==1)
  38.         cout<<maxim-minim;
  39.     else if(cer==2)
  40.         cout<<apasec(p);
  41.     else if(cer==3){
  42.         st=1;
  43.         dr=n+1;
  44.         while(st<dr){
  45.             mij=(st+dr+1)/2;
  46.             maxim=apasec(mij);
  47.             if(maxim<=s)
  48.                 st=mij;
  49.             else
  50.                 dr=mij-1;
  51.         }
  52.         cout<<st;
  53.     }else if(cer==4){
  54.         maxim=dif=0;
  55.         h=coloane[p];
  56.         rez=poz=p;
  57.         while(coloane[p]!=INFINIT){
  58.             if(coloane[p]<h){
  59.                 dif++;
  60.                 p--;
  61.                 if(coloane[p]==INFINIT && maxim<=dif){
  62.                     maxim=dif;
  63.                     rez=poz;
  64.                 }
  65.             }else{
  66.                 if(maxim<=dif){
  67.                     maxim=dif;
  68.                     rez=poz;
  69.                 }
  70.                 while(coloane[p]<=h)
  71.                         p--;
  72.                 h=coloane[p];
  73.                 dif=0;
  74.                 poz=p;
  75.                 p--;
  76.             }
  77.         }
  78.         cout<<rez;
  79.     }
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement