Codeforces 551c-gukiz hates Boxes (two points and greedy)

Source: Internet
Author: User

Test instructions

Is that n students help the Professor carry the box, the box into the M-heap, each student can choose two operations per second

Operation 1, for from I heap to the i+1 heap,

Operation 2, from the I-1 heap box to help hold a box

Ask for the minimum amount of time to hold all the boxes, and each student can act at the same time every second.

Exercises

Two minutes and then the current time, a police station some students, so that he can walk to the farthest distance

Code:

#include <stdio.h>
int flag, value[100005], value2[100005], N, m;
void find (Long long int mid)
{
Long Long int temp = MID, Fla = 0;
Temp--;
if (flag = = N) return;
if (temp = = 0) return;
while (1)
{
while (!value2[flag])
{
Flag + +;
IF (FLA) temp--;
if (flag = = N) return;
if (temp = = 0) continue;
}
if (!FLA) temp-= flag;
FLA = 1;
if (Value2[flag] <= temp)
{

Temp-= Value2[flag];
Value2[flag] = 0;
if (flag = = n-1) flag + +;
if (flag = = N) return;
if (temp = = 0) continue;
Flag + +, temp--;
if (flag = = N) return;
if (temp = = 0) continue;
}
Else
{
Value2[flag]-= temp;
Return
}
}
}
BOOL Slove (long long int mid)
{
for (int i = 0; i < n; i++)
Value2[i] = Value[i];
Flag = 0;
for (int i = 1; I <= m; i++)
{
Find (mid);
if (flag = = n) break;
}
if (flag = = N) return 1;
return 0;
}
int main ()
{
while (scanf ("%d%d", &n, &m)! = EOF)
{
A long long int sum = 0;
for (int i = 0; i < n; i++)
scanf ("%d", &value[i]), sum + = value[i];
sum + = n;
Long long int l = 0, R = Sum, Mid, Ans =-1;
while (L <= R)
{
Mid = (r+l)/2;
if (Slove (mid))
{
R = mid-1;
Ans = mid;
}
else L = mid + 1;
}
printf ("%lld\n", Ans);
}
}

Codeforces 551c-gukiz hates Boxes (two points and greedy)

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: info-contact@alibabacloud.com 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.