1. 程式人生 > >hdu 3392(滾動陣列優化dp)

hdu 3392(滾動陣列優化dp)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3392

Pie

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 793    Accepted Submission(s): 214


Problem Description A lot of boys and girls come to our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone’s height, and we believe that the less difference of a girl and a boy has, the better it is. We need to find as more matches as possible, but the total difference of the matches must be minimum.
Input The input consists of multiple test cases. The first line of each test case contains two integers, n, m (0 < n, m <= 10000), which are the number of boys and the number of girls. The next line contains n float numbers, indicating the height of each boy. The last line of each test case contains m float numbers, indicating the height of each girl. You can assume that |n – m| <= 100 because we believe that there is no need to do with that if |n – m| > 100. All of the values of the height are between 1.5 and 2.0.
The last case is followed by a single line containing two zeros, which means the end of the input.
Output Output the minimum total difference of the height. Please take it with six fractional digits.
Sample Input 2 3 1.5 2.0 1.5 1.7 2.0 0 0
Sample Output 0.000000
Author
[email protected]

Source
思路:dp+滾動陣列

(1):要求最佳匹配,首先得將兩陣列從小到大排序~

 (2): 然後再明確dp[][]表示的意思;dp[i][j] 表示a陣列中前i個數和b陣列中前j個數匹配的最優解

  (3):接下來 看看狀態轉移方程; if(i==j)dp[i][j]=dp[i-1][j-1]+fabs(a[i]-b[j]);

                                                                   else dp[i][j]=min(dp[i-1][j-1]+fabs(a[i]-b[i]),dp[i][j-1]);

   (4):   因為題目中的n最大取到10000,如果開個陣列dp[10000][10000],那麼執行不了~那麼再觀察觀察狀態轉移方程,發現當前這個數是由它左邊這列遞推過來的,我們可以用一個dp[2][10000]的滾動陣列即可,因為我只關心最後一個dp[n][m]值,所以前面的一些值被覆蓋不影響我後面的求值過程;(可以在紙上畫一畫,就知道這個滾動陣列了)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
const int maxn=11000;
using namespace std;

double a[maxn],b[maxn];
double dp[2][maxn];

int main()
{
 int n,m;
 while(cin>>n>>m)
 {
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
        for(int i=1;i<=m;i++)scanf("%lf",&b[i]);

        double *A=a,*B=b;
        if(n>m){swap(n,m);swap(A,B);}
        sort(A+1,A+1+n);
        sort(B+1,B+1+m);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
         for(int j=i;j<=i+m-n;j++)
         {
          if(i==j)
          {
              dp[i&1][j]=dp[(i-1)&1][j-1]+fabs(A[i]-B[j]);
              //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]);
          }
          else
          {
              dp[i&1][j]=min(dp[(i-1)&1][j-1]+fabs(A[i]-B[j]),dp[i&1][j-1]);
              //printf("dp[%d][%d] :%.6lf",i,j,dp[i&1][j]);
          }
         }
       printf("%.6lf\n",dp[n&1][m]);
 }
 return 0;
}


相關推薦

hdu 3392滾動陣列優化dp

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe

HDU 2829 Lawrence四邊形不等式優化DP

題意:鐵路上有n個站點,每個站可以往其他站運送糧草,現在要炸掉m段鐵路(兩個站點之間為一段)使得糧草補給之和最小,剩餘每塊連通鐵路的糧草補給Strategic Value計算方法見題目,不再累贅。

POJ1160 Post Office 四邊形不等式優化DP

single inpu cst class pac ios 不等式 lang rep There is a straight highway with villages alongside the highway. The highway is represented as

形態形成場矩陣乘法優化dp

現在 字母 個數 def long string ace 取模 lse 形態形成場(矩陣乘法優化dp) 短信中將會涉及前\(k\)種大寫字母,每個大寫字母都有一個對應的替換式\(Si\),替換式中只會出現大寫字母和數字,比如\(A→BB,B→CC0,C→123\),代表

降臨線段樹優化dp

