Konjac solution to a problem of special dp

Preface: always wanted to practice dp, God just broke a scrimmage clothing. . Last look at problem solution whim cgold big brother so he tried to write the first solution to a problem. . Unfortunately, the ability of konjac prescribe the poor do not quite out of the question, the solution to a problem other chiefs had to carry studied. .

a title

https://vjudge.net/contest/355951#problem/A

This problem is very painful process do problems

I double meaning of the title of Cheese Li wrong. .

I thought it must be on the diagonal

In fact, it is just n * n will do. .

The idea is probably to start updating from a corner, left and top edges of the same length statistics

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f 
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100005
#define P pair<int,int>
using namespace std;
char a[1005][1005];
int dp[1005][1005];
int n;


int main()
{
    int n, ans;
    while (cin>>n , n)
    {
        ans = 1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                cin >> a[j][i];
            }
        for (int i = 0; i < n; i++)
        {
            for (int j = n - 1; j >= 0; j--)
            {
                dp[i][j] = 1;
                if (i == 0 || j == n - 1) continue;
                int q = dp[i - 1][j + 1];
                for (int k = 1; k <= q; k++)
                {
                    if (a[i - k][j] == a[i][j + k]) dp[i][j]++;
                    else break;
                }
                ans = max(ans, dp[i][j]);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

 b title

https://vjudge.net/contest/355951#problem/B

This problem a little difficult. .

Konjac directly off the assembly line. . .

The method of online Gangster then learn the next bar

With DP (j, k) represents, taking candidates j, the difference is so controlled debate k in all programs, defense and control program that the maximum (this scheme is called "program DP (j, k)") is debate and control

#include <cstdio> 
#include <CString> 
#include <the iostream> 
#include <the cmath> 
#include <Vector> 
#include <algorithm> 
#include <String> 
#include <Map> 
the using namespace STD; 

#define N 220 
#define Met (A, B) Memset (A, B, the sizeof (A)) 
#define INF 0xFFFFFF 
const Long Long Max = 2000000000.; 
typedef Long Long LL; 

int D [N], P [N]; 
int DP [30] [ 1000]; /// dp [i] [k] represents a group selected individual i, k is the defense controlled differential control of defense and maximum 
int pre [30] [1000] ; /// pre [i] [k] is stored it is selected from the last person 
int Answer [N]; /// this records the selected personal number m 

int main () 
{ 
    int n-, m, = iCase. 1; 

    the while (Scanf ( "% D% D", & n- , & m),|| n m) n||m)
    {
        you i, j, k, Who, t1, t2;
 
        Met (D, 0); 
        Met (P, 0); 
        Met (DP, -1); 
        Met (pre, 0); 
        Met (Answer The, 0); 

        for (I =. 1; I <= n-; I ++) 
            Scanf ( "% D% D", & D [I], & P [I]); 

        Min = m * 20 is; /// its defense controlled differential at most m * 20 is   
        DP [0] [Min] = 0; /// first set to an initial state 0 


        /// maximum ranges of K [-Min, Min], but the arrays can not be represented by a negative number, so the array to the right translating Min, to give [0, 2 * Min] 
        for (I = 0; I <= m; I ++) 
        { 
            for (K = 0; K <= Min * 2; K ++) 
            { 
                IF (DP [I] [K] == - 1) continue; /// If there is, then find dp [i] [k] is the next state 

                for (J =. 1; J <= n-; J ++) 
                { 
                        IF (DP [I +. 1] [K + D [J] -P [J]] <DP [ I] [K] + D [J] + P [J]) 
                        {
                            T1 = I, T2 = K; 

                            the while (T1> 0 && pre [T1] [T2] = J!)   
                            { 
                                T2 - = D [pre [T1] [T2]] - P [pre [T1] [T2]] ; 
                                t1 -; 
                            } 
                            iF (t1 == 0) /// when t1 is 0, the number j before the person is not selected too 
                            { 
                                DP [I +. 1] [+ K D [j] -P [ J]] DP = [I] [K] + D [J] + P [J]; 
                                pre [I +. 1] [+ K D [J] -P [J]] = J; 
                            } 
                        } 

                } 
            } 
        } 

        int ff = Min; 
        Int num = 0, 0 = SUM1, SUM2 = 0; 

        /// to choose the smallest difference control argue, num ask the individual is selected from m control debate smallest difference 
        while (dp [m] [ff- NUM] == -. 1 && DP [m] [+ FF NUM] == -. 1) NUM ++; 

        IF (DP [m] [FF-NUM]> DP [m] [NUM FF +]) = T2-FF NUM ; 
        the else T2 = FF + NUM; 

        T1 = m; 

        for (I =. 1; I <= m; I ++) 
        { 
            Answer The [I] = pre [T1] [T2]; 

            SUM1 + = D [Answer The [I]]; 
            + P = SUM2 [Answer The [I]]; 

            T1 -; 
            T2 - D = [Answer The [I]] - P [Answer The [I]]; 
        } 

        the printf ( "% D Jury # \ n-", iCase ++); 
        the printf ( "% D Best Jury has value for% D value and prosecution of Defense for: \ n-", SUM1, SUM2); 


        Sort (Answer The +. 1, Answer The + m +. 1); 

        for (I =. 1; I <= m; I ++)
            printf(" %d", Answer[i]);

        printf("\n\n");
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/cyq123/p/12296640.html