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

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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值