HDU1257最少拦截系统

本文介绍了一种优化算法,用于解决查找数组中降序子序列的数量问题。通过减少循环次数和优化判断条件,提高了算法效率。示例代码展示了如何实现这一算法,并通过实例验证了其正确性和性能提升。

/*求有多少个降序子序列*/

#include<iostream>
using namespace std;

int main()
{
    int n;
    int a[1000];
    while (cin >> n)
    {
        if (n == 0)
        {
            cout << 1 << endl;
            continue;
        }
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
        }
        int tem = 0, min = a[0];
        there:
        for (int i = 1; i < n; ++i)
        {
            if (a[i] != 0 && a[i] < min)
            {
                min = a[i];
                a[i] = 0;
            }
            if (i == n - 1)
            {
                tem++;
                for (int t = 1; t < n; ++t)
                {
                    if (a[t] != 0)
                    {
                        min = a[t];
                        a[t] = 0;  //赋值后使其等于零
                        goto there;
                    }
                        
                }
            }
        }
        cout << tem << endl;
    }
    return 0;
}



/*
看到一个算法比我精简的
#include<stdio.h>
#include<string.h>
int main()
{
int i,n,count,a[1005],res,max;
while (scanf("%d",&n)!=EOF)
{
for (i=1; i<=n; ++i)
{
scanf("%d",&a[i]);
}
res=0;
count=n;
while (count)
{
max=50000;
res++;
for (i=1; i<=n; ++i)
{
if (a[i]>0 && a[i]<max)
{
max=a[i];
a[i]=-1;
count--;
}
}
}
printf("%d/n",res);
}
return 0;
}


*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值