Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <ostream>
- #include <istream>
- #include <string>
- #include <random>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- #include <iomanip>
- #include <time.h>
- using namespace std;
- //#pragma GCC optimize("Ofast")
- #define int long long
- #define pb push_back
- #define all(a) (a).begin(), (a).end()
- #define ld long double
- #define pii pair<int, int>
- ostream& operator << (ostream& a, const vector<int> &b) {
- for (auto k : b) cout << k << " ";
- cout << "\n";
- return a;
- }
- #define LOCAL
- #ifdef LOCAL
- #define dbg(x) cout << #x << " : " << (x) << "\n";
- #else
- #define dbg(x)
- #endif
- inline void solve(const vector<int>&a, const int& n, const int& b) {
- int val = 0, steps = 0, help, cur;
- for (int i = 1; i <= b; i++) {
- help = i;
- cur = 0;
- for (int j = n - 1; j >= 0; j--) {
- cur += help / a[j];
- help %= a[j];
- }
- if (cur > steps) {
- steps = cur;
- val = i;
- }
- }
- cout << val << " " << steps << "\n";
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n;
- cin >> n;
- vector<int> a(n);
- for (int i = 0; i < n; i++) cin >> a[i];
- vector<pii> dp(n - 1); // first = max tickets, second - sum of tickets
- // TODO: if n == 1 - ans is b[i]
- if (n == 1) {
- int q;
- cin >> q;
- int b;
- while (q--) {
- cin >> b;
- cout << b << " " << b << "\n";
- }
- return 0;
- }
- dp[0].first = a[1] - 1;
- dp[0].second = a[1] - 1;
- for (int i = 1; i < n - 1; i++) {
- dp[i].first = dp[i - 1].first + (a[i + 1] - 1 - dp[i - 1].second) / a[i];
- dp[i].second = dp[i - 1].second + ((a[i + 1] - 1 - dp[i - 1].second) / a[i]) * a[i];
- }
- // for (int i = 0; i < n - 1; i++) {
- // printf("% 4d ", dp[i].first);
- // }
- // cout << "\n";
- // for (int i = 0; i < n - 1; i++) {
- // printf("% 4d ", dp[i].second);
- // }
- // cout << "\n";
- int q;
- cin >> q;
- int b;
- int l, r, m;
- while (q--) {
- cin >> b;
- if (b >= dp[n - 2].second) {
- // cout << "here1\n";
- cout << dp[n-2].second + ((b - dp[n - 2].second)/a[n - 1]) * a[n - 1] << " ";
- cout << dp[n - 2].first + (b - dp[n - 2].second) / a[n - 1] << "\n";
- } else if (b < a[1]) {
- // cout << "here2\n";
- cout << b << " " << b << "\n";
- } else {
- // cout << "here3\n";
- l = 0, r = n - 2;
- while (r - l > 1) {
- m = (l + r) / 2;
- if (dp[m].second <= b) l = m;
- else r = m;
- }
- cout << dp[l].second + ((b - dp[l].second)/a[l + 1])*a[l + 1] << " ";
- cout << dp[l].first + (b - dp[l].second) / a[l + 1] << "\n";
- }
- }
- }
- /*
- 4
- 1 2 7 11
- 1
- 11
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement