「日常训练」 Single-use Stones (CFR476D2D)

题意(Codeforces 965D)

分析

我是激情看题解的,很惭愧。因为自己的算法喜提TLE了w
我参考的题解在这儿。官方题解用了二分的思想。
这个算法是怎么贪心的呢?就在while(p<i && frog[p]+frog[i]<=a[i])这一句。当第i
处已经满了的时候,我们就不考虑 [p+1,i1] [ p + 1 , i − 1 ] 到i的跳跃了。后面那句if是减去多余的。
其他地方结合注释和原题解的讲解来看。注意维护p指针始终和i的距离 <l < l <script type="math/tex" id="MathJax-Element-44">

代码

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pr(x) cout << #x << " = " << x << " ";
#define prl(x) cout << #x << " = " << x << endl;
#define ZERO(X) memset((X),0,sizeof(X))
#define ALL(X) (X).begin(),(X).end()
#define SZ(x) (int)x.size()

using namespace std;

typedef pair<int, int> PI;
typedef pair<pair<int, int>, int> PII;
typedef pair<pair<pair<int, int>, int>, int> PIII; 
using ull= unsigned long long;
using ll = long long;
using ld = long double;
#define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/*      debug("Precalc: %.3f\n", (double)(clock()) / CLOCKS_PER_SEC);
clock_t z = clock();
        solve();
        //debug("Test: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
template<typename T = int>
inline T read() {
    T val=0, sign=1;
    char ch;
    for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
        if (ch=='-') sign=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())
        val=val*10+ch-'0';
    return sign*val;
}

int main()
{
    int w,l; cin>>w>>l;
    int a[100005]; ZERO(a);
    int frog[100005]; ZERO(frog);
    a[w]=0x3f3f3f3f; // INF
    rep(i,1,w-1)
    {
        cin>>a[i];
        if(i<=l) frog[i]=a[i];
    }

    int p=1;
    rep(i,l+1,w)
    {
        while(i-p>l) ++p;
        while(p<i && frog[p]+frog[i]<=a[i])
            frog[i]+=frog[p++];
        if(p<i&&frog[i]!=a[i]) // greedy here. It's impossible for p+1~i-1 to jmp to a[i]. here, imply that frog[p]+frog[i]>a[i]
        {
            frog[p]-=(a[i]-frog[i]); // the a[i]-frog[i] frogs cannot jmp from p, because it's full already.
            frog[i]=a[i];
        }
    }
    cout<<frog[w]<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值