首页 > 其他分享 >2024.7.1

2024.7.1

时间:2024-07-02 15:22:42浏览次数:1  
标签:10 2024.7 int 复杂度 long ++ dp

转盘锁

可以把序列看出一个个元素, + 1, - 1 看成转移, 这就成了一个 bfs

还可以发现, \(a_0, a_1, a_2, a_3 \to b_0, b_1, b_2, b_3 = 0, 0, 0, 0 \to b_0 - a_0, b_1 - a_1, b_2 - a_2, b_3 - a_3\)

状态数只有 \(10^4\)

#include<bits/stdc++.h>

using namespace std;

unordered_map<string, int>dist;
unordered_map<string, bool>vis;
queue<string>q;
string t, s;
int a[10], b[10], T;
char c;

void C(string t, int x){
  for(auto &c : t){
    if(c < '0'){
      c += 10;
    }
    if(c > '9'){
      c -= 10;
    }
  }
  if(vis[t]){
    return;
  }
  vis[t] = 1;
  dist[t] = x;
  q.push(t);
}

void bfs(){
  C("0000", 0);
  while(q.size()){
    s = q.front();
    q.pop();
    for(int l = 0; l < 4; ++l){
      t = s;
      for(int r = l; r < 4; ++r){
        t[r]--;
        C(t, dist[s] + 1);
      }
      t = s;
      for(int r = l; r < 4; ++r){
        t[r]++;
        C(t, dist[s] + 1);
      }
    }
  }
}

void S(){
  for(int i = 1; i <= 4; i++){
    cin >> c;
    a[i] = c - '0';
  }
  s = "";
  for(int i = 1; i <= 4; ++i){
    cin >> c;
    b[i] = c - '0';
    s += char((b[i] - a[i] + 10) % 10 + '0');
  }
  cout << dist[s] << '\n';
}

int main(){
  freopen("lock.in", "r", stdin);
  freopen("lock.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0);
  bfs();
  for(cin >> T; T--; S()){
  }
  return 0;
}

时间复杂度 \(O(10^4 \cdot !4 \cdot 4 + 4T)\)
空间复杂度 \(O(10^4 \cdot !4 \cdot 4)\)

多姿多彩

对于每一个 \(i\), 所有的字符都会和其它匹配。

所以只需要统计所有字符出现次数 \(cnt_i\), 每个字符代价为 \(cnt_i \dots (n - cnt_i)\)

#include<bits/stdc++.h>

using namespace std;

long long ans;
int n, cnt[30];
char c;

int main(){
  freopen("diverse.in", "r", stdin);
  freopen("diverse.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> c;
    cnt[c - 'a']++;
  }
  for(int i = 0; i < 26; ++i){
    ans += 1ll * cnt[i] * (n - cnt[i]);
  }
  cout << ans * n;
  return 0;
}

时间复杂度 \(O(n)\)

空间复杂度 \(O(26)\)

黑白树

树形 DP

状态 \((i, j, sum)\) 表示在 \(i\) 的子树内做 \(\bmod 2 = j\) 次操作的黑节点最大值为 \(sum\)

合并子树

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int n, c[N], dp[N][4], u, v, f[4];
vector<int>g[N];

void dfs(int x, int fa){
  if(!(g[x].size() - (x != 1))){
    dp[x][1] = !c[x];
    dp[x][0] = c[x];
  }
  else{
    dp[x][0] = -1e9;
    dp[x][1] = -1e9;
  }
  for(auto v : g[x]){
    if(v != fa){
      dfs(v, x);
      f[0] = dp[v][0], f[1] = dp[v][1];
      for(int i = 0; i <= 1; ++i){
        for(int j = 0; j <= 1; ++j){
          f[i ^ j] = max(f[i ^ j], dp[x][i] + dp[v][j]);
        }
      }
      dp[x][0] = f[0], dp[x][1] = f[1];
    }
  }
  if((g[x].size() - (x != 1))){
    dp[x][0] += c[x], dp[x][1] += !c[x];
  }
}

int main(){
  freopen("mono.in", "r", stdin);
  freopen("mono.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> c[i];
  }
  for(int i = 1; i < n; ++i){
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  cout << max(dp[1][1], dp[1][0]);
  return 0;
}

时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)

苟延残喘

一开始极差为 \(a_n - a_1\)

第二次极差最大为 \(a_n - a_1\)

\(\dots\)

所以极差 \(\le 10^9\)

若最小值 \(\ge 10^9 + 7\), 整体减 \(10^9 + 7\), 即能保值不爆 long long, 又等价于取模, 还是对的。

#include<bits/stdc++.h>

using namespace std;

const int N = 3005, mod = 1e9 + 7;

struct Node{
  long long x;
  int i, j;
};

struct cmp{
  bool operator()(const Node &i, const Node &j){
    return i.x > j.x;
  }
};

priority_queue<Node, vector<Node>, cmp>pq;

int n, sum, flag;
long long a[N], b[N], minx;

int main(){
  freopen("weak.in", "r", stdin);
  freopen("weak.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for(int i = n; i > 1; --i){
    for(; pq.size(); pq.pop()){
    }
    for(int k = 1; k < n; ++k){
      pq.push({a[k] + a[k + 1], k, k + 1});
    }
    minx = 1e9;
    for(int k = 1; k < n; ++k){
      auto [sum, id, j] = pq.top();
      pq.pop();
      b[k] = sum;
      if(j != n){
        j++;
        pq.push({a[id] + a[j], id, j});
      }
      minx = min(minx, b[k] / mod);
    }
    n--;
    for(int k = 1; k <= n; ++k){
      a[k] = b[k] - minx * mod;
    }
  }
  cout << a[n];
  return 0;
}

时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)

