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] 的单调性即可。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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.xv)
                swap(u,v);
            limit[v]=max(limit[v],u);
        }

        solve(n);
    }
    return 0;
}


你可能感兴趣的:(DP)