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;
    	}
    }
    
  • 相关阅读:
    spring 定时任务 taskScheduler详解
    shiro.ini 配置详解
    nginx : server_name localhost 和 chrome : Provisional headers are shown
    参数文件管理
    归档日志管理
    日志文件管理
    控制文件管理
    数据文件管理
    OGG配置参数样例
    OGG配置案例一(源表与目标表结构一致)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811933.html
Copyright © 2011-2022 走看看