Slay the Dragon 二分 + 贪心

柔情只为你懂 2022-09-12 04:49 230阅读 0赞

二分+贪心找到最小花费,主要是边界有点麻烦

  1. // Problem: C. Slay the Dragon
  2. // Contest: Codeforces - Educational Codeforces Round 114 (Rated for Div. 2)
  3. // URL: https://codeforces.com/contest/1574/problem/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<string>
  11. #include<ctime>
  12. #include<cmath>
  13. #include<cstring>
  14. #include<algorithm>
  15. #include<stack>
  16. #include<climits>
  17. #include<queue>
  18. #include<map>
  19. #include<set>
  20. #include<sstream>
  21. #include<cassert>
  22. #include<bitset>
  23. #include<list>
  24. #include<unordered_map>
  25. using namespace std;
  26. #define ff first
  27. #define ss second
  28. #define lowbit(x) (x&-x)
  29. #define pf(a) printf("%d\n",a)
  30. #define mem(x,y) memset(x,y,sizeof(x))
  31. #define dbg(x) cout << #x << " = " << x << endl
  32. #define rep(i,l,r) for(int i = l; i <= r; i++)
  33. #define fep(i,a,b) for(int i=b; i>=a; --i)
  34. typedef pair<int,int> PII;
  35. typedef long long ll;
  36. typedef unsigned long long ull;
  37. const int N=2e5+100;
  38. int n, m;
  39. ll a[N];
  40. vector<int> v[N];
  41. void solve()
  42. {
  43. scanf("%d", &n);
  44. ll sum = 0;
  45. a[0] = 1e18;
  46. rep(i,1,n)
  47. {
  48. scanf("%lld", &a[i]);
  49. sum += a[i];
  50. }
  51. sort(a+1, a+1+n);
  52. scanf("%d", &m);
  53. while(m--)
  54. {
  55. ll defen, atta, ans = 0;
  56. scanf("%lld %lld", &defen, &atta);
  57. int t = lower_bound(a+1, a+1+n, defen) - a;
  58. if(a[t] == defen)
  59. {
  60. if(sum-a[t]>=atta) ans = 0;
  61. else ans = abs(a[t]+atta-sum);
  62. }
  63. else if(t > n)
  64. {
  65. ans += defen - a[n];
  66. if(sum - a[n] < atta) ans += (atta - sum + a[n]);
  67. }
  68. else
  69. {
  70. ll res = 0;
  71. res += abs(a[t-1]-defen);
  72. ll s1 = sum - a[t-1];
  73. if(s1 < atta) res += atta - s1;
  74. ll xx = 0;
  75. if(a[t] >= defen) {
  76. ll s3 = sum - a[t];
  77. if(s3 < atta) xx += atta - s3;
  78. }
  79. ll cnt = 0;
  80. cnt += abs(a[t]-defen);
  81. ll s2 = sum - a[t];
  82. if(s2 < atta) cnt += atta - s2;
  83. ans = min({ res, cnt,xx});
  84. }
  85. printf("%lld\n", ans);
  86. //cout << " t = " << t << " " << ans << endl;
  87. //cout << t << endl;
  88. }
  89. }
  90. int main()
  91. {
  92. solve();
  93. return 0;
  94. }

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读

    相关 uva 1623——Enter The Dragon

    题意:有n个装满水的湖,可以预知将来m天下雨情况,每次下满一个湖,或者不下,不下雨的时候可以让某个湖变干,问是否存在一种方案使得每次下雨之前湖总是干的。 思路:贪心

    相关 BZOJ 5467 Slay the Spire

    BZOJ 5467 Slay the Spire 我的概率基础也太差了.jpg 大概就是这样,因为强化牌至少翻倍,所以打出的牌必定是全部的强化牌或者$k-1$个强