• 算法训练.


    一.扩散

    题解:

    计算点之间的距离,然后对图进行处理即可,这个数据规模较小,因此我使用了floyd,还有最小生成树和二份答案加并查集的写法;

    代码:

    1. #include <iostream>
    2. #include <cstring>
    3. #include <cmath>
    4. #include <iomanip>
    5. #include <algorithm>
    6. #include <cstdio>
    7. #include <stack>
    8. #include <queue>
    9. #include<set>
    10. #include <string>
    11. #include<map>
    12. using namespace std;
    13. using ll = long long;
    14. using ull = unsigned long long;
    15. #define up(i, h, n) for (int i = h; i <= n; i++)
    16. #define down(i, h, n) for(int i = h; i >= n; i--)
    17. #define wh(x) while(x--)
    18. #define node struct node
    19. #define it ::iterator
    20. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    21. constexpr int MaxN = 200005;
    22. constexpr int MaxM = 10005;
    23. constexpr int mod = 1e9 + 7;
    24. constexpr int inf = 0x7fffffff;
    25. constexpr double value = 1e-10;
    26. int main() {
    27. int n;
    28. int x[55], y[55];
    29. int e[55][55];
    30. cin >> n;
    31. up(i, 1, n) {
    32. cin >> x[i] >> y[i];
    33. }
    34. up(i, 1, n) {
    35. up(j, 1, n) {
    36. e[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
    37. }
    38. }
    39. up(k, 1, n) {
    40. up(i, 1, n) {
    41. up(j, 1, n) {
    42. e[i][j] = min(max(e[i][k], e[k][j]), e[i][j]);
    43. }
    44. }
    45. }
    46. int ans = 0;
    47. up(i, 1, n) {
    48. up(j, 1, n) {
    49. ans = max(ans, e[i][j]);
    50. }
    51. }
    52. cout << int(ceil(ans * 1.0 / 2));
    53. return 0;
    54. }

    二.三分 函数

    题解:

    三分模版,三分和二分的原理相同,不同的是,三分对于已知的l和r,会有两个三等分点的值mid;不过这里值得注意的是一些差值,需要误差很小;

    代码:

    1. #include <iostream>
    2. #include <cstring>
    3. #include <cmath>
    4. #include <iomanip>
    5. #include <algorithm>
    6. #include <cstdio>
    7. #include <stack>
    8. #include <queue>
    9. #include<set>
    10. #include <string>
    11. #include<map>
    12. using namespace std;
    13. using ll = long long;
    14. using ull = unsigned long long;
    15. #define up(i, h, n) for (int i = h; i <= n; i++)
    16. #define down(i, h, n) for(int i = h; i >= n; i--)
    17. #define wh(x) while(x--)
    18. #define node struct node
    19. #define it ::iterator
    20. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    21. constexpr int MaxN = 10005;
    22. constexpr int MaxM = 10005;
    23. constexpr int mod = 1e9 + 7;
    24. constexpr int inf = 0x7fffffff;
    25. constexpr double value = 1e-10;
    26. node{
    27. double a,b,c;
    28. }e[MaxN];
    29. int t, n;
    30. double check(double x) {
    31. double max1 = e[0].a * x * x + e[0].b * x + e[0].c;
    32. up(i, 1, n - 1) {
    33. max1 = max(e[i].a * x * x + e[i].b * x + e[i].c, max1);
    34. }
    35. return max1;
    36. }
    37. void slove() {
    38. cin >> n;
    39. up(i, 0, n - 1) {
    40. cin >> e[i].a >> e[i].b >> e[i].c;
    41. }
    42. double l = 0, r = 1000;
    43. while (r-l>value) {
    44. double mid1 = l + (r - l) / 3.0;
    45. double mid2 = r - (r - l) / 3.0;
    46. if (check(mid1) < check(mid2)) r = mid2;
    47. else l = mid1;
    48. }
    49. printf("%.4lf\n", check(l));
    50. //cout << fixed << setprecision(4) << check(l);
    51. }
    52. int main() {
    53. Ios;
    54. cin >> t;
    55. while (t--) {
    56. slove();
    57. }
    58. }

    三.Queue Sort

    题意:

    你需要用一种特殊的排序方法对一个长为 n 的数组进行操作。每次操作,你将 a1​ 放在数组中最后一个小于等于它的元素后面(没有就不动)。求这种排序方法是否可以使得数组升序排列?

    题解:

    当 a1​ 为原始数组中最后一个最小的数时,题目中的操作变得无意义。而此时数组是否升序,取决于原始数组最后一个最小值后的元素是否升序;原始数组最后一个最小值后的元素升序时操作数为这个元素的下标减一,否则无解;

    代码:

    1. #include <iostream>
    2. #include <cstring>
    3. #include <cmath>
    4. #include <iomanip>
    5. #include <algorithm>
    6. #include <cstdio>
    7. #include <stack>
    8. #include <queue>
    9. #include<set>
    10. #include <string>
    11. #include<map>
    12. using namespace std;
    13. using ll = long long;
    14. using ull = unsigned long long;
    15. #define up(i, h, n) for (int i = h; i <= n; i++)
    16. #define down(i, h, n) for(int i = h; i >= n; i--)
    17. #define wh(x) while(x--)
    18. #define node struct node
    19. #define it ::iterator
    20. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    21. constexpr int MaxN = 200005;
    22. constexpr int MaxM = 10005;
    23. constexpr int mod = 1e9 + 7;
    24. constexpr int inf = 0x7fffffff;
    25. constexpr double value = 1e-10;
    26. int a[MaxN];
    27. void slove() {
    28. int n;
    29. cin >> n;
    30. int mins = 2e9;
    31. int ans;
    32. bool flag = true;
    33. up(i, 1, n) {
    34. cin >> a[i];
    35. if (mins > a[i]) {
    36. mins = a[i];
    37. ans = i;
    38. }
    39. }
    40. up(i, ans + 1, n - 1) {
    41. if (a[i] > a[i + 1]) flag = false;
    42. }
    43. if (flag)cout << ans - 1 << endl;
    44. else cout << -1 << endl;
    45. }
    46. int main() {
    47. Ios;
    48. int t;
    49. cin >> t;
    50. while (t--) {
    51. slove();
    52. }
    53. }

    四.Querying Multiset

    题意:

    给定一个集合和 Q 次操作,每个操作可能是以下操作之一:

    • 第一个操作给定整数 x,表示将 x 放入集合。

    • 第二个操作给定整数 x,表示将集合的数分别加上 x。

    • 第三个操作将集合最小的数删除。

    对于每个第三个操作,输出你删去的数。

    题解:

    这几个操作主要是操作二,把根里面的每一个元素遍历更新显然不符合时间复杂度要求,考虑如何把操作二变为单点修改;所以我们不难想到,用一个值 ans来表示当前的增加量,这样操作二可以解决;在这种情况下:对于操作一,把 ans 看作后面插入元素所需的减少量,那么插入的数字 x 可以用 q.push(x-ans) 来代替;对于操作三,只需要输出堆里最小元素加上 ans 的值即可;

    代码:

    1. #include <iostream>
    2. #include <cstring>
    3. #include <cmath>
    4. #include <iomanip>
    5. #include <algorithm>
    6. #include <cstdio>
    7. #include <stack>
    8. #include <queue>
    9. #include<set>
    10. #include <string>
    11. #include<map>
    12. using namespace std;
    13. using ll = long long;
    14. using ull = unsigned long long;
    15. #define up(i, h, n) for (int i = h; i <= n; i++)
    16. #define down(i, h, n) for(int i = h; i >= n; i--)
    17. #define wh(x) while(x--)
    18. #define node struct node
    19. #define it ::iterator
    20. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    21. constexpr int MaxN = 200005;
    22. constexpr int MaxM = 10005;
    23. constexpr int mod = 1e9 + 7;
    24. constexpr int inf = 0x7fffffff;
    25. constexpr double value = 1e-10;
    26. ll ans;
    27. priority_queue<long, vector <long>, greater<long>>q;
    28. void slove() {
    29. int n;
    30. cin >> n;
    31. if (n == 1) {
    32. ll x;
    33. cin >> x;
    34. q.push(x-ans);
    35. }
    36. else if (n == 2) {
    37. ll x;
    38. cin >> x;
    39. ans += x;
    40. }
    41. else {
    42. cout << q.top() + ans << endl;
    43. q.pop();
    44. }
    45. }
    46. int main() {
    47. int t;
    48. cin >> t;
    49. while (t--) {
    50. slove();
    51. }
    52. return 0;
    53. }

  • 相关阅读:
    PIC单片机3——外部中断
    ant design vue 实现组件类型推断 vue3,vite,ts
    I.MX6U-ALPHA开发板(DDR3实验)
    TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
    ​ 详解Linux内核通信-proc文件系统
    【ros2 control 机器人驱动开发】双关节多控制器机器人学习-example 6
    编写一个Mybatis程序
    反向输出一个三位数
    从0到1搭建大数据平台之数据计算
    ClearML入门:简化机器学习解决方案的开发和管理
  • 原文地址:https://blog.csdn.net/cxj0695/article/details/141034452