[UVA] 11456 - Trainsorting@Morris' Blog|PChome Online 人新台
2012-02-29 08:08:31| 人1,506| 回2 | 上一篇 | 下一篇

[UVA] 11456 - Trainsorting

0 收藏 0 0 站台

Problem A: Trainsorting

Erin is an engineer. She drives trains. She also arranges the cars within each train. She prefers to put the cars in decreasing order of weight, with the heaviest car at the front of the train.

Unfortunately, sorting train cars is not easy. One cannot simply pick up a car and place it somewhere else. It is impractical to insert a car within an existing train. A car may only be added to the beginning and end of the train.

Cars arrive at the train station in a predetermined order. When each car arrives, Erin can add it to the beginning or end of her train, or refuse to add it at all. The resulting train should be as long as possible, but the cars within it must be ordered by weight.

Given the weights of the cars in the order in which they arrive, what is the longest train that Erin can make?

Input Specification

The first line is the number of test cases to follow. The test cases follow, one after another; the format of each test case is the following:

The first line contains an integer 0 <= n <= 2000, the number of cars. Each of the following n lines contains a non-negative integer giving the weight of a car. No two cars have the same weight.

Sample Input

1 3 1 2 3 

Output Specification

Output a single integer giving the number of cars in the longest train that can be made with the given restrictions.

Output for Sample Input

3 

做法 : DP
O(nlogn), max(LIS[i]+LDS[i]-1);

#include <stdio.h>
#include <stdlib.h>
void findLIS(int N, int LIS[], int pos[], int A[]) {
int i, L = -1, l, r, m, newSet;
int j;
for(i = 0; i < N; i++) {
l = 0, r = L, newSet = -1;
while(l <= r) {
m = (l+r)/2;
if(pos[m] <= A[i]) {
if(m == L || pos[m+1] > A[i]) {
newSet = m+1;break;
} else
l = m+1;
} else {
r = m-1;
}
}
if(newSet == -1) newSet++;
pos[newSet] = A[i];
LIS[i] = newSet+1;
if(L < newSet) L = newSet;
}
}
void findLDS(int N, int LDS[], int pos[], int A[]) {
int i, L = -1, l, r, m, newSet;
int j;
for(i = 0; i < N; i++) {
l = 0, r = L, newSet = -1;
while(l <= r) {
m = (l+r)/2;
if(pos[m] >= A[i]) {
if(m == L || pos[m+1] < A[i]) {
newSet = m+1;break;
} else
l = m+1;
} else {
r = m-1;
}
}
if(newSet == -1) newSet++;
pos[newSet] = A[i];
LDS[i] = newSet+1;
if(L < newSet) L = newSet;
}
}
int main() {
int T, N, i;
int A[2001], LDS[2001], LIS[2001], pos[2001];
scanf("%d", &T);
while(T--) {
scanf("%d", &N);
for(i = 0; i < N; i++)
scanf("%d", &A[N-i-1]);
findLIS(N, LIS, pos, A);
findLDS(N, LDS, pos, A);
int max = 0;
for(i = 0; i < N; i++) {
if(LIS[i]+LDS[i]-1 > max)
max = LIS[i]+LDS[i]-1;
}
printf("%d\n", max);
}
return 0;
}

台: Morris
人(1,506) | 回(2)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 193 - Graph Coloring
此分上一篇:[UVA] 10827 - Maximum sum on a torus

Axddas
Great submit, the an extremely great website which you have the following, maintain the nice perform, will probably be again. á gà cpc1 trng u ng cp bc nht ti ng nam á á gà cpc2 u trng gà campuchia nh cao nm 2025 http://adhesive.ru.com/
2025-10-06 18:48:34
seo
Awesome posting, it has the such a interesting site there is listed here, keep up to date favorable deliver the results, might be backside. iptv nordic http://nordic-one-iptv.com/
2025-10-07 20:02:20
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86