[Codeforces 115E]Linear Kingdom Races

题目大意:

有n块地,初始是荒地。你可以把某些荒地开垦(需要花费相应的价值\(a_i\)(正整数)),然后这些荒地就可以种田。

现在有m年,每年要在l到r区间内种田,获得p(正整数)的价值(必须保证l~r都已经开荒,否则不能种田)。

问最大收益。

解题思路:

DP。

设F[i][j]表示前i块地,最后有连续的j块地已开荒的最大收益。

则\(F[i+1][0]=max\{F[i][j]\}\)。不开荒,则中间断了,所以连续的值只有0了。

F[i+1][j+1]=F[i][j]-a[i]+v。开荒,则花费价值,而且可能会有一些年份可以种田了,则加上这些收益(v是加上后能多出来的种田收益)。

于是我们要记录下以每个值作为右端点的种田个数。

答案即为\(max\{F[n][i]\}\)

发现这是个时空复杂度都是\(O(n^2)\)的东西。

首先第一维可以滚掉。

然后,考虑每个\(a[i]\)都要在整个区间减一遍,而每年种田的价值也会对一段区间有影响。

所以考虑线段树优化。

线段树每个节点记录这个节点下面的儿子的状态的最优值。

然后发现枚举i时,之前的状态都要向右偏移一格,非常麻烦。

倒着建状态就好辣~喵~o( =∩ω∩= )m

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using LoveLive=long long;
const int N=2e5+5;
LoveLive d[N*4],tag[N*4],a[N];
int n,m,L,R;
LoveLive add;
vector<pair<int,LoveLive>>v[N];
inline LoveLive max(LoveLive&a,LoveLive&b){return a>b?a:b;}
inline void pd(int&o){
    if(tag[o]){
        int l=o<<1,r=l|1;
        d[l]+=tag[o];
        d[r]+=tag[o];
        tag[l]+=tag[o];
        tag[r]+=tag[o];
        tag[o]=0;
    }
}
void add_1(int l,int r,int o){
    if(l==r)d[o]+=add;else{
        pd(o);
        int mid=l+r>>1;
        if(L<=mid)add_1(l,mid,o<<1);else
        add_1(mid+1,r,o<<1|1);
        d[o]=max(d[o<<1],d[o<<1|1]);
    }
}
void add_lot(int l,int r,int o){
    if(L<=l&&r<=R)d[o]+=add,tag[o]+=add;else{
        int mid=l+r>>1;
        pd(o);
        if(L<=mid)add_lot(l,mid,o<<1);
        if(mid<R)add_lot(mid+1,r,o<<1|1);
        d[o]=max(d[o<<1],d[o<<1|1]);
    }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>a[i];
    for(int i=1,l,r;i<=m;++i){
        cin>>l>>r>>add;
        v[r].push_back(make_pair(l,add));
    }
    for(int i=1;i<=n;++i){
        L=n-i,add=d[1];
        add_1(0,n,1);
        ++L,R=n,add=-a[i];
        add_lot(0,n,1);
        for(auto it:v[i]){
            L=n-it.first+1,R=n,add=it.second;
            add_lot(0,n,1);
        }
    }
    cout<<d[1]<<endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/Mrsrz/p/9385612.html

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值