PAT甲级 1089 Insert or Merge

题目简介

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either “Insertion Sort” or “Merge Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

题解

#include<bits/stdc++.h>

bool is_insertion(std::vector<int>& arr, std::vector<int>& compare) {
   
   
    int n = arr.size();
    for(int i = 1; i < n; i++) {
   
   
        int j = i -1;
        int key = arr[i];
        while
<think>我们被要求解释update、insertmerge语句,并且最后必须提出3-5个相关问题,以“§§相关问题§§”开头,然后列出问题。 注意:解释这三种语句,特别是在Oracle数据库的上下文中。 由于之前对话中已经涉及了MERGE的使用,这里需要分别说明这三个语句的基本用途和区别。 按照要求,回答使用中文。 </think>### UPDATE、INSERTMERGE 语句详解(Oracle 场景) #### 1. **UPDATE 语句** **作用**:修改表中**已存在**记录的列值 **语法**: ```sql UPDATE 表名 SET 列1 = 值1, 列2 = 值2 WHERE 条件; -- 必须有WHERE子句否则更新全表 ``` **示例**(更新指定ID的risk值): ```sql UPDATE risk_table SET risk = 'HIGH' WHERE id = 1001; ``` **特点**: - 仅作用于已存在数据 - 必须通过WHERE条件限定范围(否则全表更新) - 执行后需COMMIT提交事务 #### 2. **INSERT 语句** **作用**:向表中**新增**记录 **语法**: ```sql INSERT INTO 表名 (列1, 列2) VALUES (值1, 值2); -- 单行插入 ``` **示例**(插入新ID记录): ```sql INSERT INTO risk_table (id, risk) VALUES (1002, 'MEDIUM'); ``` **特点**: - 仅插入不存在的数据 - 若插入已存在的唯一键值会触发`ORA-00001`错误 - 支持多行插入:`INSERT ALL ... SELECT` #### 3. **MERGE 语句(UPSERT)** **作用**:**智能合并**数据(存在则UPDATE,不存在则INSERT) **语法**: ```sql MERGE INTO 目标表 USING 数据源 ON (匹配条件) WHEN MATCHED THEN UPDATE SET ... -- 更新操作 WHEN NOT MATCHED THEN INSERT ... -- 插入操作 ``` **示例**(原子级覆盖写入): ```sql MERGE INTO risk_table t USING (SELECT 1003 id, 'LOW' risk FROM dual) s ON (t.id = s.id) WHEN MATCHED THEN UPDATE SET t.risk = s.risk WHEN NOT MATCHED THEN INSERT (id, risk) VALUES (s.id, s.risk); ``` **核心优势**: - 原子性操作避免竞态条件 - 单语句完成逻辑判断+写入 - 性能优于先SELECT后UPDATE/INSERT的组合 > ⚠️ **关键区别**: > - `INSERT` 只增不减,`UPDATE` 只改不增 > - `MERGE` = `INSERT` + `UPDATE` + 自动存在性判断
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

polarours

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值