• CF1165F2(二分答案)


     题意:有i种物品,每种物品需要买k[i]个,然后商店会有m次特价出售,第j次为在di天出售第ti种物品,物品原价2元,特价1元,你每天上午可以获得一元,下午可以进行交易,求获取所有物品花费的最少时间为多少天?

    思路:注意到,第i+1天可以,那么第i天一定可以,有单调性,所以使用二分答案.具体来说,二分日期x,在x内对每件商品,在其在x天内的最后时间用所有的钱买他,剩下的按照普通价买即可.

    代码:

    1. #include
    2. #define int long long
    3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    4. #define ll long long
    5. // #define double long double
    6. #define ull unsigned long long
    7. #define PII pair
    8. #define PDI pair
    9. #define PDD pair
    10. #define debug(a) cout << #a << " = " << a << endl
    11. #define point(n) cout << fixed << setprecision(n)
    12. #define all(x) (x).begin(), (x).end()
    13. #define mem(x, y) memset((x), (y), sizeof(x))
    14. #define lbt(x) (x & (-x))
    15. #define SZ(x) ((x).size())
    16. #define inf 0x3f3f3f3f
    17. #define INF 0x3f3f3f3f3f3f3f3f
    18. 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;
    19. using namespace std;
    20. const int N = 2e6 + 10;
    21. int n, m, t[N];
    22. vector<int> d[N], last[N], k;//last[i]表示在x天内最后销售时限为第i天的商品
    23. bool check(int x) {
    24. auto s = k;
    25. for (int i = 1; i <= n; ++i) last[i].clear();
    26. for (int i = 1; i <= n; ++i) {
    27. int pos = 0;
    28. for (int xx : d[i]) {
    29. if (xx > x) break;
    30. pos = max(pos, xx);
    31. }
    32. if (pos) last[pos].emplace_back(i);
    33. }
    34. int mon = 0;
    35. for (int i = 1; i <= x; ++i) {
    36. ++mon;
    37. for (int xx : last[i]) {
    38. if (mon > s[xx]) mon -= s[xx], s[xx] = 0;
    39. else s[xx] -= mon, mon = 0;
    40. }
    41. }
    42. for (int i = 1; i <= n; ++i)
    43. mon -= (s[i] << 1);
    44. return mon >= 0;
    45. }
    46. void solve() {
    47. qio >> n >> m;
    48. k = vector<int> (n + 1, 0);
    49. for (int i = 1; i <= n; ++i) qio >> k[i];
    50. for (int i = 1, x; i <= m; ++i) {
    51. qio >> x >> t[i];
    52. d[t[i]].emplace_back(x);
    53. }
    54. for (int i = 1; i <= n; ++i) sort(all(d[i]));
    55. int l = 0, r = 1e6;
    56. while (l < r) {
    57. int mid = l + r >> 1;
    58. if (check(mid)) r = mid;
    59. else l = mid + 1;
    60. }
    61. qio << l << "\n";
    62. }
    63. signed main() {
    64. int T = 1;
    65. // qio >> T;
    66. while (T--) solve();
    67. }

  • 相关阅读:
    浅谈防勒索病毒方案之主机加固
    vs2017/2019串口Qt Serial Port/modbus使用报错
    饮酒后回家途中死亡,同饮者是否担责?
    SQLite3数据类型
    问题解决:MapReduce输出结果乱码(Eclipse)
    什么是串行通信?
    初识设计模式 - 访问者模式
    文心一言 VS 讯飞星火 VS chatgpt (113)-- 算法导论10.2 5题
    c++11 智能指针 (std::shared_ptr)(六)
    ffmpeg之 一张/多张图片合成视频
  • 原文地址:https://blog.csdn.net/CK1513710764/article/details/126176473