(最长上升子序列 简单dp)

虽然是简单dp但我就是做不出来 T^T
J - X and Beasts
X is fighting beasts in the forest, in order to have a better chance to survive he’s gonna buy upgrades for his weapon. Weapon upgrade shops are available along the forest, there are n shops, where the ith of them provides an upgrade with energy ai. Unfortunately X can make use only of the maximum power of two that divides the upgrade energy, lets call it the powerincrement. For example, an upgrade with energy of 6 has a power increment of 1 because 6 is divisible by 21 but not divisible by 22 or more, while for upgrade with energy of 5 power increment is 0, also after buying a weapon with energy v, X can only buy upgrades with energies greater than v, in other words, the upgrades energies that X is gonna buy should be in strictly increasing order. X is wondering what is the maximum power he can achieve at the end of his journey. note that only the energy of upgrades should be in strictly increasing order not the power increment of the upgrade. 1 < n < 100, 1 ≤ ai ≤ 106

Input
The first line of input contains one integer, T, which denotes number of test cases. Each test case contains two lines, the first one contains one integer n which denotes number of shops, and the next line contains n integers the ith of them denotes the energy of the upgrade provided by the ith shop.

Output
Print one integer denotes maximum power X can achieve at the end of his journey

Example
Input
2
3
1 10 16
2
8 12
Output
5
5
题意:按输入上升子序列的 能被2的几次幂整除的 最大和

#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <string.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int fin(int x)
{
    int count=0;
    while(x%2!=1)
    {
        count++;
        x/=2;
    }
    return count;
}
int main()
{
    int T,i,j,n,ans,a[101],dp[101];
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,-1,sizeof(dp));
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0; i<n; i++)
        {
            dp[i]=fin(a[i]);
            for(j=0; j<i; j++)
            {
                if(a[i]>a[j])
                {
                    dp[i]=max(dp[j]+fin(a[i]),dp[i]);
                }
            }
        }
        ans=-1;
        for(i=0; i<n; i++)
        {
            //printf("%d ",dp[i]);
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
//最长上升子序列 啊啊啊 啊我就是个菜鸡啊T^T

D - Sequences
One of the most wonderful qualities of an ACMer is to be multi interests so he combines multiple qualifications and hobbies not just coding. Hussain is one of the most qualified ACMers to mention when talking about hobbies and other domains of personality training despite of his qualifications in ACM problem solving and math. It’s very known about Hussain his obsession in hunting and shooting, he used to pass hours training on empty cans in his childhood. These days, Hussain became a professional and still challenge others in this game, but for his bad luck he accidentally challenged a professional ACMer, without mentioning the name, so this ACMer made a game for Hussain. He numbered N targets for Hussain with random numbers and challenged him to shoot the minimum number of targets so the remaining numbers will form a sequence of increasing (by one) numbers in their current order. Example: if there is 6 targets numbered as follow: 2 5 7 3 2 4 Hussain will shoot 5,7 and the second 2 remaining for 2 3 4. Now, Hussain will focus on shooting, we will help him and focus on the targets he must shoot. But No! Hussain is an very good ACMer, we will make it hard for him and just tell him the number of the remaining targets in the sequence.

Input
First line contain an integer T represents the number of test cases 0 < T < 100, each test case consists of two lines, the first one is an integer 0< N < 20000 represents the number of targets, then followed by the second line that contains N numbers each number 0 < Xi < 20000 represents the number written on the i’th target.

Output
For each test case print one number represents the remaining sequence’s length can be created by the input where it should be the maximum length and each number of it follow its previous by 1.

Examples
Input
4
6
2 5 7 3 2 4
7
2 18 65 33 11 5 4
5
2 7 5 9 3
5
9 8 7 10 11
Output
3
1
2
3
Note
Please consider a large input file.
找最长上升子序列,要求是必须是连续的数字比如3-4-5

#include <stdio.h>
#include <algorithm>
#include <memory.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
    int T,i,x,ans,n,dp[20010];
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        ans = -INF;
        for(i=0; i<n; i++)
        {
            scanf("%d",&x);
            dp[x]=1; //将出现的标记为1
            dp[x]=dp[x-1]+1;//就是找最长上升子序列但两个循环会超时
            ans=max(ans,dp[x]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
 ans = -INF; //会超时
        for(i=0;i<n;i++)
        {
            dp[i]=1;
            for(j=0;j<=i;j++)
            {
                if(a[i]==a[j]+1)
                {
                    dp[i]=max(dp[j]+1,dp[i]);
                }
            }
            ans = max(dp[i], ans);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值