Codeforces 856A Curfew 贪心+二分

D. Curfew
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Instructors of Some Informatics School make students go to bed.

The house contains n rooms, in each room exactly b students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to n. Initially, in i-th room there are ai students. All students are currently somewhere in the house, therefore a1 + a2 + ... + an = nb. Also 2 instructors live in this house.

The process of curfew enforcement is the following. One instructor starts near room 1 and moves toward room n, while the second instructor starts near room n and moves toward room 1. After processing current room, each instructor moves on to the next one. Both instructors enter rooms and move simultaneously, if n is odd, then only the first instructor processes the middle room. When all rooms are processed, the process ends.

When an instructor processes a room, she counts the number of students in the room, then turns off the light, and locks the room. Also, if the number of students inside the processed room is not equal to b, the instructor writes down the number of this room into her notebook (and turns off the light, and locks the room). Instructors are in a hurry (to prepare the study plan for the next day), so they don't care about who is in the room, but only about the number of students.

While instructors are inside the rooms, students can run between rooms that are not locked and not being processed. A student can run by at most d rooms, that is she can move to a room with number that differs my at most d. Also, after (or instead of) running each student can hide under a bed in a room she is in. In this case the instructor will not count her during the processing. In each room any number of students can hide simultaneously.

Formally, here is what's happening:

  • A curfew is announced, at this point in room i there are ai students.
  • Each student can run to another room but not further than d rooms away from her initial room, or stay in place. After that each student can optionally hide under a bed.
  • Instructors enter room 1 and room n, they count students there and lock the room (after it no one can enter or leave this room).
  • Each student from rooms with numbers from 2 to n - 1 can run to another room but not further than d rooms away from her currentroom, or stay in place. Each student can optionally hide under a bed.
  • Instructors move from room 1 to room 2 and from room n to room n - 1.
  • This process continues until all rooms are processed.

Let x1 denote the number of rooms in which the first instructor counted the number of non-hidden students different from b, and x2 be the same number for the second instructor. Students know that the principal will only listen to one complaint, therefore they want to minimize the maximum of numbers xi. Help them find this value if they use the optimal strategy.

Input

The first line contains three integers nd and b (2 ≤ n ≤ 100 0001 ≤ d ≤ n - 11 ≤ b ≤ 10 000), number of rooms in the house, running distance of a student, official number of students in a room.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109), i-th of which stands for the number of students in the i-th room before curfew announcement.

It is guaranteed that a1 + a2 + ... + an = nb.

Output

Output one integer, the minimal possible value of the maximum of xi.

Examples
input
Copy
5 1 1
1 0 0 0 4
output
Copy
1
input
Copy
6 1 2
3 8 0 1 0 0
output
Copy
2
Note

In the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed. This way, the first instructor writes down room 2, and the second writes down nothing.

In the second sample one of the optimal strategies is the following: firstly all students in room 1 hide, all students from room 2 run to room 3. On the next stage one student runs from room 3 to room 4, and 5 students hide. This way, the first instructor writes down rooms 1 and 2, the second instructor writes down rooms 5 and 6.


思路产生的过程消耗了小辣鸡不少时间,关键有两点。第一,所有同学逃跑的路线是不交叉的,完全可以让前半部分同学对付前面的宿管,后半部分的同学对付后面的宿管。从前往后,能挽回的宿舍尽量挽回,不能挽回的宿舍,就让其中同学的数量等于0,后半部分同理。第二,二分前后部分同学的分界点,如果前面少人的宿舍多于后面,那么一定是前面的同学不够用,分界点后移;反之,则前移。

代码没啥不好实现的,但过程中犯了个极其低级的错误:int tmp=min(pos,i+i*d)。 这导致了溢出。

贴上大破代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;

long long n,d,b;
long long a[100010];
long long sum[100010];

int main()
{
    scanf("%lld%lld%lld",&n,&d,&b);
    long long ans=100010;
    for (long long i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    memset(sum,0,sizeof(sum));
    for (long long i=1; i<=n; i++)
    {
        sum[i]=sum[i-1]+a[i];
    }
    long long l=1; long long r=n;
    long long pos;
    while (l<=r)
    {
        pos=(l+r)/2;
        long long sl=0; long long sr=0;
        long long sum1=0;
        for (long long i=1; i<=pos; i++)
        {
            if (a[i]>b)
            {
                sl+=a[i]-b;
            }
            else
            {
                long long tmp=min((long long)pos,(long long)((long long)i+(long long)i*(long long)d));
                if (sum[tmp]-sum[i-1]+sl<b)
                {
                    sum1++;
                    sl+=a[i];
                }else
                {
                    sl-=b-a[i];
                }
            }
        }
        long long sum2=0;
        for (long long i=n; i>pos; i--)
        {
            if (a[i]>b)
            {
                sr+=a[i]-b;
            }
            else
            {
                long long tmp=max(pos,i-d*(n-i+1));
                if (sum[i]-sum[tmp-1]+sr<b)
                {
                    sum2++;
                    sr+=a[i];
                }else
                {
                    sr-=b-a[i];
                }
             }
        }
        ans=min(ans,max(sum1,sum2));
        if (sum1>sum2) l=pos+1;
        else if (sum1<sum2) r=pos-1;
        else break;
    }
    printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值