HDU 3757 dp

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 4E3 + 10;
const long long INF = 1LL << 60;
short path[maxn][maxn];
long long f[maxn];
struct Node
{
        long long d;
        int num, sh;
};
int n, m, T, kase;
Node x[maxn], y[maxn];
bool cmpd(const Node &a, const Node &b)
{
        return a.d < b.d;
}
bool cmpnum(const Node &a, const Node &b)
{
        return a.num < b.num;
}
void solve(int i, int j)
{
        if (i != 1) solve(i - 1, path[i][j]);
        x[i].sh = y[j].num;
}
int main(int argc, char const *argv[])
{
        scanf("%d", &T);
        while (T--)
        {
                scanf("%d", &n);
                for (int i = 1; i <= n; i++)
                        scanf("%I64d", &x[i].d), x[i].num = i;
                scanf("%d", &m);
                for (int i = 1; i <= m; i++)
                        scanf("%I64d", &y[i].d), y[i].num = i;
                sort(x + 1, x + 1 + n, cmpd);
                sort(y + 1, y + 1 + m, cmpd);
                for (int i = 0; i <= max(n, m); i++)
                        f[i] = INF;
                f[1] = abs(x[1].d - y[1].d);
                for (int i = 2; i <= n; i++)
                        for (int j = min(i, m); j >= 1; j--)
                        {
                                if (f[j] < f[j - 1])
                                {
                                        f[j] = f[j] + abs(x[i].d - y[j].d);
                                        path[i][j] = j;
                                }
                                else
                                {
                                        f[j] = f[j - 1] + abs(x[i].d - y[j].d);
                                        path[i][j] = j - 1;
                                }
                        }
                printf("%I64d\n", f[m]);
                solve(n, m);
                sort(x + 1, x + 1 + n, cmpnum);
                for (int i = 1; i < n; i++)
                        printf("%d ", x[i].sh);
                printf("%d\n", x[n].sh);
        }
        return 0;
}

分别对人和shelter从小到大排序之后再dp,这样可以消除后效性。

设f[i][j]表示第i个人放在第j个遮蔽物里,则有:f[i][j]=min(f[i-1][j],f[i-1][j-1])+abs(x[i]-y[j]);

时间: 2024-10-11 03:22:35

HDU 3757 dp的相关文章

HDU 4832(DP+计数问题)

HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行,然后相乘累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long ll; const ll MOD = 9999991; const int N = 1005; int t, n, m, k, x, y; ll dp1

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

HDU 5928 DP 凸包graham

给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也就是 dp[j][k]代表当前链末端为j,其内部点包括边界数量为k的最小长度.这样最后得到的一定是最优的凸包. 然后就是要注意要dp[j][k]的值不能超过L,每跑一次凸包,求个最大的点数量就好了. 和DP结合的计算几何题,主要考虑DP怎么搞 /** @Date : 2017-09-27 17:27

HDU 4901 DP背包

给你n个数,问你将数分成两个数组,S,T ,T 中所有元素的需要都比S任意一个大,问你S中所有元素进行 XOR 操作和 T 中所有元素进行 &操作值相等的情况有多少种. DP背包思路 dpa[i][j][0]  表示从左开始到i,不取i,状态为j的方案数 dpa[i][j][1]  表示从作开始到i,取i,状态为j的方案数 dpb[i][j]      表示从右开始到i,状态为j的方案数 因为S集合一定在T集合的左边,那么可以枚举集合的分割线,并且枚举出的方案要保证没有重复,如果要保证不重复,只

Bone Collector II(HDU 2639 DP)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3471    Accepted Submission(s): 1792 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

hdu 4472 dp

http://acm.hdu.edu.cn/showproblem.php?pid=4472 第一直觉找规律,第二直觉 把树拆成子树,然后递推或者DP 然后发现不行,然后才发现自己题都没读,,,, dp[i]=segma(dp[j] | (i-1)%j==0) #include <cstdio> #include <cstring> #include <cstring> const int MAXN = 1000+50; const int MOD = 1e9+7; #

hdu 5791 (DP) Two

hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1421    Accepted Submission(s): 630 Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequ