经典题 最长上升子序列
先把每一个箱子都拆成六个(全排列),然后对其中的 X 坐标排序,再然后以 Z 为权值(即高度),Y 上升为条件DP。做法几乎和最长上升子序列一模一样。
Accepted | 1093 | C | 00:00.02 | 404K |
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* a,const void* b)
{
return *((int*)b) - *((int* )a);
}
void solve(int n)
{
int size[180][3] = {0 };
int i,j;
int m[180 ];
for (i = 0; i < 6 * n; i += 6 )
{
int x,y,z;
scanf("%d%d%d",&x,&y,& z);
size[i][0] = x;size[i][1] = y;size[i][2] = z;
size[i + 1][0] = x;size[i + 1][1] = z;size[i + 1][2] = y;
size[i + 2][0] = y;size[i + 2][1] = x;size[i + 2][2] = z;
size[i + 3][0] = y;size[i + 3][1] = z;size[i + 3][2] = x;
size[i + 4][0] = z;size[i + 4][1] = x;size[i + 4][2] = y;
size[i + 5][0] = z;size[i + 5][1] = y;size[i + 5][2] = x;
}
qsort(size,6 * n,sizeof(size[0 ]),cmp);
for (i = 0; i < 6 * n; i++ )
{
int max = 0 ;
for (j = 0; j < i; j++ )
{
if (size[j][1] > size[i][1] && size[j][0] > size[i][0 ])
max = max > m[j] ? max : m[j];
}
m[i] = max + size[i][2 ];
}
int ans = 0 ;
for (i = 0; i < 6 * n; i++ )
ans = ans > m[i] ? ans : m[i];
printf("maximum height = %d/n" ,ans);
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("1093.txt","r" ,stdin);
#endif
int n,t = 0 ;
while (scanf("%d",&n) != EOF && n != 0 )
{
printf("Case %d: ",++ t);
solve(n);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
#include <stdlib.h>
#include <string.h>
int cmp(const void* a,const void* b)
{
return *((int*)b) - *((int* )a);
}
void solve(int n)
{
int size[180][3] = {0 };
int i,j;
int m[180 ];
for (i = 0; i < 6 * n; i += 6 )
{
int x,y,z;
scanf("%d%d%d",&x,&y,& z);
size[i][0] = x;size[i][1] = y;size[i][2] = z;
size[i + 1][0] = x;size[i + 1][1] = z;size[i + 1][2] = y;
size[i + 2][0] = y;size[i + 2][1] = x;size[i + 2][2] = z;
size[i + 3][0] = y;size[i + 3][1] = z;size[i + 3][2] = x;
size[i + 4][0] = z;size[i + 4][1] = x;size[i + 4][2] = y;
size[i + 5][0] = z;size[i + 5][1] = y;size[i + 5][2] = x;
}
qsort(size,6 * n,sizeof(size[0 ]),cmp);
for (i = 0; i < 6 * n; i++ )
{
int max = 0 ;
for (j = 0; j < i; j++ )
{
if (size[j][1] > size[i][1] && size[j][0] > size[i][0 ])
max = max > m[j] ? max : m[j];
}
m[i] = max + size[i][2 ];
}
int ans = 0 ;
for (i = 0; i < 6 * n; i++ )
ans = ans > m[i] ? ans : m[i];
printf("maximum height = %d/n" ,ans);
}
void main()
{
#ifndef ONLINE_JUDGE
freopen("1093.txt","r" ,stdin);
#endif
int n,t = 0 ;
while (scanf("%d",&n) != EOF && n != 0 )
{
printf("Case %d: ",++ t);
solve(n);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}