Advertisement
BaoJIaoPisu

Untitled

Feb 12th, 2023
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define left BAO
  16. #define right ANH
  17. #define pb push_back
  18. #define pf push_front
  19. #define mp make_pair
  20. #define ins insert
  21. #define btpc __builtin_popcount
  22. #define btclz __builtin_clz
  23.  
  24. #define sz(x) (int)(x.size());
  25. #define all(x) x.begin(), x.end()
  26. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  27.  
  28. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  29.  
  30. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  31. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  32. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  33.  
  34. template<class X, class Y>
  35.     bool minimize(X &x, const Y &y) {
  36.         if (x > y)
  37.         {
  38.             x = y;
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43. template<class X, class Y>
  44.     bool maximize(X &x, const Y &y) {
  45.         if (x < y)
  46.         {
  47.             x = y;
  48.             return true;
  49.         }
  50.         return false;
  51.     }
  52.  
  53. const int MOD = 1e9 + 7; //998244353
  54.  
  55. template<class X, class Y>
  56.     void add(X &x, const Y &y) {
  57.         x = (x + y);
  58.         if(x >= MOD) x -= MOD;
  59.     }
  60.  
  61. template<class X, class Y>
  62.     void sub(X &x, const Y &y) {
  63.         x = (x - y);
  64.         if(x < 0) x += MOD;
  65.     }
  66.  
  67. /* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
  68.  
  69. const ll INF = 1e9;
  70. const int N = 5000 + 10;
  71.  
  72. int dp[N][N];
  73. pii trace[N][N];
  74. bool used[N];
  75.  
  76. struct D {
  77.     int fi, se, id;
  78. } a[N];
  79.  
  80. void BaoJiaoPisu() {
  81.     int n; cin >> n;
  82.     for(int i = 1; i <= n; i++) cin >> a[i].se;
  83.     for(int i = 1; i <= n; i++) {
  84.         cin >> a[i].fi;
  85.         a[i].id = i;
  86.     }
  87.  
  88.     sort(a + 1, a + 1 + n, [&](D x, D y) {
  89.         if(x.se == y.se) return x.fi < y.fi;
  90.         return x.se > y.se;
  91.     });
  92.    
  93.     for(int i = 0; i <= n; i++) {
  94.         for(int j = 0; j <= n + 2; j++) dp[i][j] = -INF;
  95.     }
  96.  
  97.     dp[0][0] = 0;
  98.     for(int i = 1; i <= n; i++) {
  99.         for(int j = 0; j <= n; j++) {
  100.             if(j > 0) {
  101.                 trace[i][j] = trace[i - 1][j - 1];
  102.                 dp[i][j] = dp[i - 1][j - 1];
  103.             }
  104.             if(maximize(dp[i][j], dp[i - 1][j + 1] + a[i].fi)) trace[i][j] = mp(i, j);
  105.         }
  106.     }
  107.  
  108.     cout << dp[n][0] << '\n';
  109.     vector<pii> ans;
  110.     int pos = n, coef = 0;
  111.     while(pos) {
  112.         int curr = trace[pos][coef].fi;
  113.         if(curr == pos) {
  114.             used[pos] = true;
  115.             ans.pb(mp(a[pos].id, 0));
  116.             pos--;
  117.             coef++;
  118.         } else {
  119.             pii x = trace[pos][coef];
  120.             pos = x.fi;
  121.             coef = x.se;
  122.         }
  123.     }
  124.  
  125.     reverse(all(ans));
  126.     int iter = 0;
  127.     for(int i = 1; i <= n; i++) {
  128.         if(!used[i]) {
  129.             ans[iter++].se = a[i].id;
  130.         }
  131.     }
  132.  
  133.     for(auto x : ans) {
  134.         cout << x.fi << " " << x.se << '\n';
  135.     }
  136. }
  137.  
  138. int main()
  139. {
  140.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  141.     #ifndef ONLINE_JUDGE
  142.     freopen("input.txt", "r", stdin);
  143.     freopen("output.txt", "w", stdout);
  144.     #else
  145.     //online
  146.     #endif
  147.  
  148.     int tc = 1, ddd = 0;
  149.     // cin >> tc;
  150.     while(tc--) {
  151.         //ddd++;
  152.         //cout << "Case #" << ddd << ": ";
  153.         BaoJiaoPisu();
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement