Codeforces Round #573 (Div. 2) - C. Tokitsukaze and Discard Items

博客围绕Tokitsukaze丢弃物品的游戏展开,给定n个物品,要丢弃m个特殊物品,每k个物品为一页。需从头找到首个含特殊物品的页,删除该页所有特殊物品,后续物品前移。分析得出应处理m,给出时间复杂度分析及解题思路,最后给出代码。

C. Tokitsukaze and Discard Items

Recently, Tokitsukaze found an interesting game. Tokitsukaze had nn items at the beginning of this game. However, she thought there were too many items, so now she wants to discard m (1≤m≤n) special items of them.

These n items are marked with indices from 1 to n. In the beginning, the item with index i is placed on the i-th position. Items are divided into several pages orderly, such that each page contains exactly k positions and the last positions on the last page may be left empty.

Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.

                                                   

Consider the first example from the statement: n=10, m=4, k=5, p=[3,5,7,10]. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices 3 and 5. After, the first page remains to be special. It contains [1,2,4,6,7], Tokitsukaze discards the special item with index 7. After, the second page is special (since it is the first page containing a special item). It contains [9,10], Tokitsukaze discards the special item with index 10.

Tokitsukaze wants to know the number of operations she would do in total.

Input

The first line contains three integers n, m and k (1 <= n <= 10 ^ {18}1 <= m <= 10 ^{5}, 1≤m,k≤n) — the number of items, the number of special items to be discarded and the number of positions in each page.

The second line contains m distinct integers p1,p2,…,pm (1≤p1<p2<…<pm≤n) — the indices of special items which should be discarded.

Output

Print a single integer — the number of operations that Tokitsukaze would do in total.

Examples

input

10 4 5
3 5 7 10

output

3

input

13 4 5
7 8 9 10

output

1

Note

For the first example:

  • In the first operation, Tokitsukaze would focus on the first page [1,2,3,4,5] and discard items with indices 3 and 5;
  • In the second operation, Tokitsukaze would focus on the first page [1,2,4,6,7] and discard item with index 7;
  • In the third operation, Tokitsukaze would focus on the second page [9,10] and discard item with index 10.

For the second example, Tokitsukaze would focus on the second page [6,7,8,9,10] and discard all special items at once.

time limit per test    :1 second

memory limit per test   : 256 megabytes

input  : standard input

output  : standard output

 

题目大意:

给一个n,那么就有一串数字1~n,给定一个k,表示从开头开始每k个数算一页,给一个数m,表示特殊的数的个数,接下来一行给出这m个特殊的数,包含至少一个特殊的数为特殊的页,从头开始找到第一个特殊的页,每次删除这页里的所有特殊的数,后面的数都要向前移,最后一页的数可以不装满整页,直至特殊的数被删光。问要这样删除多少次。

分析:

n的范围是1到10的18次方,遍历n直接超时,所以我们应该对m进行处理,可以猜想本题最有可能的时间复杂度应该是O(m) 的,当然不排除包含logm,mlogm的时间复杂度,因为我能想到的两种方法就是一种贪心的认为不移动和移动的效果是一样的,但需要在读入时读进m个数,还有一种就是在边读入m个数时边处理,显然前面一种我们可以很轻松举出反例。通过理解题目,我们知道每删除一个数,后面的数就往前移动一个,但在大小为n的数组里,这样的移动代价太高昂,因为数是从前往后开始删的,于是我们可以利用一个数x记录当前页前被删除了多少个,在用一个数cnt 记录操作次数,当下一个数与前一个数不在同一页时,使cnt ++,并更新x。但如何知道下一个数前有多少个数被删除呢,我们不必再使用一个计数器,当我们用for循环从0开始遍历到m - 1,这个数对应的i即为前面被删除的数,那又如何知道两个数是否在同一页?我们只要将这个数原本的位置p[i]减去这页前被删掉的x个数,除以k,如果两个数值相等,即在同一页,但是注意,减掉x后还要再减1,比如5个数为1页,那么位置5减去这页前的x (此时x = 0)除以5为1,而位置4为0,但两者在同一页。当然,我们可以从1开始遍历到m,m初始为1,这样就不用再减1了。

注意,结束后cnt还要再加1,因为最后只剩一页未删,而前面我们是要发现有两个数在不同页才使cnt ++。

代码:

#include <iostream>
#include <vector>
using namespace std;
long long p[100005];
int main()
{
    ios::sync_with_stdio(false);
    long long n, m, k;
    cin >> n >> m >> k;
    int cnt = 0;
    int x = 1;
    for (int i = 1; i <= m; i ++){
        cin >> p[i];
        if (i > 1){
            if ((p[i] - x)/ k != (p[i - 1] - x) / k){
                cnt ++;
                x = i;
            }
        }
    }
    cnt ++;
    cout << cnt << endl;
    return 0;
}

 

跟网型逆变器小干扰稳定性分析与控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模与分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计与参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环与内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析与控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估与改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值