| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
DP 47ms打错了個下標WA了一次,真是奇怪为啥這種bug总是能過样例!!!
#include <stdio.h>
int dp[202][32], trc[202][32];
int ff[202];
int main() {
int N,K,cnt=0;
while(1){
scanf("%d%d",&N,&K);
if(!N)break;
cnt++;
printf("Chain %d\n",cnt);
for(int i = 1; i <= N; i++){
scanf("%d",&ff[i]);
}
for(int i = 1; i <= N; i++){
trc[i][1] = i;
int r = 0;
int j=1, k=i;
while(k>j){
r+=ff[k]-ff[j];
j++;k--;
}
dp[i][1] = r;
}
for(int i = 2; i <= K; i++){
for(int j = i; j <= N; j++){
int mn = 2147483647,arg=-1;
for(int l=1; l<=j-i+1;l++){
int r=dp[j-l][i-1];
int s=j-l+1,t=j;
while(t>s){
r+=ff[t]-ff[s];
t--;s++;
}
if(r<mn){
mn=r;
arg=l;
}
}
dp[j][i]=mn;
trc[j][i]=arg;
}
}
int col[32], c=N;
for(int i = K; i > 0; i--){
col[i] = trc[c][i];
c-=trc[c][i];
}
int he = 0;
for(int i = 1; i <= K; i++){
if(col[i]>1)printf("Depot %d at restaurant %d serves restaurants %d to %d\n",i,he+(1+col[i])/2,he+1,he+col[i]);
else printf("Depot %d at restaurant %d serves restaurant %d\n",i,he+1,he+1);
he+=col[i];
}
printf("Total distance sum = %d\n\n",dp[N][K]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator