• C. Virus(差分数组,思维)


    原题链接:

    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)

    代码:

    1. #include
    2. using namespace std;
    3. #define int long long
    4. const int maxj=1e5+100;
    5. int a[maxj];
    6. signed main()
    7. {
    8. ios::sync_with_stdio(false);
    9. cin.tie(0);cout.tie(0);//按要求推导公式
    10. #ifdef LOCAL
    11. freopen("input.txt", "r", stdin);
    12. freopen("output.txt", "w", stdout);//a为add,,w
    13. #endif
    14. int t;cin>>t;
    15. while(t--){
    16. int n,m;cin>>n>>m;
    17. vector<int>ve;
    18. for(int i=1;i<=m;++i){
    19. cin>>a[i];
    20. }sort(a+1,a+m+1);
    21. for(int i=1;i
    22. ve.emplace_back(a[i+1]-a[i]-1);
    23. }
    24. ve.emplace_back(a[1]+n-a[m]-1);
    25. sort(ve.begin(),ve.end());
    26. int ans=0;//正难则反,求能保护的房子的个数
    27. for(int i=ve.size()-1;i>=0;--i){
    28. // cout<
    29. ve[i]-=4*(m-i-1);
    30. if(ve[i]==1){
    31. ans++;
    32. }else{
    33. ans+=max(0ll,ve[i]-1);
    34. }
    35. }cout<'\n';
    36. }
    37. }

  • 相关阅读:
    【ES6】阮一峰ES6学习(四) 对象的扩展
    我的NAS方案及使用的功能
    Rust的高效易用日志库—tklog
    Greenplum-表分区
    协程设计原理
    go实现N个协程交替顺序打印自然数的详细解释
    2023年三大网络安全威胁不容忽视
    Kaggle - LLM Science Exam(二):Open Book QA&debertav3-large详解
    spark on yarn 的 executor、cores、driver 作用及配置
    【设计模式】享元模式的外部状态和内部状态
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/126097402