# 【DP】 (ASC 44) Braess's Paradox

DP。。先预处理i到j的距离，可以用前缀和列出两个方程。。。然后n^2DP可以解出最大最小值。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 5005
#define maxm 400005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}

double dp[2][maxn];
double a[maxn], b[maxn], c[maxn], d[maxn];
int n;

double calc(int t1, int t2)
{
t1--;
double aa = a[t2] - a[t1];
double bb = b[t2] - b[t1];
double cc = c[t2] - c[t1];
double dd = d[t2] - d[t1];
if(fabs(aa + cc) < eps) {
return min(bb, dd);
}
double x = (cc + dd - bb) / (aa + cc);
if(x > 1) return aa + bb;
else if(x < 0) return cc + dd;
else return aa * x + bb;
}

void work()
{
for(int i = 1; i <= n; i++) {
scanf("%lf%lf%lf%lf", &a[i], &b[i], &c[i], &d[i]);
a[i] += a[i-1];
b[i] += b[i-1];
c[i] += c[i-1];
d[i] += d[i-1];
}
printf("%.11f\n", calc(1, n));
double ans = 0;
for(int i = 1; i <= n; i++) {
ans += calc(i, i);
}
printf("%.11f\n", ans);
for(int i = 1; i <= n; i++) dp[0][i] = 1e16, dp[1][i] = 0;
dp[0][0] = dp[1][0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
dp[0][i] = min(dp[0][i], dp[0][j-1] + calc(j, i));
dp[1][i] = max(dp[1][i], dp[1][j-1] + calc(j, i));
}
}
printf("%.11f\n", dp[0][n]);
printf("%.11f\n", dp[1][n]);

}

int main()
{
freopen("braess.in", "r", stdin);
freopen("braess.out", "w", stdout);
while(scanf("%d", &n)!=EOF) {
work();
}

return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：【DP】 (ASC 44) Braess's Paradox 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)