Beingamanforever

ITMO Step3 A: Looped Playlist

Oct 23rd, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  4. #define NeedForSpeed                  \
  5.     ios_base::sync_with_stdio(false); \
  6.     cin.tie(NULL);                    \
  7.     cout.tie(NULL);
  8. #define int long long
  9. #define all(x) (x).begin(), (x).end()
  10. typedef vector<int> vi;
  11. const int INF = 1e18;
  12.  
  13. void solve()
  14. {
  15.     int n, p;
  16.     cin >> n >> p;
  17.     vi a(n);
  18.     int sum = 0;
  19.     for (int i = 0; i < n; i++)
  20.     {
  21.         cin >> a[i];
  22.         sum += a[i];
  23.     }
  24.     int l = 0, r = 0, cur_sum = 0;
  25.     int min_length = INF, start = -1;
  26.     while (r < 2 * n)
  27.     {
  28.         cur_sum += a[r % n];
  29.         while (cur_sum >= p)
  30.         {
  31.             if (r - l + 1 < min_length)
  32.             {
  33.                 min_length = r - l + 1;
  34.                 start = l % n;
  35.             }
  36.             cur_sum -= a[l % n];
  37.             l++;
  38.         }
  39.         r++;
  40.     }
  41.     if (min_length == INF)
  42.     // means no subarray, and we have to take the whole array
  43.     {
  44.         int ans = p / sum;
  45.         ans *= n;
  46.         int actsum = (p / sum) * sum;
  47.         for (int i = 0; i < n; i++)
  48.         {
  49.             actsum += a[i];
  50.             if (actsum >= p)
  51.             {
  52.                 ans += i + 1;
  53.                 break;
  54.             }
  55.         }
  56.         // start from 1, and take the whole array
  57.         cout << 1 << " " << ans << endl;
  58.     }
  59.     else
  60.     {
  61.         cout << start + 1 << " " << min_length << endl;
  62.     }
  63. }
  64.  
  65. signed main()
  66. {
  67.     NeedForSpeed;
  68.     int t = 1;
  69.     while (t--)
  70.     {
  71.         solve();
  72.     }
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment