DuongNhi99

TOWER

Mar 10th, 2022
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 5e3+5;
  5.  
  6. struct pt{
  7.     int c, r, d, cs;
  8. };
  9.  
  10. bool temp1(pt a, pt b){
  11.     return a.d > b.d || (a.d == b.d && a.r >= b.r);
  12. }
  13.  
  14. bool temp2(pt a, pt b){
  15.     return a.r*a.d > b.r*b.d;
  16. }
  17.  
  18.  
  19. int n;
  20. pt a[N];
  21.  
  22. void Tower1(){
  23.     freopen("TOWERi.txt", "w", stdout);
  24.  
  25.     int TowerMax[N];
  26.     int res, trace[N];
  27.  
  28.     sort(a+1, a+n+1, temp1);
  29.    
  30.     for(int i = 1; i <= n; ++i){
  31.         TowerMax[i] = 1;
  32.  
  33.         for(int j = 1; j <= i-1; ++j){
  34.             if(a[j].r >= a[i].r && a[j].d >= a[i].d)
  35.                 if(TowerMax[i] < TowerMax[j]+1){
  36.                     TowerMax[i] = TowerMax[j]+1;
  37.                     trace[i] = j;
  38.                 }
  39.         }
  40.     }
  41.  
  42.     res = max_element(TowerMax + 1, TowerMax + n + 1) - TowerMax;
  43.     vector<pt> kq;
  44.     while(res != 0){
  45.         kq.push_back(a[res]);
  46.         res = trace[res];
  47.     }
  48.  
  49.     cout << kq.size() << '\n';
  50.     for(int i = kq.size()-1; i >= 0; --i)
  51.         cout << kq[i].cs << ' ' << kq[i].d << ' ' << kq[i].r << ' ' << kq[i].c << '\n';
  52. }
  53.  
  54. void Tower2(){
  55.     freopen("TOWER2.txt", "w", stdout);
  56.  
  57.     int TowerMax[N];
  58.     int res, trace[N];
  59.  
  60.     sort(a+1, a+n+1, temp2);
  61.    
  62.     for(int i = 1; i <= n; ++i){
  63.         TowerMax[i] = a[i].c;
  64.  
  65.         for(int j = 1; j <= i-1; ++j){
  66.             if(a[j].r >= a[i].r && a[j].d >= a[i].d)
  67.                 if(TowerMax[i] < TowerMax[j]+a[i].c){
  68.                     TowerMax[i] = TowerMax[j]+a[i].c;
  69.                     trace[i] = j;
  70.                 }
  71.         }
  72.     }
  73.  
  74.     res = max_element(TowerMax + 1, TowerMax + n + 1) - TowerMax;
  75.     vector<pt> kq;
  76.     while(res != 0){
  77.         kq.push_back(a[res]);
  78.         res = trace[res];
  79.     }
  80.  
  81.     cout << kq.size() << '\n';
  82.     for(int i = kq.size()-1; i >= 0; --i)
  83.         cout << kq[i].cs << ' ' << kq[i].d << ' ' << kq[i].r << ' ' << kq[i].c << '\n';
  84. }
  85.  
  86. int main()
  87. {
  88.     //freopen("a.txt", "r", stdin);
  89.     ios_base::sync_with_stdio(false);
  90.     cin.tie(0); cout.tie(0);
  91.  
  92.     cin >> n;
  93.     for(int i = 1; i <= n; ++i){
  94.         vector<int> t(3);
  95.         for(int j = 0; j < 3; ++j)
  96.             cin >> t[j];
  97.         sort(t.begin(), t.end());
  98.  
  99.         a[i].c = t[0];
  100.         a[i].r = t[1];
  101.         a[i].d = t[2];
  102.          a[i].cs = i;
  103.     }
  104.  
  105.     Tower1();
  106.     Tower2();
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment