Advertisement
YEZAELP

PROG-1081: ระเบิดบล็อก (blowblock)

May 4th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 5e2;
  5. using lli = long long;
  6. vector <lli> a, b, c, d;
  7.  
  8. int main(){
  9.  
  10.     int n;
  11.     scanf("%d", &n);
  12.  
  13.     int aa = 1, bb = 1;
  14.     for(int i=1;i<=n;i++){
  15.         for(int j=1;j<=n;j++){
  16.             lli input;
  17.             scanf("%lld", &input);
  18.             int x = i % 2;
  19.             int y = j % 2;
  20.             if(x == 1 and y == 1) a.push_back(input);
  21.             else if(x == 1 and y == 0) b.push_back(input);
  22.             else if(x == 0 and y == 1) c.push_back(input);
  23.             else if(x == 0 and y == 0) d.push_back(input);
  24.         }
  25.     }
  26.  
  27.     sort(a.begin(), a.end());
  28.     sort(b.begin(), b.end());
  29.     sort(c.begin(), c.end());
  30.     sort(d.begin(), d.end());
  31.  
  32.     lli sum = 0;
  33.     int sz = a.size();
  34.     for(int i=0;i<sz;i++){
  35.         sum += (lli) a[i] * b[i] * c[i] * d[i];
  36.     }
  37.  
  38.     printf("%lld", sum);
  39.  
  40.     return 0;
  41. }
  42.  
  43. /*
  44.     greedy
  45.     a b a b a b ...
  46.     c d c d c d ...
  47.     a b a b a b ...
  48.     c d c d c d ...
  49.     . . . . . .
  50.     . . . . . .
  51.     . . . . . .
  52.     ภายใน a b c d สามารถสลับตำแหน่งกันได้ทั้งหมด
  53.     ดังนั้นจึง sort เพื่อนำเลขมากมาคูณกัน
  54. */
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement