573 div2 c

C. Tokitsukaze and Discard Items

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 mm (1≤m≤n1≤m≤n) special items of them.

These nn items are marked with indices from 11 to nn. In the beginning, the item with index ii is placed on the ii-th position. Items are divided into several pages orderly, such that each page contains exactly kk 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.

imgConsider the first example from the statement: n=10n=10, m=4m=4, k=5k=5, p=[3,5,7,10]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 33 and 55. After, the first page remains to be special. It contains [1,2,4,6,7][1,2,4,6,7], Tokitsukaze discards the special item with index 77. After, the second page is special (since it is the first page containing a special item). It contains [9,10][9,10], Tokitsukaze discards the special item with index 1010.

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

Input

The first line contains three integers nn, mm and kk (1≤n≤10181≤n≤1018, 1≤m≤1051≤m≤105, 1≤m,k≤n1≤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 mm distinct integers p1,p2,…,pmp1,p2,…,pm (1≤p1<p2<…<pm≤n1≤p1<p2<…<pm≤n) — the indices of special items which should be discarded.

思路:模拟题,直接按题意模拟就好了,每次找出需要动的第一页上的所有数,再将记录移动的计数器增加,重新找下一页,

注意找的时候位置要减去移动的计数器move。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
const int manx=100005;
long long arr[manx];
int main()
{
long long m,n,k;
cin>>m>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
long long move=0;
long long i=1,j=1;
long ans=0;
while(i<=n)
{
j=i;
long long tag=(arr[i]-move-1)/k;//减1的目的是为了保证边缘也在这一页,比如k=5,如果不-1则5在下一页
while(((arr[j]-move-1)/k==tag)&&j<=m)//找这一页第一个要删除的位置,并且和这个同一页的所有特殊数全部删掉。
j++;
move+=j-i;
ans++;
i=j;
}
cout<<ans<<endl;
}