Pty loves lines 题解(dp)

题目链接

题目思路

emmm这种dp真的有点小技巧 转换了dp的思维

同时这个dp的转移设置的也是有点不同qwq

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
mt19937 rnd(time(0));
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=700+5,inf=0x3f3f3f3f,mod=20071027;
const double eps=1e-10;
int n;
bool vis[maxn];
int dp[maxn*maxn];
int pr[maxn*maxn];
signed main(){
    int _;scanf("%d",&_);
    while(_--){
        scanf("%d",&n);
        vis[n]=1;
    }
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=700;i++){
        for(int j=0;j<=700*700;j++){
            int k=j+i*(i-1)/2;
            if(k<=700*700){
                dp[k]=min(dp[k],dp[j]+i);
            }
        }
        if(vis[i]){ //直接这么写的原因是因为输入n单调递增
            int ed=i*(i-1)/2,cnt=0;
            for(int j=0;j<=ed;j++){
                if(dp[ed-j]<=i){
                    pr[++cnt]=j;
                }
            }
            for(int j=1;j<=cnt;j++){
                printf("%d%c",pr[j],j==cnt?'\n':' ');
            }
        }
    }
    return 0;
}

posted @ 2021-08-19 20:14  hunxuewangzi  阅读(130)  评论(0编辑  收藏  举报