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

 

Java是一种具备卓越性能与广泛平台适应性的高级程序设计语言,最初由Sun Microsystems(现属Oracle公司)的James Gosling及其团队于1995年正式发布。该语言在设计上追求简洁性、稳定性、可移植性以及并发处理能力,同时具备动态执行特性。其核心特征与显著优点可归纳如下: **平台无关性**:遵循“一次编写,随处运行”的理念,Java编写的程序能够在多种操作系统与硬件环境中执行,无需针对不同平台进行修改。这一特性主要依赖于Java虚拟机(JVM)的实现,JVM作为程序与底层系统之间的中间层,负责解释并执行编译后的字节码。 **面向对象范式**:Java全面贯彻面向对象的设计原则,提供对封装、继承、多态等机制的完整支持。这种设计方式有助于构建结构清晰、模块独立的代码,提升软件的可维护性与扩展性。 **并发编程支持**:语言层面集成了多线程处理能力,允许开发者构建能够同时执行多项任务的应用程序。这一特性尤其适用于需要高并发处理的场景,例如服务器端软件、网络服务及大规模分布式系统。 **自动内存管理**:通过内置的垃圾回收机制,Java运行时环境能够自动识别并释放不再使用的对象所占用的内存空间。这不仅降低了开发者在内存管理方面的工作负担,也有效减少了因手动管理内存可能引发的内存泄漏问题。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值