Codeforces Round #573 (Div. 2)
C - Tokitsukaze and Discard Items
Recently, Tokitsukaze found an interesting game. Tokitsukaze had n 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≤1018, 1≤m≤105, 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.
题意:题意大概是给你n个数(为1到n)和m个特征数,然后把这n个数每k个数放到一页纸上,即1~k,k+1~2k,...直到n。
然后你需要在一页上把特征值操作删掉,删除特征值位置空了之后后面的数往前移动。只能从最前面有特征值的页面开始操作,
每次只能动一页,动完之后数字挪动,问你要操作几次。
思路:模拟题,直接按题意模拟就好了,每次找出需要动的第一页上的所有数,再将记录移动的计数器增加,重新找下一页,
注意找的时候位置要减去移动的计数器move。由于我代码是debug出来的(本菜鸡一次过不了orzorz),所以代码有点乱,
但是理解起来问题应该不大......
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<cmath> 10 using namespace std; 11 #define ll long long 12 const int mod=1e9+7; 13 const int inf=1e9+7; 14 15 const int maxn=1e5+10; 16 ll int num[maxn]; 17 18 int main() 19 { 20 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 21 22 ll int n,m,k; 23 cin>>n>>m>>k; 24 25 for(int i=0;i<m;i++) 26 cin>>num[i]; 27 28 ll int ans=0,move=0; 29 30 ll int flag;//页数匹配的标记 31 32 if(num[0]%k==0) 33 flag=num[0]/k-1;//标记最先动的这一页 34 else 35 flag=num[0]/k; 36 37 int left=0; 38 39 ll int cnt=0; 40 while(left<m) 41 { 42 num[left]-=move;//移动位置 43 if((num[left]/k==flag)||(num[left]/k==flag+1&&num[left]%k==0))//还在这一页上 44 { //例:1~4在1~5页面 || 5要特判在该页面 45 cnt++; 46 left++;//继续找 47 } 48 else//一旦找到一个不在了 49 { 50 ans++;//处理完前面一页操作加一 51 move+=cnt;//移动数增加 52 53 num[left]-=cnt;//这一页也移动,注意移动前一次操作的计数而不是总数move 54 cnt=0;//重新初始化 55 56 if(num[left]%k==0&&num[left]>=k)//刷新标记 57 flag=num[left]/k-1;//num[left]>=k是边界问题, 58 else //是不使flag标记变成-1,从而匹配页数失败 59 flag=num[left]/k; 60 61 if((num[left]/k==flag)||(num[left]/k==flag+1&&num[left]%k==0))//还在这一页上 62 { 63 cnt++; 64 left++;//继续找 65 } 66 } 67 } 68 69 if(cnt)//处理到最后,cnt不为0说明还操作了一次,答案加一 70 ans++; 71 72 cout<<ans<<endl; 73 74 return 0; 75 }