Codeforces round #262 (Div. 2) 460c. Present (Binary)

Source: Internet
Author: User


C. presenttime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Little Beaver is a beginner programmer, so Informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the Beaver has decided to prepare a present for her. planhe tedNFlowers in a row on his windowsill and started waiting for them to grow. however, after some time the beaver noticed that the flowers stopped growing. the Beaver thinks it is bad manners to present little flowers. so he decided to come up with some solutions.

There areMDays left to the birthday. The height ofI-Th flower (assume that the flowers in the row are numbered from 1NFrom left to right) is equalAIAt the moment. At each of the remainingMDays the beaver can take a special watering and waterWContiguous Flowers (he can do that only once at a day ). at that each watered flower grows by one height unit on that day. the beaver wants the height of the smallest flower be as large as possible in the end. what maximum height of the smallest flower can he get?


The first line contains space-separated IntegersN,MAndW(1? ≤?W? ≤?N? ≤? 105; 1? ≤?M? ≤? 105). The second line contains space-separated IntegersA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 109 ).


Print a single integer-the maximum final height of the smallest flower.

Sample test (s) Input
6 2 32 2 2 2 1 1
2 5 15 8

In the first sample beaver can water the last 3 flowers at the first day. on the next day he may not to water flowers at all. in the end he will get the following heights: [2, 2, 2, 3, 2, 2]. the smallest flower has height equal to 2. it's impossible to get height 3 in this test.


Give the initial height of N flowers, arranged from left to right, watering up to M days, can only be poured once a day, each time can make the continuous height of W flowers increase by 1 unit length, ask the maximum height of the Dwarf flower after the water is poured in the last M days.


Find out the best fit from the two-point enumeration height between the lowest and highest (remember + M) heights! See the code ......

The Code is as follows:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define ll _ int64const int maxn = 200017; ll a [maxn], B [maxn], V [maxn]; int main () {ll n, m, W; while (~ Scanf ("% i64d % i64d % i64d", & N, & M, & W) {ll low = 1e9, Top =-1; for (INT I = 1; I <= N; I ++) {scanf ("% i64d", & A [I]); if (a [I] <low) Low = A [I]; if (A [I]> top) Top = A [I];} top + = m; // the maximum height of LL mid, ANS =-1; while (low <= Top) {mid = (low + TOP)> 1; for (INT I = 1; I <= N; I ++) B [I] = max (mid-A [I], (LL) 0); // memset (v, 0, sizeof (V) days for each flower to be watered )); ll day = m; // Number of days ll C = 0; // Number of days that have been poured for (INT I = 1; I <= N; I ++) {C + = V [I]; B [I]-= C; // If (B [I]> 0) {day-= B [I]; If (day <0) // The number of days is not break enough; C + = B [I]; // poured B [I] days V [I + W]-= B [I]; // watered here B [I] = 0 ;}} if (day <0) // does not match, search for Top = mid-1 for the smaller value; else // continue to search for {ans = mid for the larger value; low = Mid + 1 ;}} printf ("% i64d \ n", ANS);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.