取整问题

首先是整数类型

设 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 (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.

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 (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.

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

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

时间: 2024-10-20 05:13:14

取整问题的相关文章

c#中取整,向上取,向下取

Math.Ceiling()向上取整, Math.Floor()向下取整 示例: d = 4.56789 Math.Ceiling(Convert.ToDecimal(d)).ToString();Math.Ceiling(Convert.ToDouble(d)).ToString();Math.Floor(Convert.ToDecimal(d)).ToString(); Math.Floor(Convert.ToDouble(d)).ToString(); --记录铭心

SQL 向上取整、向下取整、四舍五入取整的实例!round、rounddown、roundup

sql server ==================================================== [四舍五入取整截取] select round(54.56,0) ==================================================== [向下取整截取] SELECT FLOOR(54.56) ==================================================== [向上取整截取] SELECT CE

java小数取整

java中提供三种小数取整方式 Math.ceil() Math.floor() Math.round() ceil:天花板,向上quzheng Math.ceil(11.5) = 12 Math.ceil(-11.5) = -11 floor:地,向下取整 Math.floor(11.5) = 11 Math.floor(-11.5) = -12 round:4舍5入 Math.round(x + 0.5),再向下取整 当 x = 11.5 时,Math.round(x + 0.5) = Ma

JavaScript 中对小数取整的常用函数

常见的js截取小数的方法 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2) 5.Number 四舍五入为指定小数位数的数字 js : 7.23.toFixed(num) . num参数为想要截取的小数位数

JS中取整以及随机颜色问题

前言:感觉自己已经好久好久没有写博客了,最近都是在写在线笔记比较多.现在来到新公司了,昨天刚刚完成一个项目所以今天有空研究研究一下前端方面的技术.下午在看一个游戏代码的时候,发现了几个别人留下的不错的代码小技巧.譬如说取整问题,随机颜色问题.其实这些问题都不大,但是仔细研究一下还是别有洞天,对于提高前端开发方面的理解还是很有帮助的. 取整问题: 1.常规方法: Math.floor(x),返回小于等于x,且最接近x的整数:   Math.floor(1.2);//1 Math.floor(-2.

向上取整

(a+(b-1))/b 34 / 11 + (34 % 11 != 0 ? 1 : 0) CUDA计算中,可以采用这两种方式来表示向上取整.

js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]

<script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write('<br>3.5的四舍五入:'+Math.round(3.01)); write('<br>3.01的进一取整:'+Math.ceil(3.01)); write('<br>3.99的舍去取整:'+Math.floor(3.99)); write('<br>获取

取整、保留小数等数字处理方法

问题描述: 后台数据类型为decimal a,在前台页面中显示时需要显示为整数. 我的做法是Convert.ToInt32(a),这样做是强制把内容转换为整数,改变了数据本来的意义. 最终做法是Math.Round(a,0),将a的小数值舍入为指定精度. 先看Math.Round()方法,是将值舍入到最接近的整数或指定的小数位数. MSDN上可以看到有这些重载的方法. Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Retur

Math取整方法归纳总结

舍掉小数取整:Math.floor(2)=2 舍掉小数取整:Math.floor(2.1)=2 舍掉小数取整:Math.floor(-2.1)=-3 舍掉小数取整:Math.floor(-2.5)=-3 舍掉小数取整:Math.floor(2.9)=2 舍掉小数取整:Math.floor(-2.9)=-3 总结:Math.floor(x) = 不大于x的最大整数 舍掉小数凑整:Math.ceil(2)=2 舍掉小数凑整:Math.ceil(2.1)=3 舍掉小数凑整:Math.ceil(2.5)

C#以及Oracle中的上取整、下取整方法

1.C#中: 上取整——Math.Ceiling(Double),即返回大于或等于指定双精度浮点数的最大整数(也可称为取天板值): eg:  Math.Ceiling(1.01)=2;      Math.Ceiling(1.37)=2; 下取整——Math.Floor(Double),即返回小于或等于指定双精度浮点数的最大整数(也可称为取地板值): eg:  Math.Floor(1.99) =1;       Math.Floor(1.87) =1; 2.Oracle中: 上取整——ceil