QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#300815#6955. Assignmentdo_while_trueAC ✓118ms4760kbC++203.6kb2024-01-08 20:51:292024-01-08 20:51:29

Judging History

你现在查看的是最新测评结果

  • [2024-01-08 20:51:29]
  • 评测
  • 测评结果:AC
  • 用时:118ms
  • 内存:4760kb
  • [2024-01-08 20:51:29]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
    r=0;bool w=0;char ch=getchar();
    while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
    while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
    return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
    int s=1;
    while(y){
        if(y&1)s=1ll*s*x%mod;
        x=1ll*x*x%mod;
        y>>=1;
    }
    return s;
}
const int N=110;
const int K=11;
int n,m;
int a[N],b[N];
int A[N][N];
int f[N][N][K],g[N][N][K],dp[N][K];
void solve(){
    memset(f,0x3f,sizeof(f));
    memset(g,0x3f,sizeof(g));
    memset(dp,0x3f,sizeof(dp));
    read(n,m);
    for(int i=1;i<=n;i++)read(a[i]);
    for(int i=1;i<=n;i++)read(b[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            read(A[i][j]);
    for(int i=1;i<=n;i++){
        f[i][i][1]=0;
        f[i][i][0]=A[b[i]][1];
        g[i][i][0]=0;
    }
    for(int len=2;len<=n;len++)
        for(int i=1;i+len-1<=n;i++){
            int j=i+len-1;
            if(b[i]==b[j]){
                for(int k=0;k<=m;k++)g[i][j][k]=g[i][j-1][k];
            }
            else{
                for(int k=1;k<=m;k++)g[i][j][k]=g[i][j-1][k-1];
            }
            for(int k=i;k<j;k++)
                for(int p=0;p<=m;p++)
                    for(int q=0;p+q<=m;q++){
                        cmin(g[i][j][p+q],g[i][k][p]+f[k+1][j][q]);
                        cmin(f[i][j][p+q],f[i][k][p]+f[k+1][j][q]);
                    }
            for(int k=0;k<=m;k++)
                cmin(f[i][j][k],g[i][j][k]+A[b[i]][len]);
        }
    dp[0][0]=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=m;j++){
            if(a[i]==b[i])dp[i][j]=dp[i-1][j];
            else if(j)dp[i][j]=dp[i-1][j-1];
        }
        for(int j=i;j>=1;j--){
            for(int p=0;p<=m;p++)
                for(int q=0;p+q<=m;q++){
                    cmin(dp[i][p+q],dp[j-1][p]+f[j][i][q]);
                }
        }
    }
    for(int i=1;i<=m;i++)cmin(dp[n][i],dp[n][i-1]);
    for(int i=0;i<=m;i++)cout << dp[n][i] << ' ';
    puts("");
}
signed main(){
    #ifdef do_while_true
//        assert(freopen("data.in","r",stdin));
//        assert(freopen("data.out","w",stdout));
    #endif
    int T;read(T);
    while(T--)solve();
    #ifdef do_while_true
//        cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
    #endif
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 118ms
memory: 4760kb

input:

10
100 10
24 56 71 49 70 39 19 23 96 26 85 33 73 84 35 75 58 77 85 40 33 36 75 55 7 28 37 7 100 94 35 64 68 46 77 80 90 28 85 8 9 23 32 45 4 51 47 82 6 49 45 55 28 69 80 7 61 41 83 42 16 25 82 56 26 92 14 66 43 1 2 62 25 84 19 58 35 37 14 61 38 12 27 95 98 100 61 36 75 83 35 19 74 56 87 10 10 6 60 5...

output:

27473279 26539877 25650181 24801901 24025447 23252605 22479763 21733001 20960159 20221324 19493165 
32063854 31138381 30266331 29465439 28683429 27901842 27157669 26414311 25690509 25035718 24380927 
3973096 3343250 2761180 2428393 2215777 2075964 1991108 1914392 1829536 1823074 1749700 
5341649 453...

result:

ok 10 lines