【CodeForces】【321E】Ciel and Gondolas

DP优化/四边形不等式


  这题……跟邮局那题简直一模一样吧……好水的E题……

  设dp[i][j]表示前 i 艘“gondola”坐了前 j 个人,那么方程即为$dp(i,j)=min\{ dp[i-1][k]+w[k][j] \} (i\leq k\leq j)$

  很明显$w(l,r)=\sum_{i=l}^r \sum_{j=l}^r u(i,j) /2$是满足四边形不等式的……那么根据决策单调性直接搞就行了……

 1 //CF 321E
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define rep(i,n) for(int i=0;i<n;++i)
 9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 using namespace std;
12 //#define debug
13 int getint(){
14     int v=0,sign=1; char ch=getchar();
15     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
16     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
17     return v*sign;
18 }
19 typedef long long LL;
20 const int N=100010,INF=~0u>>2;
21 /*******************tamplate********************/
22 LL u[4001][4001],n,m,w[4001][4001],dp[801][4001];
23 int s[801][4001];
24 int main(){
25 #ifndef ONLINE_JUDGE
26     freopen("input.txt","r",stdin);
27 //    freopen("output.txt","w",stdout);
28 #endif
29     n=getint(); m=getint();
30     F(i,1,n) F(j,1,n) u[i][j]=getint();
31     F(i,1,n) F(j,1,n) u[i][j]+=u[i-1][j]+u[i][j-1]-u[i-1][j-1];
32     
33     F(i,1,n) F(j,i+1,n) w[i][j]=u[j][j]-u[i-1][j]-u[j][i-1]+u[i-1][i-1];
34     
35     #ifdef debug
36     F(i,1,n) {F(j,1,n) printf("%3d",w[i][j]);puts("");}
37     #endif
38     F(i,1,m) F(j,1,n) dp[i][j]=INF;
39     F(i,1,n){
40         dp[1][i]=w[1][i];
41         s[1][i]=0;
42     }
43     F(i,2,m){
44         s[i][n+1]=n;
45         D(j,n,i)
46             F(k,s[i-1][j],s[i][j+1])
47                 if (dp[i-1][k]+w[k+1][j]<dp[i][j]){
48                     s[i][j]=k;
49                     dp[i][j]=dp[i-1][k]+w[k+1][j];
50                 }
51     }
52     printf("%I64d\n",dp[m][n]/2);
53     return 0;
54 }
View Code

 

转载于:https://www.cnblogs.com/Tunix/p/4392396.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、付费专栏及课程。

余额充值