fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const long mod=1e9+7;
  4. #define ll long long
  5. #define ft first
  6. #define sc second
  7. #define pb push_back
  8. ll n,rest,kq,a[300005];
  9. pair<ll ,ll > b[300005];
  10. bool kt[300005];
  11. vector<vector<ll> > f;
  12. bool cmp(pair<ll , ll > a , pair <ll ,ll > b){
  13. return a.sc<b.sc;
  14. }
  15.  
  16. ll tinh (ll i , ll c){
  17. if (i>n) return 1;
  18. if (f[i][c]!=-1) return f[i][c];
  19. ll t=0;
  20. for (int j=1;j<=n;j++){
  21. if (kt[j]==1) continue;
  22. kt[j]=1;
  23. t+=tinh(i+1,j);
  24. kt[j]=0;
  25. }
  26. return (f[i][c]=t);
  27. }
  28.  
  29. void thutu(ll i , ll m ){
  30. if (i>n) return;
  31. for (int j=m+1;j<=a[i]-1;j++) rest+=tinh(i+1,j)%mod;
  32. thutu(i+1,a[i]);
  33. }
  34.  
  35. int main()
  36. {
  37. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  38. freopen("thutu.inp","r",stdin);
  39. freopen("thutu.out","w",stdout);
  40. cin>>n;
  41.  
  42. f.resize(n+1);
  43. for (int i=1;i<=n;i++){
  44. for (int j=1;j<=n;j++) f[i].pb(-1);
  45. }
  46. for (int i=1;i<=n;i++){
  47. cin>>b[i].sc;b[i].ft=i;
  48. }
  49. sort(b+1,b+n+1,cmp);
  50. for (int i=1;i<=n;i++) a[b[i].ft]=i;
  51. thutu(1,0);
  52. cout<<rest+1;
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty