uva 10271 Chopsticks

6121 단어 dpuva
원제: In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different.He uses a set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you may guess, the length of the two shorter chopsticks should be as close as possible, but the length of the extra one is not important, as long as it’s the longest. To make things clearer, for the set of chopsticks with lengths A, B, C (A ≤ B ≤ C), (A − B) 2 is called the “badness” of the set. It’s December 2nd, Mr.L’s birthday! He invited K people to join his birthday party, and would like to introduce his way of using chopsticks. So, he should prepare K + 8 sets of chopsticks(for himself, his wife, his little son, little daughter, his mother, father, mother-in-law, father-in-law, and K other guests). But Mr.L suddenly discovered that his chopsticks are of quite different lengths! He should find a way of composing the K + 8 sets, so that the total badness of all the sets is minimized. Input The first line in the input contains a single integer T, indicating the number of test cases (1 ≤ T ≤ 20). Each test case begins with two integers K, N (0 ≤ K ≤ 1000, 3K + 24 ≤ N ≤ 5000), the number of guests and the number of chopsticks. There are N positive integers Li on the next line in non–decreasing order indicating the lengths of the chopsticks (1 ≤ Li ≤ 32000). Output For each test case in the input, print a line containing the minimal total badness of all the sets. Note: For the sample input, a possible collection of the 9 sets is: 8,10,16; 19,22,27; 61,63,75; 71,72,88; 81,81,84; 96,98,103; 128,129,148; 134,134,139; 157160 Sample Input 1 1 40 1 8 10 16 19 22 27 36 40 47 52 56 61 63 71 72 75 81 81 81 84 88 98 103 110 113 124 128 129 134 134 139 148 157 160 162 164 Sample Output 23 의 뜻: 8 + n 막대기를 주고 k조로 나누어 세 개씩 나누어주세요. 그 중 하나는 badness라고 합니다.이 값은 각 조에서 가장 짧은 두 막대기의 제곱차와 같다.지금 이 8 + n 막대기를 k조로 나누면,badness가 가장 작고 얼마냐고 물어보세요.데이터가 당신에게 비강하 순서를 줄 수 있으니 주의하세요.
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
#include <climits>
using namespace std;

int dp[5001][1001];
int cho[5001];
int main()
{
    ios::sync_with_stdio(false);
    int t,k,n;
    cin>>t;
    while(t--)
    {
        cin>>k>>n;
        k+=8;
        for(int i=n;i>=1;i--)
            cin>>cho[i];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=k;j++)
            dp[i][j]=INT_MAX;
        for(int i=3;i<=n;i++)
        {
            for(int j=1;j<=k;j++)
                if(i>=j*3)
                dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(cho[i]-cho[i-1])*(cho[i]-cho[i-1]));
        }
        cout<<dp[n][k]<<endl;
    }
// input.close();
// output.close();
    return 0;
}





해답: 처음 봤을 때 아직 흐리멍덩하다. 만약에 데이터가 작으면 수동으로 시뮬레이션을 해서 자신의 전이 방정식이 정확한지 확인할 수 있다.그러나 이 데이터는 비교적 커서 수동 시뮬레이션은 매우 힘들다.먼저 상태를 고려한다. n개의 막대기가 k개를 선택하기 때문에 상태가 2차원이라는 것을 쉽게 확인할 수 있다. dp[i][j]로 설정하면 앞의 i개의 막대기가 j조를 선택할 때의 최소badness를 나타낸다.다음에 상태 이동 과정을 보면 먼저 관건적인 점을 생각해야 한다. 사실은 매우 좋다. 왜냐하면 제목에서 이미 힌트를 주었기 때문이다. 바로 너에게 준 데이터는 비강등 순서에 따라 배열되기 때문이다.예를 들어 지금 또 세 개의 수 A, B, C, D가 있는데 A<=B<=C<=D가 있다. 지금 너에게 A, B, C, D 중의 한 개의 수 X를 줄 것이다. 너에게 다른 수 Y를 찾아서 (X-Y)^2를 최소화하라고 한다. 이것은 X와 인접한 두 개의 수를 찾아서 판단하는 것이 분명하다.상태가 이동하는 과정이 유사하여 i번째 막대기를 선택하거나 선택하지 않습니다.dp[i-1][j]로 표시하는 것을 선택하지 않으면 i번 막대기로 j조를 구성하지 않는다는 뜻이다.만약에 i번 막대기를 사용한다면 dp[i-2][j-1]+(cho[i-1]-cho[i])^2는 앞의 i-2개의 막대기로 j-1조를 구성하고 i-1, i-2개의 막대기로 구성된 조의 badness를 나타낸다.(여기서 막대기의 길이 순서를 거꾸로 해야 한다는 것을 기억한다) dp[i][j]=min(dp[i-1][j], dp[i-2][j-1]+(cho[i]-cho[i-1])*(cho[i]-cho[i-1])))).
경계 처리를 주의하면 됩니다~

좋은 웹페이지 즐겨찾기