• Codeforces Round #812 (Div. 2)A.B.C.D


    A. Traveling Salesman Problem

    题目链接:

    Problem - A - Codeforces

    题面:

     

    题意:

    你站在0,0的位置上,你可以往上,下,左,右,四个方向走。这片区域有n个箱子,位于(xi,yi),你需要走到(xi,yi)才能捡起箱子,问你从(0,0)捡完所有箱子再回到(0,0)需要几步

    思路:

    我们只需求出最左/右边箱子的横坐标,最上/下边箱子的纵坐标。这就是我们四个方向要走的最大距离,然后把四个数的绝对值乘2累加即可

    代码:

    1. #include
    2. using namespace std;
    3. int main(){
    4. int t;
    5. cin >> t;
    6. while(t--){
    7. int n;
    8. cin >> n;
    9. int a = 105;
    10. int b = 105;
    11. int c = -105;
    12. int d = -105;
    13. for(int i = 0; i < n; i++){
    14. int x, y;
    15. cin >> x >> y;
    16. if(x < 0){
    17. a = min(a, x);
    18. }else{
    19. c = max(c, x);
    20. }
    21. if(y < 0){
    22. b = min(b, y);
    23. }else{
    24. d = max(d, y);
    25. }
    26. }
    27. int ans = 0;
    28. if(a != 105){
    29. ans += abs(a) * 2;
    30. }
    31. if(b != 105){
    32. ans += abs(b) * 2;
    33. }
    34. if(c != -105){
    35. ans += c * 2;
    36. }
    37. if(d != -105){
    38. ans += d * 2;
    39. }
    40. cout << ans << endl;
    41. }
    42. return 0;
    43. }

    B. Optimal Reduction

    题目链接:

    Problem - B - Codeforces

    题面:

    题意:

    你可以对一个数组进行操作:

    1.选择两个索引l,r

    2.将a[l] ~ a[r]的所有数-1。

    f(a)表示把数组a的所有数变成0的操作数

    现在规定一种数组B是a数组进行重新排列后的新数组

    问是否所有满足条件的B数组f(B)>= f(a)

    思路:

    f(B)最小的B数组一定是一个满足Bi+1 >= Bi 或者 Bi+1 <= Bi的数组,那么如果a数组不满足的话,就会有f(B) < f(a) 

    代码:

    1. #include
    2. using namespace std;
    3. int a[100005];
    4. int main(){
    5. int t;
    6. cin >> t;
    7. while(t--){
    8. int n;
    9. cin >> n;
    10. for(int i = 0; i < n; i++){
    11. cin >> a[i];
    12. }
    13. bool f = 0;
    14. bool ff = 0;
    15. for(int i = 1; i < n - 1; i++){
    16. if(a[i] < a[i - 1]){
    17. f = 1;
    18. }
    19. if(f && a[i] < a[i + 1]){
    20. ff = 1;
    21. break;
    22. }
    23. }
    24. if(ff){
    25. cout << "NO" << endl;
    26. }else{
    27. cout << "YES" << endl;
    28. }
    29. }
    30. return 0;
    31. }

    C. Build Permutation

    题目链接:

    Problem - C - Codeforces

    题面:

    题意:

    有个数组a是0~n-1的排列的一种,如果ai + i是平方数,那么这个数组是好的,现在需要构造出a数组

    思路:

    我们可以把1e5内的所有完全平方数求出来,从(n-1)遍历到0,求出每个数能放的位置,(越大的数能放的位置越少)我们先把大的数放了,那么小的数能放的位置也会逐渐减少。我们遍历结束了a数组也就出来了

    代码:

    1. #include
    2. using namespace std;
    3. int arr[100005];
    4. int main(){
    5. int t;
    6. cin >> t;
    7. while(t--){
    8. int n;
    9. cin >> n;
    10. for(int i = 0; i < n; i++){
    11. arr[i] = -1;
    12. }
    13. vector<int> ve;
    14. for(int i = 0; i * i < n; i++){
    15. ve.push_back(i * i);
    16. if((i + 1) * (i + 1) >= n){
    17. ve.push_back((i + 1) * (i + 1));
    18. }
    19. }
    20. int m = ve.size() - 1;
    21. for(int i = n - 1; i >= 0; i--){
    22. for(int j = m; j >= 0; j--){
    23. if(ve[j] - i < n && arr[ve[j] - i] == -1){
    24. arr[ve[j] - i] = i;
    25. break;
    26. }
    27. }
    28. }
    29. for(int i = 0; i < n; i++){
    30. cout << arr[i] << " ";
    31. }
    32. cout << endl;
    33. }
    34. return 0;
    35. }

    D. Tournament Countdown

    题目链接:

    Problem - D - Codeforces

    题面:

    题意:

    这是一个交互问题。

    有一场由2n名选手组成的锦标赛。第一名选手与第二名选手竞争,第三名选手与第四名选手竞争,以此类推。之后,第一场比赛的获胜者与第二场比赛的获胜者进行竞争,依此类推。锦标赛结束时,只剩下一名选手,他被宣布为锦标赛的获胜者。这样的锦标赛方案被称为单淘汰赛。

    你不知道结果,但你想找出锦标赛的获胜者。在一个查询中,您选择两个整数a和b,它们是两个参赛者的索引。如果A比B赢了更多的比赛,评审团将返回1,如果B比A赢了更多的比赛,评审团将返回2,如果他们的胜利数量相等,评审团将返回0。

    在不超过⌈13个⋅2n+1个⌉查询中找到赢家。这里,⌈x⌉表示向上舍入到最接近的整数的x的值。

    请注意,锦标赛已经结束很久了,这意味着结果是固定的,不依赖于您的查询。

    思路:

    我们需要在2 ^ (n+1)/3次查询内得出正确答案,所以如果有4个人我们需要在2次查询内得到谁是胜者:

    有a,b,c,d四个人,我们先查询a,c

    如果a > c,那么就说明d是c,d的迎着,我们再次查询a,d即可

    如果a < c,那么我们查询b, c即可

    如果a == c,那么两个人在第一轮都是输的一方,那么查询b, d,即可

    如果剩下两人,我们需要查询这两人

    直到只剩下一人就可以确定冠军了

    代码:

    1. #include
    2. using namespace std;
    3. int ask(int a, int b){
    4. cout << "? " << a << " " << b << endl;
    5. cout.flush();
    6. int ans;
    7. cin >> ans;
    8. return ans;
    9. }
    10. int main(){
    11. int t;
    12. cin >> t;
    13. while(t--){
    14. int n;
    15. cin >> n;
    16. queue<int> q;
    17. int m = pow(2, n);
    18. for(int i = 1; i <= m; i++){
    19. q.push(i);
    20. }
    21. while(q.size() >= 4){
    22. int a = q.front();
    23. q.pop();
    24. int b = q.front();
    25. q.pop();
    26. int c = q.front();
    27. q.pop();
    28. int d = q.front();
    29. q.pop();
    30. int ans = ask(a, c);
    31. int x;
    32. if(ans == 2){
    33. ans = ask(b, c);
    34. if(ans == 2){
    35. x = c;
    36. }else{
    37. x = b;
    38. }
    39. }else if(ans == 1){
    40. ans = ask(a, d);
    41. if(ans == 1){
    42. x = a;
    43. }else{
    44. x = d;
    45. }
    46. }else{
    47. ans = ask(b, d);
    48. if(ans == 1){
    49. x = b;
    50. }else{
    51. x = d;
    52. }
    53. }
    54. q.push(x);
    55. }
    56. if(q.size() == 2){
    57. int a = q.front();
    58. q.pop();
    59. int b = q.front();
    60. q.pop();
    61. int ans = ask(a, b);
    62. if(ans == 1){
    63. q.push(a);
    64. }else{
    65. q.push(b);
    66. }
    67. }
    68. if(q.size() == 1){
    69. cout << "! " << q.front() << endl;
    70. cout.flush();
    71. }
    72. }
    73. return 0;
    74. }

  • 相关阅读:
    捕获多种异常练习
    数据库管理-第115期 too many open files(202301107)
    LeetCode-754. 到达终点数字【数学】
    算法基础-数学知识-质数、约数
    球场输了?卡塔尔背地里可能赢麻了!
    工程伦理--9.1 岗位胜任力
    手写Demo体验volatile可见性的作用
    虚拟 DOM 和 diff 算法
    CNN(七):ResNeXt-50算法的思考
    Java分支结构综合练习一之一元二次方程求解
  • 原文地址:https://blog.csdn.net/m0_55682843/article/details/126872250