暑假集训日记——7.21(区间dp+codeforce)

D. Prime Graph
题解:
暑假集训日记——7.21(区间dp+codeforce)_第1张图片

#include 
using namespace std;

bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i*i <= x; ++i) {
        if (x%i == 0) return false;
    }
    return true;
}


int main(int argc, char ** argv){
	int n; cin >> n;
    int m = n;
    while (!prime(m)) ++m;
    cout << m << "\n1 " << n << '\n';
    for (int i = 0; i < n-1; ++i) {
        cout << i+1 << ' ' << i+2 << '\n';
    }

    for (int i = 0; i < m-n; ++i) {
        cout << i+1 << ' ' << i+1+n/2 << '\n';
    }
}

题解:
开始认为很难的一道题,毕竟子序列可以不连续,manacher不能用,但是注意到只有三个字母所以简单了不少,因为前两个和后两个一定有一对字母相同。
暑假集训日记——7.21(区间dp+codeforce)_第2张图片

#include 
#include 
#include 

using namespace std;
int main() {
    string S; cin >> S;
    int N = S.size();
    int i = 0, j = N-1;
    string A;
    while (j-i >= 3) {
        if (S[i] == S[j]) {
            A.push_back(S[i]);
            ++i; --j;
        } else if (S[i] == S[j-1]) {
            A.push_back(S[i]);
            ++i; j -= 2;
        } else {
            A.push_back(S[i+1]);
            if (S[i+1] == S[j]) {
                --j;
            } else {
                j -= 2;
            }
            i += 2;
        }
    }
    string B = A;
    if (j >= i) A.push_back(S[i]);
    reverse(B.begin(),B.end());
    cout << A << B << endl;
}

A - Multiplication Puzzle
题解:区间dp

#include
#include
#include
using namespace std;
#define INF 0x3f3f3f3f

///dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i

C - Halloween Costumes
题解:区间dp

#include
#include
#include
#include
using namespace std;
#define INF 0x3f3f3f3f

///dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i>t;
    cnt=t;
    while(t--)
    {
        cin>>n;
        memset(dp,0,sizeof(dp));
        for(int  i =1;i<=n;i++){
            cin>>stone[i];
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            dp[i][j]=j-i+1;

        for(int len = 1;len<=n;len++){//枚举长度
            for(int j = 1;j+len<=n+1;j++){//枚举起点,ends<=n
                int ends = j+len - 1;
                dp[j][ends]=dp[j][ends-1]+1;
                for(int i = j;i

B - Food Delivery
题解:
区间dp: 1表示停留在区[ i , j ]右边,0表示左边。
dp[ i ][ j ][ 0 ]=min( dp[ i+1 ][ j ][ 0 ] , dp[ i+1 ][ j ][ 1 ] )
dp[ i ][ j ][ 1 ]=min( dp[ i ][ j-1 ][ 0 ] , dp[ i ][ j-1 ][ 1 ] )

#include
#include
#include
#include
using namespace std;
#define INF 0x3f3f3f3f

///dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i= 1; i--)
        {
            for (int j = pos; j <= n; j++)
            {
                if(i == j)continue;
                dp[i][j][0] = min(dp[i][j][0],dp[i + 1][j][0] + (sum[i] + sum[n] - sum[j]) * (stone[i + 1].x - stone[i].x));
                dp[i][j][0] = min(dp[i][j][0],dp[i + 1][j][1] + (sum[i] + sum[n] - sum[j]) * (stone[j].x - stone[i].x));
                dp[i][j][1] = min(dp[i][j][1],dp[i][j - 1][1] + (sum[i - 1] + sum[n] - sum[j - 1]) * (stone[j].x - stone[j - 1].x));
                dp[i][j][1] = min(dp[i][j][1],dp[i][j - 1][0] + (sum[i - 1] + sum[n] - sum[j - 1]) * (stone[j].x - stone[i].x));
            }
        }
        printf("%d\n",min(dp[1][n][0],dp[1][n][1]) * v);
    }
    return 0;
}

G - Dire Wolf
题解:区间dp,与上面的 A 题差不多

#include
#include
#include
#include
using namespace std;
#define INF 0x3f3f3f3f

///dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i>t;
    cnt=t;
    while(t--)
    {
        cin>>n;
        memset(dp,0,sizeof(dp));
        memset(stone,0,sizeof(stone));
        memset(stones,0,sizeof(stones));
        for(int  i =1;i<=n;i++){
            cin>>stone[i];
        }
        for(int  i =1;i<=n;i++){
            cin>>stones[i];
        }
        for(int len = 3;len<=n+2;len++){//枚举长度
            for(int j = 0;j+len<=n+2;j++){//枚举起点,ends<=n
                int ends = j+len - 1;
                dp[j][ends]=INF;
                for(int i = j+1;i

你可能感兴趣的:(Codeforces,DP动态规划,暑假集训,字符串类型题)