Codeforces Round #552 (Div. 3)D、E题解

£神魔★判官ぃ 2022-01-12 08:19 350阅读 0赞

D. Walking Robot

题意

机器人在一维坐标轴上从0走到x,中途可以在有光的地方可以选择给太阳能电池充电,每次移动都要消耗一单位电,蓄电池容量为a,太阳能电池容量为b,一开始都是满电,问机器人采取最佳策略最多可以走多远。

题解

直接贪心模拟即可,具体见代码注释

  1. 1 #define dbg(x) cout<<#x<<" = "<< (x)<< endl
  2. 2 #define IO std::ios::sync_with_stdio(0);
  3. 3 #include <bits/stdc++.h>
  4. 4 #define iter ::iterator
  5. 5 using namespace std;
  6. 6 typedef long long ll;
  7. 7 typedef pair<ll,ll>P;
  8. 8 #define pb push_back
  9. 9 #define se second
  10. 10 #define fi first
  11. 11 #define rs o<<1|1
  12. 12 #define ls o<<1
  13. 13 const ll inf=0x7fffffff;
  14. 14 const int N=3e5+10;
  15. 15 int n,x,y;
  16. 16 int a[N];
  17. 17 int main(){
  18. 18 IO;
  19. 19 cin>>n>>x>>y;
  20. 20 int tx=x,ty=y;
  21. 21 for(int i=1;i<=n;i++){
  22. 22 cin>>a[i];
  23. 23 }
  24. 24 int ans=0;
  25. 25 for(int i=1;i<=n;i++){
  26. 26 if(a[i]){
  27. //如果有光
  28. 27 if(x>0){
  29. //如果蓄电池有电
  30. 28 if(y==ty){
  31. //如果太阳能电池充满优先用太阳能电池
  32. 29 y--;
  33. 30 }
  34. 31 else{
  35. 32 x--;
  36. 33 if(y<ty)y++;
  37. 34 }
  38. 35 }
  39. 36 else{
  40. 37 if(y==0)break;//如果蓄电池没电太阳能电池也没电
  41. 38 y--;
  42. 39 }
  43. 40 }
  44. 41 else{
  45. //无光
  46. 42 if(y>0)y--;//如果太阳能电池有电优先用太阳能电池
  47. 43 else{
  48. 44 if(x==0)break;//如果蓄电池没电太阳能电池也没电
  49. 45 x--;
  50. 46 }
  51. 47 }
  52. 48 ans++;
  53. 49 }
  54. 50 cout<<ans<<endl;
  55. 51 }

E. Two Teams

题意

两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了。

题解

线段树维护总区间最大值,取走数相当于更新对应区间最大值为0,注意暴力更新答案会T,有一个比较取巧的方法是提前算一下部分数的离他最远的连续被取走的数的位置,然后在更新答案的时候就可以一次跳远一点,我之前就是因为一步一步跳T37了。

  1. 1 #define dbg(x) cout<<#x<<" = "<< (x)<< endl
  2. 2 #define IO std::ios::sync_with_stdio(0);
  3. 3 #include <bits/stdc++.h>
  4. 4 #define iter ::iterator
  5. 5 using namespace std;
  6. 6 typedef long long ll;
  7. 7 typedef pair<ll,ll>P;
  8. 8 #define pb push_back
  9. 9 #define se second
  10. 10 #define fi first
  11. 11 #define rs o<<1|1
  12. 12 #define ls o<<1
  13. 13 const ll inf=0x7fffffff;
  14. 14 const int N=2e5+10;
  15. 15 int n,x,y;
  16. 16 int a[N],b[N],c[N];
  17. 17 int maxv[N*4],lazy[N*4];
  18. 18 void push(int o){
  19. 19 maxv[o]=max(maxv[ls],maxv[rs]);
  20. 20 }
  21. 21 void down(int o){
  22. 22 if(lazy[o]){
  23. 23 maxv[o]=0;
  24. 24 lazy[ls]=lazy[rs]=1;
  25. 25 maxv[ls]=maxv[rs]=0;
  26. 26 }
  27. 27 }
  28. 28 void build(int o,int l,int r){
  29. 29 if(l==r){
  30. 30 cin>>maxv[o];
  31. 31 return;
  32. 32 }
  33. 33 int m=(l+r)/2;
  34. 34 build(ls,l,m);
  35. 35 build(rs,m+1,r);
  36. 36 push(o);
  37. 37 }
  38. 38 void up(int o,int l,int r,int ql,int qr){
  39. 39 if(l>=ql&&r<=qr){
  40. 40 maxv[o]=0;
  41. 41 lazy[o]=1;
  42. 42 return;
  43. 43 }
  44. 44 down(o);
  45. 45 int m=(l+r)/2;
  46. 46 if(ql<=m)up(ls,l,m,ql,qr);
  47. 47 if(qr>m)up(rs,m+1,r,ql,qr);
  48. 48 push(o);
  49. 49 }
  50. 50 int qu(int o,int l,int r){
  51. 51 if(l==r){
  52. 52 return l;
  53. 53 }
  54. 54 down(o);
  55. 55 int m=(l+r)/2;
  56. 56 if(maxv[ls]==maxv[1]){
  57. 57 return qu(ls,l,m);
  58. 58 }
  59. 59 else return qu(rs,m+1,r);
  60. 60 }
  61. 61 int k;
  62. 62 int main(){
  63. 63 IO;
  64. 64 cin>>n>>k;
  65. 65 build(1,1,n);
  66. 66 int g=0;
  67. 67 for(int i=1;i<=n;i++){
  68. 68 b[i]=c[i]=i;
  69. 69 }
  70. 70 while(1){
  71. 71 if(maxv[1]==0)break;
  72. 72 g++;
  73. 73 int h=g%2?1:2;
  74. 74 int p=qu(1,1,n);
  75. 75 int ql=0,qr=n+10;
  76. 76 for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
  77. 77 ql=i;
  78. 78 i=c[i];
  79. 79 if(a[i]!=0)continue;
  80. 80 a[i]=h;
  81. 81 cnt++;
  82. 82
  83. 83 }
  84. 84 for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
  85. 85 qr=i;
  86. 86 i=b[i];
  87. 87 if(a[i]!=0)continue;
  88. 88 a[i]=h;
  89. 89 cnt++;
  90. 90
  91. 91 }
  92. 92 for(int i=ql;i<=min(ql+10,qr);i++){
  93. 93 b[i]=qr;
  94. 94 }
  95. 95 for(int i=qr;i>=max(qr-10,ql);i--){
  96. 96 c[i]=ql;
  97. 97 }
  98. 98 up(1,1,n,max(1,ql),min(qr,n));
  99. 99 }
  100. 100 for(int i=1;i<=n;i++)cout<<a[i];
  101. 101 }

转载于:https://www.cnblogs.com/ccsu-kid/p/10723471.html

发表评论

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

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

相关阅读