Sifid and Strange Subsequences

本文介绍了Codeforces Round #722 (Div.2) B题的解决方案。题目要求找到最长子序列,使得序列中任意两个数的绝对差大于子序列的最大值。解题策略采用贪心算法,根据0的个数分为两种情况:当0的个数小于等于1时,可能存在正数;当0的个数大于1时,序列中不能有正数。通过排序和遍历,找到满足条件的子序列长度。

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

Codeforces Round #722 (Div. 2) B. Sifid and Strange Subsequences

题目链接
题目大意:给定你n个数,让你求一个最长子序列满足其中任意两个数的绝对差,均大于该子序列的最大值;
思路:贪心,该子序列最多只能有一个正数,并且判断0的个数;
第一种情况:0的个数小于等于1,此时允许正数存在,所以查找距离非正数最近的哪一个数,并且判断之后是否存在两个数的的绝对差小于该整数,如果存在则删除该正数。
第二种情况:0的个数大于1,此时不能存在正数,所以只需求小于等于0的个数,即为答案;

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<stack>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		int ans;
		scanf("%d",&n);
		int sum=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
			if(a[i]==0){
				sum++;
			}
		}
		sort(a,a+n);
		if(sum<=1){
			for(int i=n-1;i>=0;i--){
				if(a[i-1]<=0){
					ans=i+1;
					for(int j=i;j>=1;j--){
						if(a[j]-a[j-1]<a[i]){
							ans--;
							break;
						}
					}
					break;
				}
			}
		}
		else{
			for(int i=n-1;i>=0;i--){
				if(a[i]==0){
					ans=i+1;
					break;
				}
			}
		}
		printf("%d\n",ans); 
	} 
}


### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值