标签:10,2024.7,int,复杂度,long,++,dp
From: https://www.cnblogs.com/liuyichen0401/p/18278097

相关文章

  • 2024.7.2
    党同伐异可以发现,每次只会是\(a_i\)最大或者\(a_i\)最小的人被淘汰,所以留下的肯定是从小到大排序后的一段区间。还可以发现一个单调性,越靠近左边就越不可能票左边,所以可以通过二分求出左右两边各被票了多少。#include<bits/stdc++.h>usingnamespacestd;const......
  • 2024.7.2 集训
    ###数位DP1.记录:1.是否顶上限;2.是否当前填了的都是前导$0$;3.当前位是否是从左往右数第一位。(2和3是两种做法,2是在Query里只调用一次DFS,3是在Query里枚举第一个非$0$位调用多次DFS)。2.记忆化的数组可以不用记所有内容。3.注意DFS返回时要返回res,而不是记......
  • 云原生周刊:Argo Rollouts 支持 Kubernetes Gateway API 1.0 | 2024.7.1
    开源项目KubetoolsRecommenderSystemKubetoolsRecommenderSystem(Krs)是一个基于GenAI的工具,用于帮助管理和优化Kubernetes集群。buoybuoy是Kubernetes的声明式TUI仪表板。你可以在JSON文件中定义仪表板,它将从Kubernetes集群中获取信息并构建仪表板,以便在......
  • 2024.7.1 - 7.15
    Question1-[ABC360G]SuitableEditforLIS给定一个长度为\(n\)的序列\(A\),你可以执行如下操作恰好一次,最大化LIS的长度:选定一个下标\(x\)满足\(1\leqx\leqn\),选定一个任意的整数\(y\),然后将\(A_x\)替换为\(y\)。\(1\leqn\leq2\times10^5,1\leqA_i\le......
  • 记录:2024.7.1,VMware17免费后的安装方法
    省流:下载地址:VMware17.5.2forLinux:https://www.123pan.com/s/RBdkTd-1rM3d.htmlVMware17.5.2forWindows:https://www.123pan.com/s/RBdkTd-xrM3d.htmlVMware在2024年5月13宣布VMwarepro免费给个人用户使用,并且所有VMware支持都被迁移到博通网站VMwareFusionPro:......
  • 2024.7 - 做题记录与方法总结
    2024/07/01AtCoderBeginnerContest360E-RandomSwapsofBalls期望\(dp\)题问题陈述有\(N-1\)个白球和一个黑球。这些\(N\)个球排成一排,黑球最初位于最左边的位置。高桥正好要进行下面的操作\(K\)次。在\(1\)和\(N\)之间均匀随机地选择一个整数,包括两......
  • 2024.7.1 之后的做题小记
    7.1P7124[Ynoi2008]stcm维护一个\(O(n\logn)\)级别的子树补不删除莫队。Solution1:考虑菊花图,忽略根节点,一个显然的做法是把这些节点扔进线段树,然后遍历某个节点时候就把它的兄弟节点内所有点加进来。这个做法是线段树所有节点大小和即\(O(n\logn)\)。然后在一条链上......
  • 2024.7.1 初识Linux
    1、Linux安装:(1)VmwareWorkstation安装(2)Centos7系统安装(3)使用Mobaxterm远程操控Linux虚拟机2、Linux命令(1)ipaddress查看本机的ip地址(2)cal查看日历用法:cal[选项][[[日]月]年]选项:-1,--one只显示当前月份(默认)-3,--three显示上个月、当月和下......
  • 2024.7~8 训练日记
    \(\color{grey}\bigstar\)可以秒杀的题。\(\color{green}\bigstar\)思考一会儿后可以秒的题。\(\color{blue}\bigstar\)需要较长时间思考的题。\(\color{#F1C40F}\bigstar\)看题解、稍加指点就会做的题。\(\color{red}\bigstar\)看题解后需要较长时间消化,甚至现在都没有......
  • 【周考】Round1 2024.7.6
    SummaryScore:\(100+90+0+50+4=244\)T1减法操作考虑对\(n\)分奇偶讨论:偶数:显然最小质因子为\(2\),而每次减\(2\)后仍是偶数。所以偶数一定进行了\(\dfracn2\)次操作;奇数:因为是奇数,所以最小质因子一定也是奇数,减去后则变为偶数,接着可以转化为偶数处理。code......