
题意:有i种物品,每种物品需要买k[i]个,然后商店会有m次特价出售,第j次为在di天出售第ti种物品,物品原价2元,特价1元,你每天上午可以获得一元,下午可以进行交易,求获取所有物品花费的最少时间为多少天?
思路:注意到,第i+1天可以,那么第i天一定可以,有单调性,所以使用二分答案.具体来说,二分日期x,在x内对每件商品,在其在x天内的最后时间用所有的钱买他,剩下的按照普通价买即可.
代码:
- #include
- #define int long long
- #define IOS ios::sync_with_stdio(false), cin.tie(0)
- #define ll long long
- // #define double long double
- #define ull unsigned long long
- #define PII pair
- #define PDI pair
- #define PDD pair
- #define debug(a) cout << #a << " = " << a << endl
- #define point(n) cout << fixed << setprecision(n)
- #define all(x) (x).begin(), (x).end()
- #define mem(x, y) memset((x), (y), sizeof(x))
- #define lbt(x) (x & (-x))
- #define SZ(x) ((x).size())
- #define inf 0x3f3f3f3f
- #define INF 0x3f3f3f3f3f3f3f3f
- namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
- using namespace std;
- const int N = 2e6 + 10;
- int n, m, t[N];
- vector<int> d[N], last[N], k;//last[i]表示在x天内最后销售时限为第i天的商品
- bool check(int x) {
- auto s = k;
- for (int i = 1; i <= n; ++i) last[i].clear();
- for (int i = 1; i <= n; ++i) {
- int pos = 0;
- for (int xx : d[i]) {
- if (xx > x) break;
- pos = max(pos, xx);
- }
- if (pos) last[pos].emplace_back(i);
- }
- int mon = 0;
- for (int i = 1; i <= x; ++i) {
- ++mon;
- for (int xx : last[i]) {
- if (mon > s[xx]) mon -= s[xx], s[xx] = 0;
- else s[xx] -= mon, mon = 0;
- }
- }
- for (int i = 1; i <= n; ++i)
- mon -= (s[i] << 1);
- return mon >= 0;
- }
- void solve() {
- qio >> n >> m;
- k = vector<int> (n + 1, 0);
- for (int i = 1; i <= n; ++i) qio >> k[i];
- for (int i = 1, x; i <= m; ++i) {
- qio >> x >> t[i];
- d[t[i]].emplace_back(x);
- }
- for (int i = 1; i <= n; ++i) sort(all(d[i]));
- int l = 0, r = 1e6;
- while (l < r) {
- int mid = l + r >> 1;
- if (check(mid)) r = mid;
- else l = mid + 1;
- }
- qio << l << "\n";
-
- }
- signed main() {
- int T = 1;
- // qio >> T;
- while (T--) solve();
- }