• 贪心,队列,运算符重载,牛客:连环爆炸


    C-连环爆炸_第四届辽宁省大学生程序设计竞赛(正式赛)(重现赛)@兴安真人 (nowcoder.com)

    链接:登录—专业IT笔试面试备考平台_牛客网
    来源:牛客网
     

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 262144K,其他语言524288K
    64bit IO Format: %lld

    题目描述

    星穹铁道,启动!

    希儿打怪,对面是n个自爆怪。每个怪有ai​和bi​两个参数,ai​代表这个怪物的血量,bi​代表这个怪物身亡后自爆对所有单体怪物造成的伤害。


    你可以选择任意怪物,进行“手动消灭”,将其血量变为0。

    当一个怪物由于“手动消灭”或其他怪物的自爆,血量变为0及0以下,则该怪物被消灭,并对所有单体怪物造成bi​的伤害。

    问最少“手动消灭”几个怪物能消灭所有怪物。

    注意:这些怪是可以通过怪的自爆连续引爆的。

    输入描述:

    第一行一个正整数n,表示怪物的数量。
    
    接下来的n行,每行两个正整数ai​和bi​。(ai​代表这个怪物的血量,bi​代表这个怪物身亡后自爆对所有单体怪物造成的伤害)
    
    1≤n≤4000
    
    1≤ai≤1000000000
    
    1≤bi≤250000

    输出描述:

    输出一个正整数,表示能消灭所有怪物的最少使用“手动消灭”的次数。

    示例1

    输入

    复制5 4 2 6 2 7 2 8 2 10 2

    5
    4 2
    6 2
    7 2
    8 2
    10 2

    输出

    复制2

    2

    示例2

    输入

    复制5 4 2 6 2 7 100 8 2 10 2

    5
    4 2
    6 2
    7 100
    8 2
    10 2

    输出

    复制1

    1

    解析:

     

    1.首先,想明白杀怪的最优解。需要筛选出一定不能被炸死的怪(即所有怪爆炸伤害之和减去该怪的爆炸伤害<该怪的生命值),手动将它杀死,积累伤害值。这一步非常重要!!!!

    2.剩余的都是可以被炸死的怪,可以放心地按爆炸伤害从高到低的顺序杀(注意伤害相等时先杀生命值高的),每杀一个,积累伤害值,需要找到目前伤害可以炸死的怪物,因此想到还需要一个按生命值从小到大排序的数据结构。可以知道此时怪物死到哪里了,不需要死一个怪再判断其他的怪物是否有被炸死的。

     

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. using namespace std;
    15. typedef long long LL;
    16. const int N = 4e3 + 5;
    17. int n;
    18. struct node {
    19. LL a, b;
    20. int flg = 0;
    21. }arr[N];
    22. int cmp(const struct node& a, const struct node& b) {
    23. if (a.b == b.b)
    24. return a.a > b.a;
    25. return a.b > b.b;
    26. }
    27. int main() {
    28. cin >> n;
    29. LL num = 0;
    30. for (int i = 1; i <= n; i++) {
    31. scanf("%lld%lld", &arr[i].a, &arr[i].b);
    32. num += arr[i].b;
    33. }
    34. sort(arr + 1, arr + 1 + n, cmp);
    35. LL sum = 0,cnt=0;
    36. int ff = 0;
    37. for (int i = 1; i <= n; i++) {
    38. if (num - arr[i].b < arr[i].a) {
    39. sum += arr[i].b;
    40. arr[i].flg = 1;
    41. cnt++;
    42. }
    43. }
    44. ff = 1;
    45. for (int i = 1; i <= n; i++) {
    46. //cout << cnt << endl;
    47. while (ff) {
    48. ff = 0;
    49. for (int j = n; j > 0; j--) {
    50. if (arr[j].a <= sum && arr[j].flg == 0) {
    51. sum += arr[j].b;
    52. arr[j].flg = 1;
    53. ff = 1;
    54. }
    55. }
    56. }
    57. if (arr[i].flg == 0) {
    58. arr[i].flg = 1;
    59. sum += arr[i].b;
    60. cnt++;
    61. ff = 1;
    62. }
    63. }
    64. cout << cnt << endl;
    65. return 0;
    66. }

    队列版:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. using namespace std;
    15. typedef long long LL;
    16. const int N = 4e3 + 5;
    17. int n;
    18. LL s[N][2],fg[N];
    19. struct ss {
    20. int index;
    21. LL a, b;
    22. bool operator <(const ss& t)const {
    23. if (b == t.b)return a < t.a;
    24. return b < t.b;
    25. }
    26. };
    27. struct ll {
    28. int index;
    29. LL a, b;
    30. bool operator<(const ll& t)const {
    31. return a > t.a;
    32. }
    33. };
    34. priority_queueq1;
    35. priority_queueq2;
    36. int main() {
    37. cin >> n;
    38. int sum = 0;
    39. for (int i = 1; i <= n; i++) {
    40. scanf("%lld%lld",&s[i][0],&s[i][1]);
    41. sum += s[i][1];
    42. }
    43. int cnt = 0, su = 0;
    44. for (int i = 1; i <= n; i++) {
    45. fg[i] = 1;
    46. }
    47. for (int i = 1; i <= n; i++) {
    48. if (sum - s[i][1] < s[i][0]) {
    49. su += s[i][1];
    50. cnt++;
    51. fg[i] = 0;
    52. }
    53. else {
    54. q1.push({ i,s[i][0],s[i][1] });
    55. q2.push({ i,s[i][0],s[i][1] });
    56. }
    57. }
    58. while (!q1.empty() && !q2.empty()) {
    59. while (!q2.empty()&&su>=q2.top().a) {
    60. if(fg[q2.top().index])su += q2.top().b;
    61. fg[q2.top().index] = 0;
    62. q2.pop();
    63. }
    64. if (q2.empty())break;
    65. while (!q1.empty()&&fg[q1.top().index]==0) {
    66. q1.pop();
    67. }
    68. if (q1.empty())break;
    69. cnt++;
    70. su += q1.top().b;
    71. fg[q1.top().index] = 0;
    72. q1.pop();
    73. }
    74. cout << cnt << endl;
    75. return 0;
    76. }

  • 相关阅读:
    jenkins 从机连接主机显示 404 Not Found
    服务器内存故障预测居然可以这样做!
    ClassNotFoundException与NoClassDefFoundError
    COMSOL泰森多边形Voronoi图多孔骨架优化模型受力分析
    Vue3中使用Element-Plus分页组件
    matlab GPR高斯过程回归与股票价格预测
    MYSQL(事务+锁+MVCC+SQL执行流程)理解(2)
    6 张图告诉你 RocketMQ 是怎么保存偏移量的
    五子棋团队项目(大学适用)
    Apache配置ssl证书-实现https访问
  • 原文地址:https://blog.csdn.net/Landing_on_Mars/article/details/134299333