当前位置:   article > 正文

算法测试.

算法测试.

一.CodeForces 1829A

题意:

题目意思就是给你t个字符串,让你找出每个串与codeforces这个串有多少不同的字母;

题解:

定义一个变量循环比较字符串,不相同计数即可;

代码:

  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. using namespace std;
  12. using ll = long long;
  13. using ull = unsigned long long;
  14. #define up(i, h, n) for (int i = h; i <= n; i++)
  15. #define down(i, h, n) for(int i = h; i >= n; i--)
  16. #define wh(x) while(x--)
  17. #define node struct node
  18. #define it ::iterator
  19. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  20. constexpr int MaxN = 200005;
  21. constexpr int MaxM = 100005;
  22. constexpr int mod = 1e9 + 7;
  23. constexpr int inf = 0x7fffffff;
  24. void slove() {
  25. string s = "codeforces";
  26. string a;
  27. cin >> a;
  28. int ans = 0;
  29. up(i, 0, s.size()) {
  30. if (a[i] != s[i])
  31. ans++;
  32. }
  33. cout << ans << endl;
  34. }
  35. int main() {
  36. int t;
  37. cin >> t;
  38. while (t--) {
  39. slove();
  40. }
  41. }

二.CodeForces 17B

题意:

尼克的公司有 n 名员工。现在,尼克需要在公司中建立一个 "主管-同级 "关系的树状层次结构(也就是说,除了一名员工外,每名员工都有一名主管)。有 m 个应用程序以如下形式编写:"员工 ai 愿意成为员工 bi 的主管,但需支付额外费用 ci "。已知每位员工的资质 qj ,对于每个申请,以下情况为真: qai > qbi 。

你能帮尼克计算出这样一个层次结构的最低成本吗?

题解:

贪心,把m条关系按照代价排序,当x的大于y的,且y没有主管即可能让x当y的主管;

代码:

  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. using namespace std;
  12. using ll = long long;
  13. using ull = unsigned long long;
  14. #define up(i, h, n) for (int i = h; i <= n; i++)
  15. #define down(i, h, n) for(int i = h; i >= n; i--)
  16. #define wh(x) while(x--)
  17. #define node struct node
  18. #define it ::iterator
  19. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  20. constexpr int MaxN = 200005;
  21. constexpr int MaxM = 100005;
  22. constexpr int mod = 1e9 + 7;
  23. constexpr int inf = 0x7fffffff;
  24. int a[1005];
  25. int f[1005];
  26. node {
  27. int x, y, c;
  28. }e[10005];
  29. int cmp(node x, node y) {
  30. return x.c < y.c;
  31. }
  32. int main() {
  33. int n;
  34. cin >> n;
  35. up(i, 1, n) {
  36. cin >> a[i];
  37. }
  38. int m;
  39. cin >> m;
  40. up(i, 1, m) {
  41. cin >> e[i].x >> e[i].y >> e[i].c;
  42. }
  43. sort(e + 1, e + m + 1, cmp);
  44. int ans = 0, num = 0;
  45. up(i, 1, m) {
  46. if (!f[e[i].y] && a[e[i].x] > a[e[i].y]) {
  47. f[e[i].y] = 1;
  48. ans += e[i].c;
  49. num++;
  50. }
  51. if (num == n - 1) {
  52. break;
  53. }
  54. }
  55. if (num == n - 1) {
  56. cout<<ans<<endl;
  57. }
  58. else cout<<-1<<endl;
  59. }

三.熙巨打票

题解:

一道简单的题,注意用longlong就行

代码:

  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. using namespace std;
  12. using ll = long long;
  13. using ull = unsigned long long;
  14. #define up(i, h, n) for (int i = h; i <= n; i++)
  15. #define down(i, h, n) for(int i = h; i >= n; i--)
  16. #define wh(x) while(x--)
  17. #define node struct node
  18. #define it ::iterator
  19. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  20. constexpr int MaxN = 200005;
  21. constexpr int MaxM = 100005;
  22. constexpr int mod = 1e9 + 7;
  23. constexpr int inf = 0x7fffffff;
  24. void slove() {
  25. ll a, b, n;
  26. cin >> a >> b >> n;
  27. if (n <= 2) cout << b * n << endl;
  28. else if (a <= b) cout << b * n << endl;
  29. else cout << b*n+(n-1)/2*(a-b)<< endl;
  30. }
  31. int main() {
  32. int t;
  33. cin >> t;
  34. while (t--) {
  35. slove();
  36. }
  37. }

四.Colorful Beans

题意:

有N种豆子,这些豆子只能靠颜色来区分,每个豆子都有它的美味度;题目要求的是每种颜色豆子里面的美味度最小值的最大值;

题解:

运用map来存储最小的美味度,然后循环遍历找最大的美味度就好;

代码:

  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 = 100005;
  23. constexpr int mod = 1e9 + 7;
  24. constexpr int inf = 0x7fffffff;
  25. int main() {
  26. Ios;
  27. int n;
  28. cin >> n;
  29. map<int, int> p;
  30. up(i, 0, n - 1) {
  31. int a, c;
  32. cin >> a >> c;
  33. if (!p[c])p[c] = a;
  34. else p[c] = min(p[c], a);
  35. }
  36. int ans = 0;
  37. for (auto s : p) {
  38. ans = max(ans, s.second);
  39. }
  40. cout << ans << endl;
  41. }

五.刻录光盘

题解:

运用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. using namespace std;
  12. using ll = long long;
  13. using ull = unsigned long long;
  14. #define up(i, h, n) for (int i = h; i <= n; i++)
  15. #define down(i, h, n) for(int i = h; i >= n; i--)
  16. #define wh(x) while(x--)
  17. #define node struct node
  18. #define it ::iterator
  19. #define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  20. constexpr int MaxN = 200005;
  21. constexpr int MaxM = 100005;
  22. constexpr int mod = 1e9 + 7;
  23. constexpr int inf = 0x7fffffff;
  24. int f[205];
  25. int book[205][205];
  26. int main() {
  27. Ios;
  28. int n, t;
  29. cin >> n;
  30. up(i, 1, n) {
  31. cin >> t;
  32. while (t != 0) {
  33. book[i][t] = 1;
  34. cin >> t;
  35. }
  36. }
  37. up(i, 1, n) {
  38. f[i] = i;
  39. }
  40. up(k, 1, n) {
  41. up(i, 1, n) {
  42. up(j, 1, n) {
  43. if (book[i][k] && book[k][j])
  44. book[i][j] = 1;
  45. }
  46. }
  47. }
  48. up(i, 1, n) {
  49. up(j, 1, n) {
  50. if (book[i][j] == 1)
  51. f[j] = f[i];
  52. }
  53. }
  54. int ans = 0;
  55. up(i, 1, n) {
  56. if (f[i] == i) ans++;
  57. }
  58. cout << ans << endl;
  59. return 0;
  60. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/970299
推荐阅读
相关标签
  

闽ICP备14008679号