码迷,mamicode.com
首页 > 其他好文 > 详细

取整问题

时间:2019-07-14 09:28:55      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:limit   round   ret   order   least   inter   graph   XML   OWIN   

 

首先是整数类型


设 ll a,k;

求a/k  向上取整

ans=(a-1)/k+1;

 求a/k 向下取整

ans=(a-1)/k;

 

int/int 是整除

强制类型转化 等 都是向下取整

 

 

例题 codeforce  C - Tokitsukaze and Discard Items

Codeforces Round #572 (Div. 2)

 

 

https://codeforces.com/contest/1191/problem/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 (1mn1≤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.

技术图片 Consider 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 (1n10181≤n≤1018 , 1m1051≤m≤105 , 1m,kn1≤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 (1p1<p2<<pmn1≤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
Copy
10 4 5
3 5 7 10
Output
Copy
3
Input
Copy
13 4 5
7 8 9 10
Output
Copy
1
Note

For the first example:

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

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

 

code

//
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,m;
    ll k;
    cin>>n>>m>>k;
    ll a;
    ll sum=0;
    ll ans=0;
    ll tot=0;
    ll stp=0;
    for(int i=1;i<=m;i++)
    {
        cin>>a;
        if(i==1)
        {
            ans=((a-sum-1)/(k)+1);
            tot++;
        }
        else
        {
        if(ans==((a-sum-1)/(k)+1))
        {
            tot++;
        }
        else
        if(ans!=((a-sum-1)/(k)+1))
        {
            sum+=tot;
            tot=1;
            ans=((a-sum-1)/(k)+1);
            stp++;
        }
        }
    }
    cout<<stp+1;
}

 

 

取整问题

标签:limit   round   ret   order   least   inter   graph   XML   OWIN   

原文地址:https://www.cnblogs.com/OIEREDSION/p/11183092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!