Advertisement
BaoJIaoPisu

Untitled

Aug 16th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minele min_element
  11. #define maxele max_element
  12. #define lb lower_bound //first pos >= val
  13. #define ub upper_bound // first pos > val
  14. #define cnt_bit __builtin_popcount
  15. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  16. //#pragma GCC optimize("Ofast")
  17. //#pragma GCC target("avx,avx2,fma")
  18. using namespace std;
  19.  
  20. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  21.  
  22. typedef long long ll;
  23. typedef pair<ll, ll> pll;
  24. typedef pair<int, int> pii;
  25.  
  26.  
  27. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  28. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  29. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  30.  
  31. const ll oo = 1e18;
  32. const ll maxN = 1e6;
  33.  
  34. /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
  35.  
  36. void maximize(int &a, int b) {
  37.     a = max(a, b);
  38. }
  39.  
  40. void minimize(int &a, int b) {
  41.     a = min(a, b);
  42. }
  43.  
  44. int p[50], a[5], b[5];
  45. int dp[1010][1010];
  46. int f[22][(1 << 16)];
  47.  
  48. void solve() {
  49.     int v, n, m;
  50.     cin >> v >> n >> m;
  51.     for(int i = 1; i <= n; i++) cin >> p[i];
  52.     if(v <= 1000) {
  53.         for(int i = 0; i <= v; i++) {
  54.             for(int j = 0; j <= v; j++) {
  55.                 dp[i][j] = -1;
  56.             }
  57.         }
  58.         dp[v][0] = 0;
  59.  
  60.         queue<pii> q;
  61.         q.push(make_pair(v, 0));
  62.         while(!q.empty()) {
  63.             a[0] = q.front().fi, a[1] = q.front().se; q.pop();
  64.             a[2] = v - a[0] - a[1];
  65.             for(int i = 0; i <= 2; i++) {
  66.                 for(int j = 0; j <= 2; j++) {
  67.                     if(i == j) continue;
  68.                     //from ai to aj
  69.                     for(int x = 0; x <= n; x++) {
  70.                         if(a[i] <= p[x]) break;
  71.                         int add = a[i] - p[x];
  72.                         for(int y = 0; y < 3; y++) b[y] = a[y];
  73.                         b[i] = a[i] - add; b[j] = a[j] + add;
  74.                         if(dp[b[0]][b[1]] == -1) {
  75.                             dp[b[0]][b[1]] = dp[a[0]][a[1]] + 1;
  76.                             q.push(make_pair(b[0], b[1]));
  77.                         }
  78.                     }
  79.  
  80.                     for(int x = 0; x <= n; x++) {
  81.                         if(a[j] >= p[x]) continue;
  82.                         int add = p[x] - a[j];
  83.                         if(add > a[i]) break;
  84.                         for(int y = 0; y < 3; y++) b[y] = a[y];
  85.                         b[i] = a[i] - add; b[j] = a[j] + add;
  86.                         if(dp[b[0]][b[1]] == -1) {
  87.                             dp[b[0]][b[1]] = dp[a[0]][a[1]] + 1;
  88.                             q.push(make_pair(b[0], b[1]));
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.  
  95.         int ans = 1e9;
  96.         for(int i = 0; i <= v; i++) {
  97.             for(int j = 0; j <= v; j++) {
  98.                 if(dp[i][j] == -1) continue;
  99.                 if(i == m || j == m || (v - i - j) == m) ans = min(ans, dp[i][j]);
  100.             }
  101.         }
  102.  
  103.         if(ans == 1e9) ans = -1;
  104.         cout << ans;
  105.     } else {
  106.         for(int i = 0; i <= n; i++) {
  107.             for(int j = 0; j <= v; j++) f[i][j] = -1;
  108.         }
  109.         f[0][v] = 0;
  110.  
  111.         queue<pii> q;
  112.         q.push(make_pair(0, v));
  113.         while(!q.empty()) {
  114.             int g = f[q.front().fi][q.front().se];
  115.             a[0] = p[q.front().fi], a[1] = q.front().se; q.pop();
  116.             a[2] = v - a[0] - a[1];
  117.             for(int i = 0; i < 3; i++) {
  118.                 for(int j = 0; j < 3; j++) {
  119.                     if(i == j) continue;
  120.  
  121.                     //from ai to aj
  122.                     for(int x = 0; x <= n; x++) {
  123.                         if(a[i] <= p[x]) continue;
  124.                         int add = a[i] - p[x];
  125.                         for(int y = 0; y < 3; y++) b[y] = a[y];
  126.                         b[i] = p[x]; b[j] = a[j] + add;
  127.                         if(f[x][b[j]] == -1) {
  128.                             f[x][b[j]] = g + 1;
  129.                             q.push(make_pair(x, b[j]));
  130.                         }
  131.                     }
  132.  
  133.                     for(int x = 0; x <= n; x++) {
  134.                         if(a[j] >= p[x]) continue;
  135.                         int add = p[x] - a[j];
  136.                         if(add > a[i]) break;
  137.                         for(int y = 0; y < 3; y++) b[y] = a[y];
  138.                         b[i] = a[i] - add; b[j] = p[x];
  139.                         if(f[x][b[i]] == -1) {
  140.                             f[x][b[i]] = g + 1;
  141.                             q.push(make_pair(x, b[i]));
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.  
  148.         int ans = 1e9;
  149.         for(int i = 0; i <= n; i++) {
  150.             for(int j = 0; j <= v; j++) {
  151.                 if(f[i][j] == -1) continue;
  152.                 if(p[i] == m || j == m || (v - p[i] - j == m)) ans = min(ans, f[i][j]);
  153.             }
  154.         }
  155.  
  156.         if(ans == 1e9) ans = -1;
  157.         cout << ans;
  158.     }
  159. }
  160.  
  161. int main()
  162. {
  163.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  164.     #ifndef ONLINE_JUDGE
  165.     freopen("input.txt", "r", stdin);
  166.     freopen("output.txt", "w", stdout);
  167.     #else
  168.     //online
  169.     #endif
  170.  
  171.     int tc = 1, ddd = 0;
  172.     // cin >> tc;
  173.     while(tc--) {
  174.         //ddd++;
  175.         //cout << "Case #" << ddd << ": ";
  176.         solve();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement