Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)

这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了。

二分答案时,先确定答案肯定在哪个区间内。然后二分判断,关键在于怎么判断每次枚举的这个答案行不行。

我是用a[i]数组表示初始时花的高度,b[i]表示要达到当前枚举的答案(即mid的值)需要这朵花再涨多少。这两个数组很好算,关键是一次浇连续的w朵花,如何更新区间(暴力的O(n2)的去更新就超时了)?可以用线段树,但是这道题没有涉及区间查询,就是在一个数组上更新区间,用线段树未免小题大做。那么其实这种更新就用延迟标记的思想(懒操作)就可以O(n)的解决了。具体方法参见这道很裸的题:hdu4970

lazy[]这个标记数组的作用其实就是能记录更新区间到哪里停止.

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
const int maxn=110009;
int n,m,w,a[maxn],b[maxn],lazy[maxn],ans=0;
/*lazy[]这个标记数组的作用其实就是能记录更新区间到哪里停止*/
int main()
{
    //freopen("in6.txt","r",stdin);
    scanf("%d%d%d",&n,&m,&w);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    int l=1,r=maxn+INF,mid;
    while(l<=r)
    {
        memset(lazy,0,sizeof(lazy));
        mid=(r+l)/2;
        for(int i=1; i<=n; i++) b[i]=max(0,mid-a[i]);
        int tm=m,x=0;
        for(int i=1;i<=n;i++)//开始浇水
        {
            x+=lazy[i];//要先看这一步是不是有标记
            b[i]-=x;//把前面对影响到当前b[i]的浇水操作浇上
            if(b[i]>0)//还大于0,那么就必须要从它开始再来一段浇水了
            {
                tm-=b[i];
                if(tm<0) break;//天数限制到了
                lazy[i+w]-=b[i];
                x+=b[i];
            }
        }
        if(tm<0) r=mid-1;
        else
        {
            ans=mid;
            l=mid+1;
        }
    }
    printf("%d\n",ans);
}
时间: 2025-01-18 17:47:23

Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)的相关文章

Codeforces Round #256 (Div. 2)D 二分答案

D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication tabl

Codeforces Round #262 (Div. 2) 总结:二分

B. Little Dima and Equation 思路:本来前两题都很快做出来的,然后觉得ranting应该可以增加了.然后觉得代码没问题了,又想到了一组cha人的数据,然后就锁了,然后刚锁就被别人cha了看了代码才发现尼玛忘了判断小于10^9了,然后C反正想了好多种方法都不会就没心情了,就这样rating又降了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #in

Codeforces Round #262 (Div. 2) 460C. Present(二分)

题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little beaver is a beginner programmer, so informatics is his favorite subjec

Codeforces Round #262 (Div. 2)

Codeforces Round #262 (Div. 2) A:水题,直接不断模拟即可 B:由于s(x)大小最大到1e9,所以数位和最多为81,这样只要枚举s(x),就只要枚举1到81即可,然后在计算出x,判断是否符合,符合就加进答案 C:二分高度,然后判断的时候for一遍,每次不符合的位置就去浇水,从左往右推一遍即可 D:构造,如果k >= 5, 那么就可以直接放偶数,奇数,偶数,奇数,由于偶数和偶数+1异或必然为1,所以这样放4个异或和就到最小的0了,然后k = 1,2,4都可以特判到,关

Codeforces Round #262 (Div. 2) C

题目: C. Present time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put o

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #262 (Div. 2) B

题目: B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following pr