zoukankan      html  css  js  c++  java
  • Help Jimmy[dp]

    在这里插入图片描述

    #include <iostream>
    #include <string>
    #include <math.h>
    #include<algorithm>
    #include<cstdio>
    #include<string.h>
    using namespace std;
    const int N = 1010;
    const int inf = 0x3f;
    struct time
    {
    	int x1, x2, h;
    }a[N];
    int dp[N][2];
    
    bool cmp(time a, time b)
    {
    	return a.h < b.h;
    }
    int main()
    {
    	int t, x, y, i, n, max_h;
    	cin >> t;
    	while (t--)
    	{
    		cin >> n >> x >> y >> max_h;
    		for (int i = 1; i <= n; i++)
    		{
    			cin >> a[i].x1 >> a[i].x2 >> a[i].h;
    		}
    		sort(a + 1, a + n + 1,cmp);
    		a[n + 1].x1 = a[n + 1].x2 = x;
    		a[n + 1].h = y;
    		a[0].x1 = -inf, a[0].x2 = inf, a[0].h = 0;//将出发点和地面当做一个平台
    		memset(dp, 0, sizeof(dp));
    		for (int i = 1; i <= n+1; i++)
    		{
    			int lflag = 0, rflag = 0;
    			for (int j = i - 1; j >= 0 && a[i].h - a[j].h <= max_h; j--)
    			{
    				if (a[j].x1 <= a[i].x1 && a[j].x2 >= a[i].x1)
    				{
    					lflag = 1;
    					if (j == 0)
    						dp[i][0] = a[i].h;
    					else
    						dp[i][0] = a[i].h - a[j].h + min(dp[j][0] + a[i].x1 - a[j].x1, dp[j][1] + a[j].x2 - a[i].x1);
    					break;
    				}
    			}
    			if (lflag == 0)
    				dp[i][0] = inf;
    			for (int j = i - 1; j >= 0 && a[i].h - a[j].h <= max_h; j--)
    			{
    				if (a[j].x2 >= a[i].x2 && a[j].x1 <= a[i].x2)
    				{
    					rflag = 1;
    					if (j == 0)
    						dp[i][1] = a[i].h;
    					else
    						dp[i][1] = a[i].h - a[j].h + min(dp[j][1] + a[j].x2 - a[i].x2, dp[j][0] + a[i].x2 - a[j].x1);
    					break;
    				}
    			}
    			if (rflag == 0)
    				dp[i][1] = inf;
    		}
    		cout << min(dp[n + 1][0], dp[n + 1][1]) << endl;
    	}
    }
    
  • 相关阅读:
    6-2 对象克隆
    5-2 equal getClass or instanceOf
    6-2 回调
    6-1 接口的默认方法
    认识ExtJS(05)--
    认识ExtJS(04)--常见Web框架的ExtJS改造
    MyEclipse快捷键全
    ExtJS4.1自带API打不开的问题解决
    浅析十三种常用的数据挖掘的技术&五个免费开源的数据挖掘软件
    C++ 多态
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811933.html
Copyright © 2011-2022 走看看