SX [2020.01.09] NOIP improve the set of simulated race (day1)

[2020.01.09] NOIP improve the set of simulated race (day1)

The exam results are not satisfactory, only the first two questions, two questions after no time to do, do as the title lack pace.

 source : 100 + 20 + 0 + 0 = 120 rank7 

Very unfriendly.

Well, that is entered

\ (T1 \)

Portal: [USACO17JAN] Cow cow dance Dance Show

analysis

A fresh water problem, it is easy to think of half the answer, is easy to prove monotone.
Then consider acceptable \ (Check \)
Analyzing a small heap root, based on the time taken when the first stack maintenance.
Then - cut
the code slightly

\(T2\)

Portal: [USACO17JAN] Hoof,, Paper, Scissor hoof scissors ...
a present should be no more difficult to \ (the DP \) , but considering the leakage examination A method of transferring, a direct blow to the \ (20 \) minutes.
According to the meaning of problems, easy to think of setting state \ (f_ {i, j, k} \) represents the first \ (i \) round, there is a change \ (j \) times the chance, and is currently a \ (k \) ( H / S / P, is set to \ (1/2/3 \) )
of transfers:
setting \ (P \) , \ (Q \) represents Unlike \ (K \) of the other two gestures, \ (win [i] [j] \) represents a gesture \ (I \) win gesture \ (J \) , \ (a [I] \) represents the current \ (FJ \) gestures there is:
\ [F [I] [j] [k] = max (f [i-1] [j-1] [p], f [i-1] [j-1] [q], f [i-1] [j] [k ]) + win [k] [ a [i]] \]

Code

#include<cstdio>
#include<iostream>
using namespace std;

const int N = 1e5;
int n , k , a[N + 5] , f[N + 5][25][4] , ans = 0 , p , q;
char ch;

int data[4][4];

int main()
{
//  freopen("hps.in" , "r" , stdin);
//  freopen("hps.out" , "w" , stdout);
    scanf("%d%d" , &n , &k);
    for(register int i = 1; i <= n; i++)
    {
        ch = getchar();
        while (ch != 'H' && ch != 'P' && ch != 'S') ch = getchar();
        if (ch == 'H') a[i] = 1;
        else if (ch == 'P') a[i] = 2;
        else if (ch == 'S') a[i] = 3;
    }
    data[1][3] = data[2][1] = data[3][2] = 1;
    for(register int i = 1; i <= n; i++) 
        for(register int j = 1; j <= 3; j++)
            f[i][0][j] = f[i - 1][0][j] + data[j][a[i]];
    for(register int i = 1; i <= n; i++)
        for(register int j = 1; j <= k; j++)
            for(register int l = 1; l <= 3; l++)
            {
                if (l == 1) p = 2 , q = 3;
                else if (l == 2) p = 1 , q = 3;
                else if (l == 3) p = 1 , q = 2;
                f[i][j][l] = max(f[i - 1][j][l] + data[l][a[i]], max(f[i - 1][j - 1][p] + data[l][a[i]] , f[i - 1][j - 1][q] + data[l][a[i]]));
            }
    for(register int i = 0; i <= k; i++)
        for(register int j = 1; j <= 3; j++)
            ans = max(f[n][i][j] , ans);
    printf("%d" , ans);
}

Gone······

\ (T3 \)

Portal: [USACO17JAN] Cow Navigation cows navigation
a code is not difficult, but a large amount of details and more questions
\ (Tag \) : with \ (BFS \) simulation do \ (DP \)
analyzed in detail as follows:
first conversion: Because read map reason, so we set the starting point \ ((n, 1) \ ) , the end point \ ((1, n) \
) have two cows, respectively \ ((n, 1) \ ) thereof facing two directions
and to note: to end the cows ignoring any new commands, no matter what the other cattle, he does not move; if the current instruction has a cow would have crossed the line did not move. (For more serious look at the subject)

Set \ (f_ {x1, y1, d1, x2, y2, d2} \) represents bovine arrival \ ((x1, y1) \ ) facing \ (D1 \) , bovine two arrival \ ((x2, y2) \ ) for \ (D2 \) instruction minimum length
due to the contribution of each instruction is the answer \ (1 \) , it is possible to use \ (the BFS \) to do the most short side edge extended update \ (DIS \)
but it can actually optimize the space that knocked \ (d2 \) that dimension
nice thought, because two cows receiving the same instruction, their direction is relative, to know which one, will be able to know the other side, depending on a start toward

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int N = 20;

int n , map[N + 5][N + 5] , head , tail , ans = 1e5;
int f[N + 5][N + 5][4][N + 5][N + 5][4] , vis[N + 5][N + 5][4][N + 5][N + 5][4];
int fx[4][2] = { -1 , 0 , 0 , 1 , 1 , 0 , 0 , -1 };
char ch;

struct node{
    int x1 , y1 , d1 , x2 , y2 , d2;
}q[N * N * N * N * 16 + 10];

inline bool check(int x , int y , int xx , int yy)
{
    return (x <= 0 || y <= 0 || x > n || y > n || map[x][y] == 1 || (xx == 1 && yy == n));
}

