1. 程式人生 > >Make It Equal

Make It Equal

C. Make It Equal time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

There is a toy building consisting of nn towers. Each tower consists of several cubes standing on each other. The ii-th tower consists of hihicubes, so it has height hihi.

Let's define operation slice on some height HH as following: for each tower 

i">ii, if its height is greater than HH, then remove some top cubes to make tower's height equal to HH. Cost of one "slice" equals to the total number of removed cubes from all towers.

Let's name slice as good one if its cost is lower or equal to kk (

n">knk≥n).

Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.

Input

The first line contains two integers nn and kk (

5">1n21051≤n≤2⋅105, nk109n≤k≤109) — the number of towers and the restriction on slices, respectively.

The second line contains nn space separated integers h1,h2,,hnh1,h2,…,hn (1hi21051≤hi≤2⋅105) — the initial heights of towers.

Output

Print one integer — the minimum number of good slices you have to do to make all towers have the same heigth.

Examples input Copy
5 5
3 1 2 2 4
output Copy
2
input Copy
4 5
2 3 4 5
output Copy
2
Note

In the first example it's optimal to make 22 slices. The first slice is on height 22 (its cost is 33), and the second one is on height 11 (its cost is 44).

cf的一道特煩的題,超時到爆,然後發現可以用字首和。

首先把所有塔的高度減去其中的最小值,問題就變成了將塔消完所需要的步數。(可以不減,那樣最後就是>min而不是>0)

用字首和做一個數組brr,即這麼高的塔有幾個;

然後從最大的判斷相減就好了,(注意前一個高度等於原來的加後一個)(相加和都不超過給定的k)

#include<iostream>
#include<algorithm>
using namespace std;
int brr[200001]={0};
int main()
{
	int n,m;
	cin>>n>>m;
	int arr[200001];
	int min=200001,max=0;
	for(int i=1;i<=n;i++)
	{
		cin>>arr[i];
		if(arr[i]<min)
			min=arr[i];
		if(arr[i]>max)
			max=arr[i];
	}
	for(int i=1;i<=n;i++)
	{
		brr[arr[i]-min]++;
	}
	int k=0,sum=0;
	for(int i=max-min;i>0;i--)
	{
		k+=brr[i];
		brr[i-1]+=brr[i];
		if(k>m)
		{
			brr[i-1]-=brr[i];
			i++;
			k=0;
			sum++;
		}
	}
	if(k>0)
		sum++;
	cout<<sum;
 } 

  

相關推薦

codeforces Educational Codeforces Round 52 div2C - Make It Equal

題意 有 n n n座塔,每座塔都有一個高度

C. Make It Equal

題意:給出n個高度,問最少通過多少次代價最多為k的消減能夠使得所有的高度相同。(代價:消減的總高度) 題解:可以考慮從他的最高高度開始一階一階的減,一直減到最小的那個高度,通過記錄每次減一的代價與k進行比較即可得出結果。

Make It Equal(codeforces 1065c 貪心)

There is a toy building consisting of n n towers. Each tower consists of several cubes standing on each other. The i-th tower consists of hi cubes

Codeforces1065C——Make It Equal

題目省略 思路不清晰,後面想複雜了,然後就tle了 題意是給一個數列,代表每個建築的高度,然後水平一刀切下去,每次拿走的不能超過k個,然後要切多少次才能切平 首先讀取每一個高度的時候記錄下最大和最小的,最小的其實就是切平的位置,所以以下的都不用管了,然後先記錄每一個高度

Educational Codeforces Round 52 (Rated for Div. 2) C. Make It Equal

題目:https://codeforces.com/contest/1065/problem/C 這道題是服了,我的演算法是貪心模擬,貪心的找切片,寫的比較慢,因為是模擬,所以考慮的比較多,而且變數比較多。。。。。。 賽後居然被hack了。。。好氣,為什麼呢,就是少考慮了初始時高度都一樣

Make It Equal

C. Make It Equal time limit per test 2 seconds memory limit per test 256 megabytes input standard input

cf1065 C Make It Equal

There is a toy building consisting of nn towers. Each tower consists of several cubes standing on each other. The ii-th tower consists o

Make It Equal(codeforces 1065c 貪心)

There is a toy building consisting of n n towers. Each tower consists of several cubes standing on each other. The i-th tower consi

codeforces 1065 C. Make It Equal(差分+貪心)

題意:有n座塔,塔高h[i],每次給定高度H對他們進行削切,要求每次削掉的所有格子數不能超過k個,輸出最少削幾次才能使所有塔的高度相同。 思路: 比較明顯,只要知道對應的每層高度有多少個格子,然後再從高到低下來使其符合<k的條件即可。 而去求得每層高度對應多少

codeforces1065C、Make It Equal(線段樹+二分)

                                                                                            C. Make It Equal time limit per test 2 secon

Make It Equal(思維+細心)

C. Make It Equal time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Th

UVa 10176 - Ocean Deep ! - Make it shallow !!

size make algo 推斷 math div ont names tdi 題目:給你一個二進制串。推斷是否能被131071 整除。 分析:數論。直接模擬除法運算,求出余數就可以。 說明:註意可能有非法字符(比如空格)。 #include

洛谷 CF1043F Make It One 解題報告

CF1043F Make It One 題意 從一堆數中選擇最少的數,使它們的\(\gcd=1\) 輸入輸出格式 輸入格式 第一行:一個正整數\(n\)。 第二行:\(n\)個正整數,給出了這個數列。 輸出格式 一行,\(-1\)(如果任意選擇都不能得到\(1\)分)或一個正整數(表示選擇的數的

Make It Real 行臻·致遠” ——戴爾科技集團與客戶及合作伙伴共聚,體驗、暢想和實現數字化未來!

正值戴爾中國成立20週年之際,2018戴爾科技峰會在北京國家會議中心隆重召對開。此次峰會以“行臻•致遠”為主題,以與廣大客戶及合作伙伴攜手共聚並體驗、暢想和實現數字化未來為目標。 大會現場 本屆戴爾科技峰會是首次在中國以戴爾科技集團名義

Codeforces Round #519 by Botan Investments F. Make It One (莫比烏斯反演)

題目連結:F 參考部落格:https://blog.csdn.net/qq_32506797/article/details/83507061 可以再看看用組合數學+dp寫的,對比下發現,差不多https://blog.csdn.net/LJD201724114126/article/d

Codeforces Round #519 by Botan Investments F. Make It One (組合數學+dp)

題目連結:F 題意:給一串數字,讓你從中找出最少的數字,使得它們的最大公約數為1。   參考連結:https://blog.csdn.net/Link_Ray/article/details/83627570   題解:由2 * 3 * 5 * 7 * 1

If you can take it, you can make it.

緣起lemon師傅在安全客裡發表的一篇文章,總結的很詳細,學到了很多姿勢。但是在此我還想畫蛇添足的做一些補充及解釋。補充一下命令執行的漏洞。 空格繞過 < 符號 %09 符號需要php環境,這裡就不搭建啦,見諒) $IFS$9 符號${IFS} 符號 這裡解釋一下${IFS},$IFS,$IFS

An introduction to Generative Art: what it is, and how you make it

An introduction to Generative Art: what it is, and how you make itMandelbrot’s Fractal is derived from a deceptively simple equationGenerative art can be a

Tech Advances Make It Easier to Assign Blame for Cyberattacks

"All you have to do is look at the attacks that have taken place recently--WannaCry, NotPetya and others--and see how quickly the industry and government i

Codeforces Round #529 (Div. 3) F. Make It Connected(最小生成樹)

AC 最小生成樹,建邊的時候不需要 N 2