UVA497 Strategic Defense Initiative【LIS+DP】

大城市里の小女人 提交于 2020-04-06 13:28:19

“Commander! Commander! Please wake up commander!”
    “… mmmph. What time is it?”
    “4:07 am, Commander. The following message just arrived on the emergency zeta priority classified scrambler, marked your eyes only.”
    You grudgingly take the letter, rub the sleep from your eyes, fleetingly wish that the ’Backer closed at an earlier hour, and start to read.


``Dear StarWars SDI Commander,
Bad news, buddy. Crazy Boris had a bit too much vodka last night and when he woke up this morning, instead of the snooze button on his alarm clock, he … well, let me put it this way: we’ve got tons of nuclear missles flying this way. Unfortunately, all that we have is a chart of the altitudes at which the missles are flying, arranged by the order of arrivals. Go for it, buddy.
Good luck.
Secretary of Defense
P.S. Hilly and Bill say hi.’’



To make things worse, you remeber that SDI has a fatal flaw due to the budget cuts. When SDI sends out missles to intercept the targets, every missle has to fly higher than the previous one. In other words, once you hit a target, the next target can only be among the ones that are flying at higher altitudes than the one you just hit.
    For example, if the missles are flying toward you at heights of 1, 6, 2, 3, and 5 (arriving in that order), you can try to intercept the first two, but then you won’t be able to get the ones flying at 2, 3, 5 because they are lower than 6. Your job is to hit as many targets as possible. So you have to quickly write a program to find the best sequence of targets that the flawed SDI program is going to destroy.
    Russian war tactics are fairly strange; their generals are stickers for mathematical precision. Their missles will always be fired in a sequence such that there will only be one solution to the problem posed above.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input to your program will consist of a sequence of integer altitudes, each on a separate line.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    Output from your program should contain the total number of targets you can hit, followed by the altitudes of those targets, one per line, in the order of their arrivals.
Sample Input
1









1
6
2
3
5
Sample Output
Max hits: 4
1
2
3
5









问题链接UVA497 Strategic Defense Initiative
问题简述:(略)
问题分析:本题与参考链接的题类似,也是计算最长递增子序列,只是输入输出格式略有不同。
程序说明:(略)
参考链接UVA481 What Goes Up【LIS+DP】
题记:(略)




AC的C++语言程序如下:

/* UVA497 Strategic Defense Initiative */

#include <bits/stdc++.h>

using namespace std;

const int N = 10000;
int a[N], dp[N], pos[N], ans[N];
char s[20];

int main()
{
    int t;
    while(~scanf("%d", &t)) {
        getchar();
        getchar();
        while(t--) {
            int cnt = 0;
            while(gets(s) && s[0]) a[cnt++] = atoi(s);

            memset(dp, 0x7F, sizeof(dp));
            int len = 1;
            for(int i = 0; i < cnt; i++) {
                int p = lower_bound(dp, dp + len, a[i]) - dp;
                dp[p] = a[i];
                pos[i] = p + 1;
                if(p == len) len++;
            }

            int len2 = len;
            for( int i = cnt - 1; i >= 0; i-- ) {
                if( pos[i] == len2 ) {
                    ans[len2] = a[i];
                    if( --len2 == 0 ) break;
                }
            }

            if(cnt) {
                printf("Max hits: %d\n", len);
                for( int i = 1; i <= len; i++ )
                    printf("%d\n", ans[i]);
            } else printf("Max hits: 0\n");
            if(t) printf("\n");
        }
    }

    return 0;
}
发布了2244 篇原创文章 · 获赞 2364 · 访问量 262万+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!