湖北民族学院oj 1883 之 Sequence Number

本文探讨了在一组有序的不同整数中寻找序列对的最大长度问题,并提供了一段C++实现代码作为解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1883: Sequence Number


Time Limit: 1 Sec     Memory Limit: 1280 MB
Total Submissions: 20     Accepted: 6
[ Submit]   [ Statistic]   [ Go Back]

题目描述

     In Linear algebra, we have learned the definition of inversion number:

    Assuming A is a ordered set with n numbers ( n > 1 ) which are different from each other. If exist positive integers i , j, ( 1 ≤ i < j ≤ n and A[i] > A[j]), <A[i], A[j]> is regarded as one of A’s inversions. The number of inversions is regarded as inversion number. Such as, inversions of array <2,3,8,6,1> are <2,1>, <3,1>, <8,1>, <8,6>, <6,1>,and the inversion number is 5.

     Similarly, we define a new notion —— sequence number, If exist positive integers i, j, ( 1 ≤ i ≤ j ≤ n and A[i]  <=  A[j], <A[i], A[j]> is regarded as one of A’s sequence pair. The number of sequence pairs is regarded as sequence number. Define j – i as the length of the sequence pair.

     Now, we wonder that the largest length S of all sequence pairs for a given array A. 

输入描述

    There are multiply test cases.

    In each case, the first line is a number N(1<=N<=50000 ), indicates the size of the array, the 2th ~n+1th line are one number per line, indicates the element Ai (1<=Ai<=10^9) of the array.  

输出描述

 Output the answer S in one line for each case. 

输入样例

5
2 3 8 6 1

输出样例

3


AC代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;

int a[maxn];

int main()
{
	int n,maxi;
	while(cin>>n)
	{
	    maxi=0;
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=n-1;i>=0;i--)
        {
            for(int j=0;j+i<n;j++)
            {
                if(j<i && a[j]<=a[i+j])
                {
                    maxi=max(maxi,i);
                    i=-1;j=n;//退出多重循环
                    break;
                }
            }
        }
        cout<<maxi<<endl;
	}
	return 0;
}


### 关于 XTU OJ 平台上的 K-Good Number 问题 #### 题目描述 题目要求计算 n 位无前导零的二进制数中,“好数”的数量。所谓“好数”,是指该数的二进制表示中,数码 `1` 的个数大于数码 `0` 的个数。 --- #### 解题思路 为了求解此问题,可以采用动态规划的方法来高效解决: 1. **状态定义** 定义 `dp[i][j]` 表示 i 位二进制数中,有 j 个 `1` 的所有可能组合的数量[^5]。 2. **转移方程** 对于每一位二进制数,可以选择填入 `0` 或者 `1`: - 填入 `0`: 则当前的状态由上一位少一个位置且相同数量的 `1` 转移而来,即 `dp[i][j] += dp[i-1][j]`。 - 填入 `1`: 当前的状态由上一位少一个位置且 `1` 数量减少一的情况转移而来,即 `dp[i][j] += dp[i-1][j-1]`。 3. **边界条件** 初始化时,当只有一位二进制数时: - 若只有一个 `1` (`dp[1][1]=1`); - 若只有 `0` (`dp[1][0]=1`)。 4. **最终结果** 计算满足条件的结果:对于给定的 n 位二进制数,统计其中 `1` 的个数严格大于 `0` 的情况总数。由于最高位不能为 `0` (因为不允许前导零),因此需要特别处理第一位固定为 `1` 后剩余部分的可能性。 --- #### 动态规划实现代码 以下是基于上述分析的 Python 实现代码: ```python def count_good_numbers(n): MOD = 10**9 + 7 # Initialize DP table with zeros dp = [[0]*(n+1) for _ in range(n+1)] # Base case initialization dp[0][0] = 1 # Fill the DP table using recurrence relations for i in range(1, n+1): # For each bit position from 1 to n for j in range(i+1): # Possible number of '1's at this stage if j > 0: dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % MOD # Case when current bit is '1' dp[i][j] = (dp[i][j] + dp[i-1][j]) % MOD # Case when current bit is '0' result = 0 for ones in range((n//2)+1, n+1): # Count cases where '1' exceeds '0' result = (result + dp[n][ones]) % MOD return result # Example usage if __name__ == "__main__": n = int(input()) print(count_good_numbers(n)) ``` --- #### 复杂度分析 - 时间复杂度: \(O(n^2)\), 主要来源于双重循环填充 DP 表格。 - 空间复杂度: \(O(n^2)\), 使用二维数组存储中间状态。 通过优化空间复杂度(仅保留两行数据),可进一步降低内存消耗至 \(O(n)\)。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值