• (AtCoder Beginner Contest 321)(退背包,二叉树)


    A.直接暴力即可

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N =2e5+10;
    4. #define int long long
    5. typedef long long LL;
    6. typedef pair<int, int> PII;
    7. int n,m;
    8. void solve(){
    9. string s;
    10. cin>>s;
    11. for(int i=1;i<s.size();i++){
    12. int x=s[i-1]-'0';
    13. int y=s[i]-'0';
    14. if(x<=y){
    15. cout<<"No\n";
    16. return ;
    17. }
    18. }
    19. cout<<"Yes\n";
    20. }
    21. signed main(){
    22. cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    23. int t=1;
    24. // cin>>t;
    25. while(t--) solve();
    26. }

    B.也是暴力枚举每个分数即可

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N =2e5+10;
    4. #define int long long
    5. typedef long long LL;
    6. typedef pair<int, int> PII;
    7. int n,m;
    8. int a[N];
    9. void solve(){
    10. cin>>n>>m;
    11. for(int i=1;i<n;i++){
    12. cin>>a[i];
    13. }
    14. int res=INT_MAX;
    15. for(int j=0;j<=100;j++){
    16. vector<int> b;
    17. for(int i=1;i<n;i++) b.push_back(a[i]);
    18. b.push_back(j);
    19. sort(b.begin(),b.end());
    20. int now=0;
    21. for(int i=1;i<b.size()-1;i++) now+=b[i];
    22. if(now>=m){
    23. res=min(res,j);
    24. }
    25. }
    26. if(res==INT_MAX){
    27. cout<<-1;
    28. return ;
    29. }
    30. cout<<res;
    31. }
    32. signed main(){
    33. cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    34. int t=1;
    35. // cin>>t;
    36. while(t--) solve();
    37. }

    C.我二分+数位dp了

    直接二分当前数的范围内有多少个 “321”的数的个数

    1. import random
    2. import sys
    3. import os
    4. import math
    5. from collections import Counter, defaultdict, deque
    6. from functools import lru_cache, reduce,cache
    7. from itertools import accumulate, combinations, permutations
    8. from heapq import nsmallest, nlargest, heapify, heappop, heappush
    9. from io import BytesIO, IOBase
    10. from copy import deepcopy
    11. import threading
    12. import bisect
    13. BUFSIZE = 4096
    14. class FastIO(IOBase):
    15. newlines = 0
    16. def __init__(self, file):
    17. self._fd = file.fileno()
    18. self.buffer = BytesIO()
    19. self.writable = "x" in file.mode or "r" not in file.mode
    20. self.write = self.buffer.write if self.writable else None
    21. def read(self):
    22. while True:
    23. b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
    24. if not b:
    25. break
    26. ptr = self.buffer.tell()
    27. self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
    28. self.newlines = 0
    29. return self.buffer.read()
    30. def readline(self):
    31. while self.newlines == 0:
    32. b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
    33. self.newlines = b.count(b"\n") + (not b)
    34. ptr = self.buffer.tell()
    35. self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
    36. self.newlines -= 1
    37. return self.buffer.readline()
    38. def flush(self):
    39. if self.writable:
    40. os.write(self._fd, self.buffer.getvalue())
    41. self.buffer.truncate(0), self.buffer.seek(0)
    42. class IOWrapper(IOBase):
    43. def __init__(self, file):
    44. self.buffer = FastIO(file)
    45. self.flush = self.buffer.flush
    46. self.writable = self.buffer.writable
    47. self.write = lambda s: self.buffer.write(s.encode("ascii"))
    48. self.read = lambda: self.buffer.read().decode("ascii")
    49. self.readline = lambda: self.buffer.readline().decode("ascii")
    50. sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
    51. input = lambda: sys.stdin.readline().rstrip("\r\n")
    52. def I():
    53. return input()
    54. def II():
    55. return int(input())
    56. def MI():
    57. return map(int, input().split())
    58. def LI():
    59. return list(input().split())
    60. def LII():
    61. return list(map(int, input().split()))
    62. def GMI():
    63. return map(lambda x: int(x) - 1, input().split())
    64. def LGMI():
    65. return list(map(lambda x: int(x) - 1, input().split()))
    66. def solve():
    67. k = II()
    68. @cache
    69. def dfs(i: int, limit: bool, num: bool, pre: int,s:str) -> int:
    70. if i == len(s):
    71. return int(num)
    72. res = 0
    73. if not num: res = dfs(i + 1, False, False, 10,s)
    74. up = int(s[i]) if limit else 9
    75. di = 0
    76. if not num: di = 1
    77. for d in range(di, up + 1):
    78. if pre > d:
    79. res += dfs(i + 1, limit and d == up, True, d,s)
    80. return res
    81. l, r = 1, 10 ** 18
    82. while l < r:
    83. mid = (l + r) >> 1
    84. if dfs(0, True, False, 10,str(mid)) >= k:
    85. r = mid
    86. else:
    87. l = mid + 1
    88. print(l)
    89. if __name__ == '__main__':
    90. for _ in range(1):
    91. solve()

    D.

    每个ai都要乘每个bi

    排序后双指针维护当前ai+bi<=k的最有边界即可

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N =2e5+10;
    4. #define int long long
    5. typedef long long LL;
    6. typedef pair<int, int> PII;
    7. int n,m,k;
    8. int a[N],b[N];
    9. void solve(){
    10. cin>>n>>m>>k;
    11. for(int i=1;i<=n;i++)
    12. {
    13. cin>>a[i];
    14. }
    15. for(int j=1;j<=m;j++) cin>>b[j];
    16. sort(a+1,a+1+n);
    17. sort(b+1,b+1+m);
    18. int res=0;
    19. int now=0;
    20. for(int j=1;j<=m;j++) now+=b[j];
    21. int idx=m;
    22. for(int i=1;i<=n;i++)
    23. {
    24. while(idx>=1&&a[i]+b[idx]>=k)
    25. {
    26. now-=b[idx];
    27. idx--;
    28. }
    29. // cout<<idx<<"\n";
    30. res+=idx*a[i]+now+(m-idx)*k;
    31. }
    32. cout<<res;
    33. }
    34. signed main(){
    35. cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    36. int t=1;
    37. // cin>>t;
    38. while(t--) solve();
    39. }

    E.

    直接get函数就是找以当前x为根节点的子树下面距离为k的有多少个数,最大值是n

    那么最左边界是x<

    然后往父节点跳即可,边跳边计算父节点的另一个儿子节点

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N =2e5+10,mod= 998244353;
    4. #define int long long
    5. typedef long long LL;
    6. typedef pair<int, int> PII;
    7. void solve()
    8. {
    9. int n,x,k;
    10. int res=0;
    11. auto get=[&](int x,int k,int n){
    12. if(k>=64) return 0ll;
    13. if(k<0) return 0ll;
    14. __int128 num=1ll<<k,l=x;
    15. l=l<<k;
    16. __int128 r=l+num-1;
    17. if(l>n) return 0ll;
    18. LL res=min(r,(__int128)n)-l+1;
    19. return res;
    20. };
    21. cin>>n>>x>>k;
    22. if(k)
    23. {
    24. res+=get(x,k,n);
    25. }
    26. while(x>1&&k>0){
    27. k--;
    28. res+=get((x^1),k-1,n);
    29. x/=2;
    30. }
    31. if(k==0&&x){
    32. res++;
    33. }
    34. cout<<res<<"\n";
    35. }
    36. signed main(){
    37. cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    38. int t=1;
    39. cin>>t;
    40. while(t--) solve();
    41. }

    F:退背包问题

    添加就是正常的01背包

    然后删除就是

    维护g数组

    状态是,不选当前球,体积为i的方案数

    那么g[i]=f[i]-g[i-x]

    考虑容斥原理

    不选当前球体积为i=选+不选当前球体积为i-选当前球体积为i的方案数

    =f[i]-g[i-x]

    g[i-x]可以理解为选了这个球后,剩下的 i−x�−�就是由不选该球的方案数组成

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N =2e5+10,mod= 998244353;
    4. #define int long long
    5. typedef long long LL;
    6. typedef pair<int, int> PII;
    7. int n,m;
    8. int a[N];
    9. int dep[N];
    10. void solve()
    11. {
    12. int q,k;
    13. cin>>q>>k;
    14. vector<int> f(k+1,0);
    15. f[0]=1;
    16. while(q--){
    17. string op;
    18. int x;cin>>op>>x;
    19. if(op[0]=='+'){
    20. for(int i=k;i>=x;i--){
    21. f[i]+=f[i-x];
    22. f[i]%=mod;
    23. }
    24. }
    25. else
    26. {
    27. //不选该球和为j的方案书
    28. //选和不选
    29. vector<int> g(k+1,0);
    30. for(int i=0;i<=k;i++){
    31. if(i<x){
    32. g[i]=f[i];
    33. }
    34. else{
    35. g[i]=f[i]-g[i-x];
    36. if(g[i]<0) g[i]+=mod;
    37. }
    38. }
    39. f.swap(g);
    40. }
    41. cout<<f[k]<<"\n";
    42. }
    43. }
    44. signed main(){
    45. cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    46. int t=1;
    47. // cin>>t;
    48. while(t--) solve();
    49. }

  • 相关阅读:
    Busybox实践2:分析busybox文件链接原理并编程模拟实现自己的busybox文件
    SpringBoot启动流程
    MySQL表加字段可为null导致ArrayIndexOutOfBoundsException报错问题记录
    HTML基本讲解与使用
    PinnedSectionItemDecoration - 一个强大的粘性标签库
    About 9.25 This Week
    IMX6-->8 RTC同步程序涉及到的知识点
    Web前端-Vue2+Vue3基础入门到实战项目-Day1(初始Vue, Vue指令, 小黑记事本)
    【前端】前端三要素之BOM
    【Servlet】超详细开发步骤|在idea上配置Tomcat|网页显示当前系统时间
  • 原文地址:https://blog.csdn.net/qq_61657632/article/details/133254441