main spa space odi pri line 除了 發現 獲得 降臨 選定點i會有代價\(c_i\),如果一個區間j內的點全被選擇,就可以獲得回報\(p_j\)。點數和區間個數\(<1e5\)。 還以為是線段樹優化網絡流(50萬個點200萬條邊看上去很可

題解:CF115E線段樹優化dp

定義 可能 bit tput space nbsp sans odi bsp 題目描述 你是一個賽車比賽的組織者,想在線性王國中安排一些比賽。 線性王國有n條連續的從左到右的道路。道路從左到右依次編號為從1到n,因此道路按照升序排列。在這些道路上可能會有幾場比賽,每一場

CF-833B The Bakery線段樹優化Dp

imu ger another art 區間 produce let span start Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought r

CodeForces - 321E:Ciel and Gondolas 四邊形不等式優化DP

題意:N個人排成一行,分成K組,要求每組的不和諧值之和最小。 思路:開始以為是斜率優化DP,但是每個區間的值其實已經知道了,即是沒有和下標有關的未知數了,所以沒必要用斜率。 四邊形優化。 dp[i][j]表示前j個人分為i組的最小代價。 #include<bits/stdc++.h>

2018.09.23 孫悟空大戰鯉魚精單調佇列優化dp

傳送門 一道帶了spj的單調佇列優化dp。 注意元素入隊和出隊的條件。 程式碼: #include<bits/stdc++.h> #define N 200005 using namespa

賽前練手#3BZOJ1499[NOI2005] 瑰麗華爾茲單調佇列優化DP

1499: [NOI2005]瑰麗華爾茲 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2123 Solved: 1291 [Submit][Status][Discuss] Description 你跳過華爾茲

DP計劃】11.3——[BZOJ]股票交易單調佇列優化DPMEDIUM

Description 最近lxhgww又迷上了投資股票,通過一段時間的觀察和學習,他總結出了股票行情的一些規律。 通過一段時間的觀察,lxhgww預測到了未來T天內某隻股票的走勢,第i天的股票買入價為每股APi,第i天的股票賣出價為每股BPi(資料保證對於每個

[ZJOI2010]基站選址線段樹優化dp

坑待填。 \(Code\ Below:\) #include <bits/stdc++.h> #define lson (rt<<1) #define rson (rt<<1|1) using namespace std; const int maxn=20000+10

Vijos1243:生產產品單調佇列優化dp

傳送門 題意: 有 n 個任務,m 個機器,每個機器完成每個任務都有特定的時間,任務必須依次完成,且一個機器只能連續完成 l 個任務,每次更換機器需要時間 k ,求完成所有任務的最短時間。 (n≤

hdu 5008 字尾陣列 + rmq +二分

題意:給出一個字串,求出第k小的子串,並求出字串的起止位置,如果有多個重複的子串,求出位置最靠左的子串。 思路:比賽時,想到了要用字尾陣列,但是沒想到如何做。 其實,因為子串是字尾的字首,字尾陣列對字尾排序的同時,也對子串進行了排序。對於

HDU 1028 母函式或者dp

“The second problem is, given an positive integer N, we define an equation like this: N=a[1]+a[2]+a[3]+…+a[m]; a[i]>0,1<

HDU 3507 Print Article斜率優化DP

hdu clas 進行 維護 直接 元素 string 單調性 ons 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 題目大意:概題意就是要輸出N個數字a[N],輸出的時候可以連續連續的輸出,每連續輸出一串,它的費

HDU 3401 Trade用單調佇列優化DP

題意:給出T天內,每天的股票買賣價格和每天的股票買賣最大數量,而且最多隻能擁有maxP數量的股票,開始時有無限本金,任意兩次交易需要間隔W天及以上,也就是第i天交易,第i+w+1天及以後才能再交易。問最多能賺多少錢?(題意應該還有一個隱晦的要求是,當天只能買股票或者賣股票

P5241 序列滾動數組+前綴和優化dp

觀察 總結 復雜度 get namespace 正數 const tar 下標 P5241 序列 挺神仙的一題 看看除了dp好像沒什麽其他辦法了 想著怎麽構個具體的圖出來,然鵝不太現實。 於是我們想辦法用幾個參數來表示dp數組 加了幾條邊肯定要的吧,於是加個參數$

HDU 4856 TunnelsBFS+狀壓DP

pid air san void hit uil set itl pair HDU 4856 Tunnels 題目鏈接 題意:給定一些管道。然後管道之間走是不用時間的,陸地上有障礙。陸地上走一步花費時間1,求遍歷全部管道須要的最短時間。每一個管道僅僅能走一次 思

[Vijos 1243]生產產品單調隊列優化Dp

lib div solution int 目的 每一個 lan || space Description 在經過一段時間的經營後,dd_engi的OI商店不滿足於從別的供貨商那裏購買產品放上貨架,而要開始自己生產產品了!產品的生產需要M個步驟,每一個步驟都可以在N臺機器中