【DP】 (ASC 44) Braess's Paradox
2015-07-10 20:44
423 查看
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> #pragma comment(linker, "/STACK:16777216") 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;} // head 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] ); printf("%.11f\n", dp[1] ); } int main() { freopen("braess.in", "r", stdin); freopen("braess.out", "w", stdout); while(scanf("%d", &n)!=EOF) { work(); } return 0; }
相关文章推荐
- Android API 翻译之 UsbDevice
- [LeetCode][Java] Implement strStr()
- 锁排查--实例
- 设计模式读书笔记:Interpreter(解释器)
- Java学习笔记
- 浮点数的范围和精度
- javaweb 待填坑
- 结构体指针
- 系统设计题分析
- java基础-输入流-读取文本文件中数据至字符串数组
- 严重: A child container failed during start
- Python编码问题
- Java集合 之 Queue集合
- 正则表达式
- [LeetCode] Valid Palindrome
- JAVA多线程和并发执行
- 蚂蚁【A001】
- 奔跑中的2015——运维在路上
- vmware虚拟机下ubuntu设置1080p的有效方法
- 树的实现遍历及应用