当前位置: 首页 > news >正文

【排列距离 / B】

康托展开详解_康托展开原理-CSDN博客

题目

TLE代码

#include <bits/stdc++.h>
using namespace std;
int main()
{int cntall = 1, m1, m2;string s = "abcdefghijklnopqr";while (next_permutation(s.begin(), s.end())){if (s == "aejcldbhpiogfqnkr")m1 = cntall;else if (s == "ncfjboqiealhkrpgd")m2 = cntall;cntall++;}cout << min(abs(m2 - m1), cntall - abs(m2 - m1));
}

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a1, a2;
LL f[20];
void get_f(int n)
{f[1] = 1;for (int i = 2; i <= n; i++){f[i] = i * f[i - 1];}
}
void canto(string str, LL &x)
{int n = str.size();for (int i = 0; i < n; i++){int temp = 0;for (int j = i + 1; j < n; j++){if (str[i] > str[j])temp++;}x += temp * f[n - 1 - i];}
}
int main()
{get_f(17);string s1 = "aejcldbhpiogfqnkr";string s2 = "ncfjboqiealhkrpgd";canto(s1, a1), canto(s2, a2);cout << min(abs(a1 - a2), f[17] - abs(a1 - a2));
}


http://www.mrgr.cn/news/34956.html

相关文章:

  • 大厂离职故事:创业路上,不怕犯错(电梯节能)
  • Python163邮箱发送:提升发送效率的技巧?
  • Excel名字查重筛选,查找重复内容原来这么简单
  • 视频监控相关笔记
  • 企业有了ELT就不需要ETL了?别被忽悠了
  • k8s中,pod生命周期,初始化容器,容器探针,事件处理函数,理解其设计思路及作用
  • SpringBoot | Maven快速上手
  • Webpack优化问题
  • 两数之和、三数之和、四数之和
  • 开源 AI 智能名片与 S2B2C 商城小程序:嫁接权威实现信任与增长
  • Spring MVC 拦截器总结
  • 解决银河麒麟操作系统在单用户模式下根分区只读的问题
  • flink 的 Barrier 对齐 的优劣详解:
  • 【C++掌中宝】深入理解函数重载:概念、规则与应用
  • 《现代畜牧兽医》是什么级别的期刊?是正规期刊吗?能评职称吗?
  • Spring Boot集成LiteFlow使用详解
  • Jetpack——Room
  • 水政渔政船、轮渡邮轮及作业船舶监控:视频监控在水利水资源保护中的应用
  • 程序员下班以后做什么副业合适?_35技术人员副业
  • async await执行顺序