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.

img
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 问题描述

img

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值