int main()
{
//  freopen("cow.in" , "r" , stdin);
//  freopen("cow.out" , "w" , stdout);
    
    scanf("%d" , &n);
    for(register int i = 1; i <= n; i++)
        for(register int j = 1; j <= n; j++)
        {
            while (ch != 'E' && ch != 'H') ch = getchar();
            if (ch == 'H') map[i][j] = 1;
            ch = getchar();
        }
    q[++tail] = (node){n , 1 , 0 , n , 1 , 1};
    memset(f , 0x3f , sizeof(f));
    f[n][1][0][n][1][1] = 0;
    vis[n][1][0][n][1][1] = 1;
    while (head < tail)
    {
        struct node now = q[++head];
        int x1 , y1 , d1 , x2 , y2 , d2;
        
        d1 = now.d1 , d2 = now.d2;
        x1 = now.x1 + fx[d1][0] , y1 = now.y1 + fx[d1][1];
        if (check(x1 , y1 , now.x1 , now.y1)) x1 = now.x1 , y1 = now.y1;
        x2 = now.x2 + fx[d2][0] , y2 = now.y2 + fx[d2][1];
        if (check(x2 , y2 , now.x2 , now.y2)) x2 = now.x2 , y2 = now.y2;
        if (f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1 < f[x1][y1][d1][x2][y2][d2] && vis[x1][y1][d1][x2][y2][d2] == 0)
        {
            q[++tail] = (node){x1 , y1 , d1 , x2 , y2 , d2};
            f[x1][y1][d1][x2][y2][d2] = f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1;
            vis[x1][y1][d1][x2][y2][d2] = 1;
        }
        
        d1 = (now.d1 + 1) % 4 , d2 = (now.d2 + 1) % 4;
        x1 = now.x1 , y1 = now.y1 , x2 = now.x2 , y2 = now.y2;
        if (f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1 < f[x1][y1][d1][x2][y2][d2] && vis[x1][y1][d1][x2][y2][d2] == 0)
        {
            q[++tail] = (node){x1 , y1 , d1 , x2 , y2 , d2};
            f[x1][y1][d1][x2][y2][d2] = f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1;
            vis[x1][y1][d1][x2][y2][d2] = 1;
        }
        
        d1 = (now.d1 + 3) % 4 , d2 = (now.d2 + 3) % 4;
        x1 = now.x1 , y1 = now.y1 , x2 = now.x2 , y2 = now.y2;
        if (f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1 < f[x1][y1][d1][x2][y2][d2] && vis[x1][y1][d1][x2][y2][d2] == 0)
        {
            q[++tail] = (node){x1 , y1 , d1 , x2 , y2 , d2};
            f[x1][y1][d1][x2][y2][d2] = f[now.x1][now.y1][now.d1][now.x2][now.y2][now.d2] + 1;
            vis[x1][y1][d1][x2][y2][d2] = 1;
        }
    }
    for(register int i = 0; i < 4; i++) 
        for(register int j = 0; j < 4; j++)
            ans = min(ans , f[1][n][i][1][n][j]);
    printf("%d" , ans);
}

\ (T4 \)

Portal: [USACO17JAN] Subsequence Reversal reverse sequence
interpreted as follows:
it seems impossible to start the title, is actually a very routine intervals \ (the DP \)
according to only the value (1 ~ 50 \) \ characteristics, we can use the operation of the magical knocked-called flip
ado, Look
set \ (f_ {i, j, down, up} \) representing the interval \ ([I, J] \) , the numerical value range \ ([down, up] \) the longest sequence length is
then interval \ (the DP \) routines transfer, but the two sections are nested together metastasis

Code

#include<cstdio>
#include<iostream>
using namespace std;

const int N = 50;
int n , f[N + 5][N + 5][N + 5][N + 5] , a[N + 5] , Mx;

int main()
{
//  freopen("reversal.in" , "r" , stdin);
//  freopen("reversal.out" , "w" , stdout);
    scanf("%d" , &n);
    for(register int i = 1; i <= n; i++)
    {
        scanf("%d" , &a[i]);
        for(register int down = 1; down <= a[i]; down++)
            for(register int up = a[i]; up <= 50; up++)
                f[i][i][down][up] = 1;
    }
    for(register int len1 = 2; len1 <= n; len1++)
        for(register int i = 1; i <= n - len1 + 1; i++)
        {
            register int j = i + len1 - 1;
            for(register int len2 = 2; len2 <= 50; len2++)
                for(register int down = 1; down <= 51 - len2; down++)
                {
                    register int up = down + len2 - 1 , Mx;
                    Mx = max(f[i][j][down + 1][up] , f[i][j][down][up - 1]);
                    Mx = max(f[i + 1][j][down][up] + (a[i] == down) , Mx);
                    Mx = max(f[i][j - 1][down][up] + (a[j] == up) , Mx);
                    Mx = max(f[i + 1][j - 1][down][up] + (a[j] == down) + (a[i] == up) , Mx);
                    f[i][j][down][up] = Mx;
                }
        }
    printf("%d" , f[1][n][1][50]);
}

Guess you like

Origin www.cnblogs.com/leiyuanze/p/12173650.html