2021-09-30每日刷题打卡

一、ZOJ-1037:Gridland

1.1 问题描述

Background
For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the “easy” problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the “hard” ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.

Problem
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North-South or East-West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 * 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 * 3-Gridland, the shortest tour has length 6.

2021-09-30每日刷题打卡
Figure 7: A traveling-salesman tour in 2 * 3-Gridland.

题目大意:

当m或n是偶数的时候,最短长度是nm,同时为奇数的时候是nm+0.41

1.2 问题解决

#include <cstdio>
#include <iostream>
using namespace std;
 
int main()
{
    int cas;
    scanf("%d",&cas);
    int num;
    for (num = 1; num <= cas; num++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        printf("Scenario #%d:\n",num);
        printf("%d.",n*m);
        if (n&1 && m&1)
            printf("41");
        else printf("00");
        printf("\n\n");
    }
}

二、Leetcode-148:排序链表

2.1 问题描述

2021-09-30每日刷题打卡

2.2 问题解决

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if(head == nullptr || head->next  == nullptr)
            return head;
        
        ListNode* fast = head->next;
        ListNode* slow = head;
        while(fast != nullptr && fast->next != nullptr)
        {
            slow = slow->next;
            fast = fast->next->next;
        }

        ListNode* tmp = slow->next;
        slow->next = nullptr;
        ListNode* left = sortList(head);
        ListNode* right = sortList(tmp);

        ListNode preleft;
        ListNode* cur = &preleft;
        preleft.next = left;

        while(true)
        {
            if(right == nullptr)
                return preleft.next;
            
            if(cur->next == nullptr)
                    break;

            if(cur->next->val > right->val)
            {
                ListNode* tmp = right;
                right = right->next;
                tmp->next = cur->next;
                cur->next = tmp;
                //cur = tmp;
            }
            else
                cur = cur->next;
        }

        cur->next = right != nullptr ? right : nullptr;

        return preleft.next;
    }
};

三、生词

  • polynomial n. 多项式

  • exponential-time algorithms 指数时间算法

  • rectangular adj. 矩形的

  • grid n. 网格

  • Euclidean distance 欧几里得距离

  • measure vt. 测量

四、参考文章

  1. ZOJ 1037 A - Gridland
  2. ZOJ-1037-Gridland
上一篇:leetcode 148. 排序链表(归并排序 快慢指针 递归 迭代)


下一篇:C++实现二叉树的层序遍历