Home > Database > Mysql Tutorial > body text

Topcoder srm 653 div.2 1000

WBOY
Release: 2016-06-07 15:44:24
Original
1330 people have browsed it

题意: 给出一个序列,请你找出两个没有交且并为全集的子序列,使得每个子序列相邻两位之间的数字差的绝对之和最

题意:

给出一个序列,请你找出两个没有交且并为全集的子序列,使得每个子序列相邻两位之间的数字差的绝对值之和最小。

思路:

DP....(dp弱渣, 折腾了好久请教了人才会>,<..>

dp[i][j] 表示一人最后一个取的位置是i, 一人最后一个取的位置是j.

分两种情况:

1.i-j>1: dp[i][j] = dp[i-1][j] + abs(pitch[i-2]-pitch[i-1]) 让A一直取数..

2.i-j=1: 1) A只取i, 其他的都给B.

              2)A取j, 枚举A上一次取的位置k,B取i, 上一次取k: res = min(res, dp[j][k] + abs(pitch[i-1]-pitch[k-1])) 

                  理解: 两个人会把其中一个位置当做他取的最后一个位置, 那另一个人取的最后一个位置就是最后一个数了. 可以先想比如只有3个人, 这样写是可以把所有的情况涵盖的,相当于问题的子问题.

AC.

    #line 7 "SingingEasy.cpp"
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <bitset>
    #include <list>
    #include <stdexcept>
    #include <functional>
    #include <utility>
    #include <ctime>

    using namespace std;

    #define PB push_back
    #define MP make_pair

    #define REP(i,n) for(i=0;i=(l);--i)

    typedef vector<int> VI;
    typedef vector<string> VS;
    typedef vector<double> VD;
    typedef long long LL;
    typedef pair<int> PII;


    int dp[2005][2005];

    class SingingEasy
    {
            public:
            int solve(vector <int> pitch)
            {
                int len = pitch.size();
                if(len  1) {
                            dp[i][j] = dp[i-1][j] + abs(pitch[i-2]-pitch[i-1]);
                        }
                        else {
                            int res = 1e9+5;
                            for(int k = 1; k <br>
<br>
</int></int></double></string></int></ctime></utility></functional></stdexcept></list></bitset></iomanip></numeric></fstream></stack></queue></set></map></sstream></iostream></string></vector></algorithm></cmath></cstdio></cstring></cctype></cstdlib>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template