Advertisement
a53

maxime

a53
Jan 19th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 100005
  3. using namespace std;
  4. ifstream fin("maxime.in");
  5. ofstream fout("maxime.out");
  6. int n, m, maxim, premaxim, pozmaxim, pozpremaxim, p, rep, i, varf;
  7. int a[NMAX], CNT[NMAX], PZ[NMAX], st[NMAX], DR[NMAX], OK[NMAX];
  8.  
  9. int main()
  10. {
  11. fin>>n;
  12. premaxim=-1;
  13. maxim=-1;
  14. pozpremaxim=-1;
  15. premaxim = -1;
  16. a[n+1]=NMAX+1;
  17. for(i=1; i<=n+1; ++i)
  18. {
  19. if(i<= n)
  20. fin>>a[i];
  21. if(a[i]>maxim)
  22. {
  23. premaxim=maxim;
  24. pozpremaxim=pozmaxim;
  25. maxim=a[i];
  26. pozmaxim=i;
  27. if(PZ[pozpremaxim]==-1)
  28. PZ[pozpremaxim]=i;
  29. PZ[pozmaxim]=-1;
  30. CNT[i]=1;
  31.  
  32. }
  33. else
  34. {
  35. if(PZ[pozmaxim]==-1 && a[i]>=premaxim)
  36. {
  37. PZ[pozmaxim]=i;
  38. if(a[i]==premaxim)
  39. OK[pozmaxim]=1;
  40. }
  41. if(a[i] == maxim)
  42. {
  43. CNT[i]=2;
  44. rep++;
  45. }
  46. }
  47. }
  48. st[0] = n+1;
  49. DR[st[0]] = 0;
  50. varf = 0;
  51. for(i=n;i>=1;i--)
  52. {
  53. while(a[i]>a[st[varf]])
  54. varf--;
  55. if(a[i]==a[st[varf]])
  56. DR[i]=1+DR[st[varf]];
  57. else
  58. DR[i]=DR[st[varf]];
  59. st[++varf]=i;
  60. }
  61. fin>>m;
  62. for(i=1;i<=m;i++)
  63. {
  64. fin>>p;
  65. if(CNT[p]==0)
  66. fout<<rep<<" ";
  67. else
  68. if(CNT[p]==2)
  69. fout<<rep-1<<" ";
  70. else
  71. if(CNT[p]==1)
  72. {
  73. if(OK[p])
  74. fout<<1+rep-DR[p]+DR[PZ[p]]<<" ";
  75. else
  76. fout<<rep-DR[p]+DR[PZ[p]]<<" ";
  77. }
  78. }
  79. return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement