我是靠谱客的博主 缓慢发夹,这篇文章主要介绍ACdream群OJ 1074 风之国 单调队列优化DP,现在分享给大家,希望可以做个参考。

题目连接:http://acdream.info/problem?pid=1074

思路:

首先,按xi值排序,处理顺序,按排序后的顺序依次给城市编号。记矛盾关系为[u,v](排序后的点),按v值从小到大排序。思考,发现v值一样的矛盾关系,只需取其中最大的u则可。

用dp[i]表示:处理了v值为1-i的所有矛盾关系的最小花费。dp[i]的具体怎么转移呢?枚举最后一条删除的边,得到转移方程dp[i] = min( dp[j] + x[j+1]-x[j] )。需要注意,不是任意一个边都可以作为最后一条边的。j的最小值是i以及i之前的最小的u。

然后,因为转移中的j是不降的,则维护 dp[j] + x[j+1]-x[j] 的单调性即可。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//#pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<cmath> #include<cctype> #include<string> #include<algorithm> #include<iostream> #include<ctime> #include<map> #include<set> using namespace std; #define MP(x,y) make_pair((x),(y)) #define PB(x) push_back(x) typedef long long LL; //typedef unsigned __int64 ULL; /* ****************** */ const int INF=1000111222; const double INFF=1e100; const double eps=1e-8; //const LL mod=1000000007; const int NN=100010; const int MM=2000010; /* ****************** */ struct node { int x,id; }a[NN]; int dui[NN],limit[NN],q[NN],cost[NN]; int dp[NN]; bool cmp(node aa,node bb) { return aa.x<bb.x; } void solve(int n) { int i,head,tail,temp; int pre=limit[1]; q[head=tail=0]=0; cost[0]=0; for(i=1;i<=n;i++) { pre=max(pre,limit[i]); while(q[head]<pre) head++; dp[i]=cost[head]; temp=dp[i]+a[i+1].x-a[i].x; while(head<=tail && temp<=cost[tail]) tail--; q[++tail]=i; cost[tail]=temp; } printf("%dn",dp[n]); } int main() { int n,m,i; int u,v; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&a[i].x); a[i].id=i; } sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++) { dui[ a[i].id ]=i; } memset(limit,-1,sizeof(limit)); for(i=0;i<m;i++) { scanf("%d%d",&u,&v); u=dui[u]; v=dui[v]; if(u>v) swap(u,v); limit[v]=max(limit[v],u); } solve(n); } return 0; }


最后

以上就是缓慢发夹最近收集整理的关于ACdream群OJ 1074 风之国 单调队列优化DP的全部内容,更多相关ACdream群OJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部