原题链接:

There are nn houses numbered from 11 to nn on a circle. For each 1≤i≤n−11≤i≤n−1, house ii and house i+1i+1 are neighbours; additionally, house nn and house 11 are also neighbours.

Initially, mm of these nn houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.

Every day, the following things happen in order:

  • Cirno chooses an uninfected house, and protect it permanently.
  • All uninfected, unprotected houses which have at least one infected neighbor become infected.

Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.

Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of test cases follows.

The first line of each test case consists of two positive integers n,mn,m (5≤n≤1095≤n≤109, 1≤m≤min(n,105)1≤m≤min(n,105)) — the number of houses on the circle, and the number of houses that are initially infected.

The second line of each test case consists of mm distinct positive integers a1,a2,⋯,ama1,a2,⋯,am (1≤ai≤n1≤ai≤n) — the indices of the houses infected initially.

It is guaranteed that the sum of mm over all test cases does not exceed 105105.

Output

For each test case, output an integer on a separate line, which is the minimum number of infected houses in the end.

Example

input

Copy

8
10 3
3 6 8
6 2
2 5
20 3
3 7 12
41 5
1 11 21 31 41
10 5
2 4 6 8 10
5 5
3 2 5 4 1
1000000000 1
1
1000000000 4
1 1000000000 10 16

output

Copy

7
5
11
28
9
5
2
15

Note

In the first test case:

At the start of the first day, house 33, 66, 88 are infected. Choose house 22 to protect.

At the start of the second day, house 33, 44, 55, 66, 77, 88, 99 are infected. Choose house 1010 to protect.

At the start of the third day, no more houses are infected.

In the second test case:

At the start of the first day, house 22, 55 are infected. Choose house 11 to protect.

At the start of the second day, house 22, 33, 44, 55, 66 are infected. No more available houses can be protected.

思路:

正着求被污染的是挺难的,应该求能被保护的房子的数量

第一步:维护一个差分数组,并排序

第二步:从大到小的遍历差分数组,ve【i】-=4*(m-i-1)指的是由于之前隔离时间的原因,中间的房子已经被污染了的(自己想象个图,可以辅助理解)

第三步:ans维护最大可能保护的房子,ve【i】==1时加1,其它取max(ve【i】-1,0)

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=1e5+100;
int a[maxj];
signed main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//按要求推导公式
#ifdef LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);//a为add,,w
#endifint t;cin>>t;while(t--){int n,m;cin>>n>>m;vector<int>ve;for(int i=1;i<=m;++i){cin>>a[i];}sort(a+1,a+m+1);for(int i=1;i<m;++i){ve.emplace_back(a[i+1]-a[i]-1);}  ve.emplace_back(a[1]+n-a[m]-1);sort(ve.begin(),ve.end());int ans=0;//正难则反,求能保护的房子的个数for(int i=ve.size()-1;i>=0;--i){// cout<<ve[i]<<' ';ve[i]-=4*(m-i-1);if(ve[i]==1){ans++;}else{ans+=max(0ll,ve[i]-1);}}cout<<n-ans<<'\n';}
}

C. Virus(差分数组,思维)相关推荐

  1. CodeForces - 1343D Constant Palindrome Sum(思维+差分数组)

    题目链接:点击查看 题目大意:给出 n 个数,保证 n 是偶数,且每个数的范围都在 [ 1 , k ] 之间,现在问我们至少需要给多少个数重新赋值,使得可以满足条件: 所有的数的值域都在 [ 1 , ...

  2. Gym 101775J Straight Master(差分数组)题解

    题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去:例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1 ...

  3. Leetcode题库 798.得分最高的最小轮调(差分数组 C实现)

    文章目录 算法一 双层循环 算法二 差分数组 算法一 双层循环 时间复杂度:n^2 空间复杂度:n int bestRotation(int* nums, int numsSize){char Dif ...

  4. 差分数组 and 树上差分

    差分数组 定义 百度百科中的差分定义 //其实这完全和要讲的没关系 qwq 进去看了之后是不是觉得看不懂? 那我简单概括一下qwq 差分数组de定义:记录当前位置的数与上一位置的数的差值. 栗子 容易 ...

  5. POJ - 1743 Musical Theme(二分+后缀数组+差分数组)

    题目链接:点击查看 题目大意:给出n个连续的数字组成的序列,现在要求出其中两个不重叠的字序列,满足两个子序列"相似",相似的定义是两个子序列当且仅当长度相等并且每一位的数字差都相等 ...

  6. Gym - 101775J Straight Master(差分数组)

    题目链接:点击查看 题目大意:给出n种数,每种数有a[i]个,每3-5种连续的数都可以被消去,现在问给出的所有数字最后能否全部消去 题目分析:正难则反,题目问能否将数字全部消去,我们不妨设一开始的起点 ...

  7. python刷题总结_【python刷题】差分数组

    前缀和主要适用的场景是原始数组不会被修改的情况下,频繁查询某个区间的累加和. 差分数组的主要适用场景是频繁对原始数组的某个区间的元素进行增减. class Difference: def __init ...

  8. Master of GCD(差分数组||线段树)

    题意:长度为n的数组,一开始都是1.对于区间操作l,r,x,在l~r上乘以x.x2||x3.问操作完毕之后,n个数的最大公因子是多少. 对于每个x,都等于2或者是3.那么看最大的公因子,就看各个位置上 ...

  9. Greg and Array CodeForces - 296C(差分数组+线段树)

    Greg has an array a = a1, a2, -, an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ...

最新文章

  1. 兴趣部落的 Git 迁移实践
  2. 如何迅速成为Java高手[Tomjava原创]
  3. ifstat,iftop
  4. 华为Mate系列新机海外亮相 或将于MWC2019发布
  5. ubuntu 14.04系统DHCP服务器搭建
  6. STM8 ADC转换模式-------单次扫描模式
  7. Java学习资料-java基本数据类型
  8. 小白Linux入门之:终端复用器Tmux使用参考
  9. Windows下,Unicode、UTF8,GBK(GB2312)互转
  10. C语言——知识点汇总
  11. Java程序怎么运行?
  12. J2EE框架DDoS漏洞预警公告
  13. 计算机网络毕业论文格式模板范文,计算机毕业论文格式模板范文(计算机毕业论文选题)...
  14. web常见的五种前端布局方式
  15. 学习笔记-状态方程精确离散化
  16. Android 项目正式签名打包教程
  17. [实战] Android 发短信 - SMS
  18. Office EXCEL如何批量把以文本形式存储的数字转换为数字
  19. QT之远程控制对方电脑
  20. html无法获取图片高宽,如何解决谷歌浏览器下jquery无法获取图片的尺寸

热门文章

  1. 为何腾讯和阿里用LoRa,运营商用NB-loT?
  2. 咸鱼翻身beta冲刺博客集
  3. RemObjects Remoting SDK for Delphi
  4. android 相机故障 代码 解决,android – 如何在调用Camera时解决ANR错误?
  5. F28335中断系统详解及其应用
  6. String 字符串常量池
  7. C/C++飞机订票系统
  8. 如何修改java运行环境版本
  9. php小蛋白奶粉有哪些牌子,含乳铁蛋白的奶粉有哪些牌子?宝妈们需要知道这些挑选标准...
  10. 什么是服务器? 服务器能拿来干嘛? 服务器在网站建设中,怎么用? 该怎么访问呢?