[WQS二分套WQS二分] Codeforces #739E. Gosha is hunting

O(n3) DP 很显然。要优化就只能 WQS 二分了。每种食物肯定是都用完的,所以相当于强制选若干个 A 物品,若干个 B 物品。发现物品选越多,收益是会增加的越来越慢的,所以这两维都可以 WQS 二分。就能做到 O(nlog2n) ,很优美。
http://codeforces.com/blog/entry/49691

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2005;
int n,A,B,num_a[maxn],num_b[maxn];
double a[maxn],b[maxn],f[maxn];

void Solve(double cA,double cB){
    f[0]=0; num_a[0]=num_b[0]=0;
    for(int i=1;i<=n;i++){
        f[i]=f[i-1]; num_a[i]=num_a[i-1]; num_b[i]=num_b[i-1];
        if(f[i-1]+a[i]-cA>f[i]){
            f[i]=f[i-1]+a[i]-cA;
            num_a[i]=num_a[i-1]+1; num_b[i]=num_b[i-1];
        }
        if(f[i-1]+b[i]-cB>f[i]){
            f[i]=f[i-1]+b[i]-cB;
            num_a[i]=num_a[i-1]; num_b[i]=num_b[i-1]+1;
        }
        if(f[i-1]+1-(1-a[i])*(1-b[i])-cA-cB>f[i]){
            f[i]=f[i-1]+1-(1-a[i])*(1-b[i])-cA-cB;
            num_a[i]=num_a[i-1]+1; num_b[i]=num_b[i-1]+1;
        }
    }
}
int main(){
    freopen("cf739E.in","r",stdin);
    freopen("cf739E.out","w",stdout);
    scanf("%d%d%d",&n,&A,&B);
    for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
    for(int i=1;i<=n;i++) scanf("%lf",&b[i]);
    double L1=0,R1=1,mid1,L2=0,R2=1,mid2;
    while(R1-L1>=0.00000001){
        mid1=(L1+R1)/2;
        L2=0,R2=1;
        while(R2-L2>=0.00000001){
            mid2=(L2+R2)/2;
            if((Solve(mid1,mid2),num_b[n])>B) L2=mid2; else R2=mid2;    
        }
        if((Solve(mid1,R2),num_a[n])>A) L1=mid1; else R1=mid1;
    }
    Solve(R1,R2); printf("%.5lf\n",f[n]+R1*A+R2*B);
    return 0;
}
### 动态规划中的凸优化 动态规划(Dynamic Programming, DP)是一种通过分解子问题来解决复杂问题的方法。然而,在某些情况下,标准的动态规划方法可能效率较低,因此引入了凸优化技术以加速计算过程。当状态转移方程具有单调性和凸性时,可以利用这些性质进一步减少时间复杂度。 #### 凸优化的核心思想 如果一个问题的状态转移函数满足某种形式的凸性条件,则可以通过维护决策点集合的方式降低每次更新的时间开销。具体来说,对于形如 \( dp[i] = \min_{j} (dp[j] + cost(i,j)) \) 的状态转移方程,其中 \( cost(i,j) \) 是关于 \( j \) 单调或者呈现特定形状的函数,那么可以用斜率优化等技巧提高性能[^1]。 ```python def convex_optimization_dp(n, costs): """ 使用凸优化处理动态规划问题的一个简单例子。 参数: n: 状态数量 costs: 成本数组 返回: 最优解的结果列表 """ dp = [0] * (n + 1) deque = [] for i in range(1, n + 1): while len(deque) >= 2 and slope(deque[-2], deque[-1]) <= -costs[i]: deque.pop() k = deque[0] dp[i] = dp[k] + costs[i] * (i - k) while len(deque) >= 2 and cross_product(deque[-2], deque[-1], i) <= 0: deque.pop() deque.append(i) return dp[n] def slope(x, y): """ 计算两点之间的斜率 """ return (f(y) - f(x)) / (y - x) def cross_product(a, b, c): """ 判断三点共线情况下的叉积方向 """ return (b-a)*(g(c)-g(b))-(c-b)*(g(b)-g(a)) ``` --- ### WQS二分算法详解 WQS二分(Weighted Queue Sliding Binary Search)主要用于求解带有额外约束条件的最优化问题。它通常应用于组合优化领域,尤其是涉及分配资源或物品的情况下。其核心在于调整目标函数中的权重参数,使得最终方案既满足约束又达到全局最优。 #### 实现细节 假设我们有一个背包容量为C的商品集S={a_1,a_2,...,a_n},每件商品有重量w_i和价值v_i,并且存在一个附加限制——最多只能选K件商品。此时可以直接采用如下策略: 1. 定义辅助变量λ作为惩罚因子; 2. 构造新的效用函数F&#39;(x)=Σ(v_i-w_i*λ),并尝试最大化该表达式的值; 3. 调整λ直至选出恰好k个元素为止; 这种方法能够有效应对多种变体问题,比如多重背包、区间覆盖等问题。 ```python from bisect import insort_right def wqs_binary_search(items, capacity, max_count): low, high = min(item[&#39;weight&#39;] for item in items), sum(item[&#39;value&#39;]/item[&#39;weight&#39;] for item in items) def check(lambda_val): total_weight = 0 count = 0 sorted_items = sorted((item[&#39;value&#39;] - lambda_val * item[&#39;weight&#39;], idx) for idx,item in enumerate(items)) res = [] current_sum = 0 for val,idx in reversed(sorted_items[:max_count]): if total_weight + items[idx][&#39;weight&#39;]<=capacity: total_weight +=items[idx][&#39;weight&#39;] current_sum+=val+lambda_val*items[idx][&#39;weight&#39;] insort_right(res,(current_sum,-total_weight)) best=next(iter(res or [(float(&#39;-inf&#39;),)]))[0] return best>=sum(itm[&#39;value&#39;]for itm in items[:len(res)])and(best,total_weight)==res[-1] eps = 1e-7 while abs(high-low)>eps: mid=(low+high)/2. flag=check(mid) if not flag: low=mid else: high=mid opt_lambda=(low+high)/2. _,solution=check(opt_lambda) return solution,opt_lambda ``` --- ### 带权二分的应用场景 带权二分本质上是对传统二分查找的一种扩展,允许我们在搜索过程中考虑不同选项的重要性差异。这种技术广泛用于图论、网络流等领域,特别是在寻找最小割/最大流路径时非常有用。 例如,在Dijkstra算法中加入优先级队列支持负边权的情况就是一种典型实例。通过对节点间距离赋予适当权重系数,我们可以更灵活地控制寻路行为,从而适应更多实际需求。 ```python import heapq def dijkstra_with_weights(graph, start_node, weight_func=lambda u,v,w:w): distances = {node : float(&#39;infinity&#39;) for node in graph} previous_nodes = {} pq = [] distances[start_node]=0 heapq.heappush(pq,[distances[start_node],start_node]) while pq: curr_dist,node=heapq.heappop(pq) if curr_dist>distances[node]: continue for neighbor,edge_weight in graph[node].items(): distance=curr_dist+weight_func(node,neighbor,edge_weight) if distance<distances[neighbor]: distances[neighbor]=distance previous_nodes[neighbor]=node heapq.heappush(pq,[distance,neighbor]) return distances,previous_nodes ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值